Fractals

Lesson 22: Recursion drawing fractals

from fltk import *

def mid(a,b):
    '''calculate midpoint of a line segment'''
    x=round((a[0]+b[0])/2)
    y=round((a[1]+b[1])/2)
    return (x,y)

class sierpinski(Fl_Window):
    def __init__(self, level=7,x=100,y=0, w=700,h=700):
        super().__init__(x,y,w,h)
        self.level=level
        self.a=(0,h-50) #bottom left
        self.b=(w,h-50) #bottom right
        self.c=( round(w/2), round(w-((3**0.5)/2*w))-50) #top
        self.i=0

    def draw(self):
        super().draw()
        fl_color(FL_BLUE)
        fl_line_style(FL_SOLID,1)
        self.tri(self.level, self.a, self.b, self.c)

    def tri(self, n, a ,b, c):
        if n > 0:
            n -= 1
            self.i+=1
            print(self.i/2)
            fl_line(a[0], a[1], b[0], b[1])
            fl_line(b[0], b[1], c[0], c[1])
            fl_line(c[0], c[1], a[0], a[1])
            ab= mid(a,b)
            ac= mid(a,c)
            bc= mid(b,c)
            self.tri(n,  a, ab, ac)
            self.tri(n,  b, ab, bc)
            self.tri(n,  c, ac, bc)

app=sierpinski()
app.show()
Fl.run()

Sierpinski Triangle

sierpinski_triangle.png

Plus Fractal (aka Cross Fractal)

plusfractal.png

from fltk import *

class Plus(Fl_Window):
    def __init__(self, level=6 ,x=100,y=0, w=700,h=700):
        super().__init__(x,y,w,h)
        self.level=level
        self.length=w//2
        self.center=(w//2,h//2)

    def draw(self):
        super().draw()
        fl_color(FL_BLUE)
        fl_line_style(FL_SOLID,1)
        self.plus(self.level, self.length, self.center)

    def plus(self, n, length, center):
        length=round(length/2)
        left = (center[0]-length, center[1])
        top = (center[0], center[1]-length)
        right = (center[0]+length, center[1])
        bot = (center[0], center[1]+length)
        if n > 0:
            n -= 1
            fl_line(top[0], top[1], bot[0], bot[1])
            fl_line(left[0], left[1], right[0], right[1])
            self.plus(n, length, top)
            self.plus(n, length, right)
            self.plus(n, length, bot)
            self.plus(n, length, left)

app=Plus()
app.show()
Fl.run()

Sierpinsksi Carpet

siercarpet.png

from fltk import *

class Carpet(Fl_Double_Window):
    def __init__(self, level):
        super().__init__(729, 729,'Sierpinski Carpet') #3**6=729
        self.level=level
        self.color(FL_BLUE)

    def draw(self):
        super().draw()
        fl_color(FL_WHITE)
        self.pattern(self.level, (0,0), self.w())

    def pattern(self, n, loc, w):
        if n>0:
            w = round(w/3)
            fl_rectf(loc[0]+w, loc[1]+w, w, w)
            locations=[]
            for c in range(3):
                for r in range(3):
                    locations.append( (loc[0]+(r*w), loc[1]+(c*w)) )
            locations.pop(4) #do not recurse center square
            for point in locations:
                self.pattern(n-1, point, w)
app=Carpet(6)
app.show()
Fl.run()