Lines

Lesson 18: Drawing Lines

from fltk import *
import time

class MyApp(Fl_Window):
    def __init__(self, x,y, w, h, label=None):
        Fl_Window.__init__(self, x, y, w, h, label)
        self.color(FL_BLACK)
        self.end()

    def draw(self):
        Fl_Window.draw(self) #or the line below
        #super().draw() 

        sep=100
        fl_color(FL_RED) #set color before line style in Windows
        #fl_line_style(FL_DOT, 10)
        for x in range(0,self.w()+1, sep):# vertical lines
            fl_line(x,0,x,self.h())
        fl_color(FL_YELLOW)
        for y in range(0,self.h()+1, sep):# horizontal lines
            fl_line(0,y,self.w(),y)

size=601
app = MyApp(0,0, size, size)
app.show()
Fl.run()

Second assignment overriding handle

from fltk import *
import time

class MyApp(Fl_Window):
    def __init__(self, x,y, w, h, label=None):
        Fl_Window.__init__(self, x, y, w, h, label)
        self.color(FL_BLACK)
        self.end()
        self.sep=10

    def handle(self, event):
        r=super().handle(event)
        if event==FL_SHORTCUT:
            if Fl.event_key()==FL_Up:
                if self.sep < self.w()-1: #max window size
                    self.sep +=1
                    self.redraw()
                return 1
            elif Fl.event_key()==FL_Down:
                if self.sep > 1: #min value 1
                    self.sep -=1
                    self.redraw()
                return 1
        return r

    def draw(self):
        Fl_Window.draw(self)

        fl_color(FL_RED)
        for x in range(0,self.w()+1,self.sep):
            fl_line(x,0,x,self.h())
        for y in range(0,self.h()+1, self.sep):
            fl_line(0,y,self.w(),y)

size=301
app = MyApp(0,0, size, size)
app.show()
Fl.run()

Draw an Equilateral Triangle and draw lines from each side to the adjacent side at 10 pixel intervals as shown in the graphic below.

eqtriangle.png

Here is the math to get the line equations:

eqtrianglemath.png

Now the code which creates the graphic:

from fltk import *

class MyApp(Fl_Window):
    def __init__(self, x,y, w, h, label=None):
        Fl_Window.__init__(self, x, y, w, h, label)
        #create lists of (x,y) 61 points along each line
        self.top=[ (x,10) for x in range(0,601,10) ]
        self.left= [ (x, round(1.732*x)+10) for x in range(0, 301, 5) ]
        self.right = [ (x, round(-1.732*x+1039.2)+10) for x in range(300, 601, 5) ]

    def draw(self):
        Fl_Window.draw(self)
        fl_color(FL_BLUE)
        for i in range(61):
            #fl_line(x1,y1,x2,y2) draws a line between (x1,y1) and (x2,y2)
            fl_line(self.top[i][0], self.top[i][1], self.left[60-i][0], self.left[60-i][1])

        fl_color(FL_RED)
        for i in range(61):
            fl_line(self.top[i][0], self.top[i][1], self.right[60-i][0], self.right[60-i][1])

        fl_color(FL_GREEN)
        for i in range(61):
            fl_line(self.left[i][0], self.left[i][1], self.right[i][0], self.right[i][1])

size=601
app = MyApp(0,0, size, size)
app.resizable(app)
app.show()
Fl.run()