-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturtleplot.py
122 lines (99 loc) · 4.9 KB
/
turtleplot.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 1 18:34:46 2020
@author: Roneet
"""
import matplotlib.pyplot as plt
import numpy as np
def turtlePlot(turtleCommands):
# TURTLEPLOT Creates and displays a matplotlib pyplot based on the
# turtleCommands.
#
# Usage: turtlePlot(turtleCommands)
#
# Takes an array 'turtleCommands' generated by the turtleGraph function as
# an argument. Calculates X and Y coordinates for points, which are passed
# to the matplotlib.pyplot.plot function as two arrays, one for X and one
# for Y coordinates.
#
# Author: Roneet V. Nagale, s204091@dtu.dk, 2020
angleSpecArray = []
# Initialize an array of direction-vectors with an initial vector (1,0),
# the canonical basis vector.
arrayOfDirections=np.array([[1,0]])
# Initialize an array if points with the origin for the planar coordinate-
# system.
arrayOfPoints=np.array([[0,0]])
# Assign the length specification to a variable. It is the 0th element of
# the turtleCommands array (as well as all elements with even indices).
# Because of how it's calculated in the turtleGraph function, the length
# specification is constant throughout the turtleCommands array and thus
# only needs to be read once.
lengthSpec=turtleCommands[0]
#Based on the structure of the turtleCommands array, create a new array
#with only the angle specifications (elements of the turtleCommands array
# with odd indices).
for specIndex in range(0, len(turtleCommands)):
if specIndex%2 != 0:
angleSpecArray=np.append(angleSpecArray, turtleCommands[specIndex])
# Use the array of angle specifications, iterate through it using a for
# loop and calculate the values of new points.
for angleSpec in angleSpecArray:
# Pick the last/latest added element in the array of points.
currentPoint=arrayOfPoints[-1]
# Calculate a new point, based on the current point, the length
# specification and the latest direction vector in the array of
# directions.
newPoint = currentPoint+lengthSpec*arrayOfDirections[-1]
# Add the new point to the array of points.
arrayOfPoints=np.vstack((arrayOfPoints, newPoint))
# Calculate a new direction, by taking a dot product of a matrix of
# cosine, -sine, sine and cosine of the angle specification, with
# the last direction vector recorded in the array of directions.
newDirection=np.array([np.dot([[np.cos(angleSpec),-np.sin(angleSpec)],
[np.sin(angleSpec),np.cos(angleSpec)]],arrayOfDirections[-1])])
# Add the new direction vector to the array of directions. Reiterate
# loop until all angle specifications are used.
arrayOfDirections=np.vstack((arrayOfDirections, newDirection))
# After the loop is done and the array of points is filled, add an ending
# point, based again on the canonincal basis vector and the latest point
# added to the array of points. (*This might not be the correct solution
# to have the graph appear symmetrical)
arrayOfPoints=np.vstack((arrayOfPoints, arrayOfPoints[-1]+lengthSpec*
arrayOfDirections[0]))
# Transpose the array of points (which are arrays themselves), to split it
# into an array of X values and an array of Y values.
plotArray = np.transpose(arrayOfPoints)
# Pass the two arrays of X and Y values to the matplotlib.pyplot.plot
# function, and show the plot.
plt.plot(plotArray[0], plotArray[1])
#Save the figure to a variable, as it is deleted once plt.show() is called.
currentFigure = plt.gcf()
plt.show()
#Give the user an option to save the plot.
while True:
saveChoice = input("""Do you want to save the figure?
(Input 1 to save, else press return to skip.)
(Note: There is no check in place for existing
files. Choose a new name each time to avoid
overwriting.)
""")
if saveChoice == '1':
filename = input("Please name the file.")
try:
filename = str(filename)
except:
print("Please enter a string!")
continue
else:
try:
currentFigure.savefig(filename+".svg")
except:
print("Unable to save file.")
else:
print("Saved successfully to "+filename+".svg")
break
else:
break
input("Returning to main menu.")
return