So, I have my code here:
import turtle
class fractal:
"""A class containing methods that will replicate
various fractals.
Attributes:
num: An integer representing the number of iterations
t: A turtle graphics object imported from Turtle.py
"""
def __init__(self, num, t=turtle.Turtle()):
self.num = num
self.t = t
def kochCurve(self):
axiom = 'F'
for _ in range(self.num):
linSys = 'F+F--F+F'
axiom = axiom.replace('F',linSys)
for i in axiom:
if i == 'F':
self.t.forward(10)
elif i == '+':
self.t.left(60)
elif i == '-':
self.t.right(60)
def kochIsland(self):
axiom = 'F-F-F-F'
for _ in range(self.num):
linSys = 'F+FF-FF-F-F+F+F'
axiom = axiom.replace('F',linSys)
for i in axiom:
if i == 'F':
self.t.forward(10)
elif i == '+':
self.t.left(90)
elif i == '-':
self.t.right(90)
def tree(self):
axiom = '[X]'
for _ in range(self.num):
linSys = 'F-[[X]+X]+F[+FX]-X'
axiom = axiom.replace('X',linSys)
axiom = axiom.replace('F','FF')
self.t.setheading(90)
self.t.setposition(0,-300)
stack = []
for i in axiom:
if i == 'F':
self.t.forward(10)
elif i == '+':
self.t.right(25)
elif i == '-':
self.t.left(25)
elif i == '[':
pos = self.t.position()
head = self.t.heading()
stack.append((pos,head))
elif i == ']':
(pos,head) = stack.pop()
self.t.penup()
self.t.setposition(pos)
self.t.setheading(head)
self.t.pendown()
fractal(3).kochCurve()
Overall, the code just seems a bit redundant and LONG. I'm creating this class to share on a repository for some others to view. However, I don't particularly enjoy the way it looks.
Essentially, this class makes a turtle from Turtle Graphics and draws fractals with it. So far, I have the three different fractals. All that's needed to call into the object is an integer that will be the number of iterations.
What improvements can I make here?