-
Notifications
You must be signed in to change notification settings - Fork 1
/
l-system-plotter.py
76 lines (61 loc) · 2.24 KB
/
l-system-plotter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
#from matplotlib import cm
mpl.use('Agg')
import sys
from math import pi, sin, cos
DEGREES_TO_RADIANS = pi / 180
from math import isnan
plt.rcParams['axes.facecolor'] = 'black'
#cmap=plt.get_cmap('ocean')
def plot_coords(coords, fname, bare_plot=False):
if bare_plot:
# Turns off the axis markers.
plt.axis('off')
# Ensures equal aspect ratio.
plt.axes().set_aspect('equal', 'datalim')
# Converts a list of coordinates into
# lists of X and Y values, respectively.
X, Y = zip(*coords)
# Draws the plot.
# cm=plt.get_cmap('ocean')
plt.plot(X, Y, 'm');
plt.savefig(fname)
def print_coords(coords):
for (x, y) in coords:
if isnan(x):
print('<gap>')
else:
print('({:.2f}, {:.2f})'.format(x, y))
def turtle_to_coords(turtle_program, turn_amount=60):
# The state variable tracks the current location and angle of the turtle.
# The turtle starts at (0, 0) facing right (0 degrees).
state = (0.0, 0.0, 0.0)
# Throughout the turtle's journey, we "yield" its location. These coordinate
# pairs become the path that plot_coords draws.
yield (0.0, 0.0)
# Loop over the program, one character at a time.
for command in turtle_program:
x, y, angle = state
if command in 'F': # Move turtle forward
state = (x - cos(angle * DEGREES_TO_RADIANS),
y + sin(angle * DEGREES_TO_RADIANS),
angle)
yield (state[0], state[1])
elif command == 'B':
state = (x + cos(angle * DEGREES_TO_RADIANS),
y - sin(angle * DEGREES_TO_RADIANS),
angle)
yield (state[0], state[1])
elif command == '+': # Turn turtle clockwise without moving
state = (x, y, angle + turn_amount)
elif command == '-': # Turn turtle counter-clockwise without moving
state = (x, y, angle - turn_amount)
in_fname = sys.argv[1]
out_fname = sys.argv[2]
data = []
angle = int(sys.argv[3])
with open(in_fname, 'r') as file:
data = file.read().replace('\n', ' ')
plot_coords(turtle_to_coords(data, angle), out_fname)