-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturtleplot.py
More file actions
97 lines (72 loc) · 3.38 KB
/
turtleplot.py
File metadata and controls
97 lines (72 loc) · 3.38 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 5 21:30:48 2018
@author: nik
"""
import numpy as np
import matplotlib.pyplot as plt
import math
def turtlePlot(turtleCommands, System, N):
# Initial starting point at origo
# This array will grow with iterations
# Will contain the final X and Y coordinates for plotting
xy = np.array( [[0], [0]] )
# Direction vector for every turn
# Used for calculating next coordinate
Orientation = np.array( [[1], [0]] )
# Finds every angle in the turtleCommands string
# and creates a single array for accessing angles
angles = np.array([turtleCommands[i] for i in range(len(turtleCommands)) if i % 2 == 1])
# Shorten line segment lengths according to system
# Will assess if system is korch or sierpinski
# divides lengths with respectively (1/3)^N for Korch and (1/2)^N for Sierpinski
# Also set system name to Capital first letter, used for the plot title
if System.lower() == 'korch':
length = np.array( [turtleCommands[i] / (3)**N for i in range(len(turtleCommands)) if i % 2 == 0])
System = 'Korch'
elif System.lower() == 'sierpinski':
length = np.array( [turtleCommands[i] / (2)**N for i in range(len(turtleCommands)) if i % 2 == 0])
System = 'Sierpinski'
if N %2 == 1 and N > 1 and System.lower() == 'sierpinski':
Orientation = np.dot([[np.cos(1/3*math.pi), -np.sin(1/3*math.pi)],[np.sin(1/3*math.pi), np.cos(1/3*math.pi)]], Orientation)
# Calculate next direction vector 'Orientation'
for i in range(0,len(angles)):
# Defining cos and sin variables 'functions' for direction handling
# Mainly initialized for more readably code in the matrix for direction calulation
cos = np.cos(angles[i])
sin = np.sin(angles[i])
# Creating matrix of direction function
# Using before-assigned cos and sin function where i in angle parsed from angles array
d = np.array( [[cos,-sin],[sin,cos]] )
# Calculating the matrix.vector product for new orientation vector
dot = np.array(np.dot(d, Orientation[:,i]),copy=False, subok=True, ndmin=2).T
# Appending new orientation to array of orientaions
Orientation = np.concatenate(( Orientation, dot), axis=1)
# Calculates next coordinate sets and add it to xy array for plotting
for i in range(0,len(length)):
# Calculate the vector
vector = np.array(xy[:,i]+length[i]*Orientation[:,i], copy=False, subok=True, ndmin=2).T
# Adding the new vector to'xy' array
xy = np.concatenate((xy, vector), axis=1)
# Deals with zero iterations ie. N=0
# Will draw a horizontal line for both systems if N<1
if len(turtleCommands) < 1:
xy = np.array( [[0,1], [0,0]] )
# Plots the system in a graph
plt.plot(xy[0], xy[1])
plt.title('System: {}, Iteration: {}'.format(System,N))
plt.xlim([0, 1])
# if System == 'Sierpinski' and len(turtleCommands) > 0:
# plt.ylim([0, ])
# elif System == 'Korch' and len(turtleCommands) > 0:
# plt.ylim([-0.2, ])
plt.yticks([])
plt.show()
return xy
import matplotlib
matplotlib.use('Agg')
def saveFig(xy, System, N, fileName):
plt.plot(xy[0],xy[1])
plt.title('System: {}, Iteration: {}'.format(System,N))
plt.savefig(fileName)