Sierpinski square curve: Difference between revisions

Content added Content deleted
(add python example)
Line 460: Line 460:
printf(fn,svgfmt,{max(X)+xt+10,max(Y)+yt+10,points,xt,yt})
printf(fn,svgfmt,{max(X)+xt+10,max(Y)+yt+10,points,xt,yt})
close(fn)</lang>
close(fn)</lang>

=={{header|Python}}==
<lang Python>import matplotlib.pyplot as plt
import math


def nextPoint(x, y, angle):
a = math.pi * angle / 180
x2 = (int)(round(x + (1 * math.cos(a))))
y2 = (int)(round(y + (1 * math.sin(a))))
return x2, y2


def expand(axiom, rules, level):
for l in range(0, level):
a2 = ""
for c in axiom:
if c in rules:
a2 += rules[c]
else:
a2 += c
axiom = a2
return axiom


def draw_lsystem(axiom, rules, angle, iterations):
xp = [1]
yp = [1]
direction = 0
for c in expand(axiom, rules, iterations):
if c == "F":
xn, yn = nextPoint(xp[-1], yp[-1], direction)
xp.append(xn)
yp.append(yn)
elif c == "-":
direction = direction - angle
if direction < 0:
direction = 360 + direction
elif c == "+":
direction = (direction + angle) % 360

plt.plot(xp, yp)
plt.show()


if __name__ == '__main__':
# Sierpinski Square L-System Definition
s_axiom = "F+XF+F+XF"
s_rules = {"X": "XF-F+F-XF+F+XF-F+F-X"}
s_angle = 90

draw_lsystem(s_axiom, s_rules, s_angle, 3)</lang>


=={{header|Quackery}}==
=={{header|Quackery}}==