-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturtle torus
116 lines (90 loc) · 3.24 KB
/
turtle torus
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
import turtle
import math
edges = {}
width = 0
height = 0
currentEdge = ''
endEdge = ''
nextX = 0
nextY = 0
def square(x):
for edge in range(4):
turtle.forward(x)
turtle.left(90)
#makes square around origin
def markOrigin(x):
turtle.pu()
turtle.goto(0, 0)
turtle.pd()
for squares in range(4):
square(x)
turtle.left(90)
return
def torus(x,y):
global edges
global width
global height
width=x
height=y
#edges format (x1,y1, x2,y2, turtlebearing), positive angle, 0 angle, negative angle)
edges = {'A0' : (0,0, 0,y, 90,'B0','A1','B1'),
'A1' : (x,0, x,y, 90,'B0','A0','B1'),
'B0' : (0,0, x,0, 0, 'A1','B1','A0'),
'B1' : (0,y, x,y, 0, 'A1','B0','A0'),
}
#For each edge in the edge table, the edge is drawn
for edge in edges:
turtle.pu()
turtle.goto(edges[edge][0], edges[edge][1])
turtle.pd()
turtle.goto(edges[edge][2], edges[edge][3])
#returns the distance between two points
def distance(x1,y1,x2,y2):
dist = math.hypot(x2 - x1, y2 - y1)
return dist
#Inputs: current edge, a position, and an angle
#Result: finds the edge on the same face which a ray would intersect
def setNext(edge, x0, y0, theta):
global endEdge, nextX, nextY
#tests each of the edges which a given edge shares a face with
for i in range(5, len(edges[edge])):
potentialEdge = edges[edge][i]
x1= edges[potentialEdge][0]
y1= edges[potentialEdge][1]
x2= edges[potentialEdge][2]
y2= edges[potentialEdge][3]
#calculating intersection pt of each set of lines, sets this as x and y
m1 = math.tan(math.radians(theta))
if(x2==x1):
x=x1
else:
m2 = ((y2-y1)/(x2-x1))
x = ((m1*x0 - x1*m2 + y1 - y0)/(m1-m2))
y = m1*(x-x0)+y0
#setting nextEdge, nextX, nextY if the solution (x,y) for an edge is between (x1,y1) and (x2,y2)
if((0.999*min(y1,y2)<= y <=1.001*max(y1,y2)) and (min(x1,x2) <= x <= max(x1,x2))):
endEdge = potentialEdge
nextX=x
nextY=y
print(endEdge)
print(str(nextX) + "," + str(nextY))
def jump(edge, length, angle):
global currentEdge
currentEdge = edge
#Moving into position
turtle.pu()
#Go to length on edge
turtle.goto(edges[edge][0]+length*math.cos(math.radians(edges[edge][4])),edges[edge][1]+length*math.sin(math.radians(edges[edge][4])))
turtle.setheading(angle)
turtle.pd()
print("jumped to:" + str(turtle.xcor()) + ", " + str(turtle.ycor()))
setNext(currentEdge, turtle.xcor(), turtle.ycor(), angle)
turtle.goto(nextX, nextY)
print("went to:" + str(nextX) + ", " + str(nextY))
nextEdge = str(endEdge[0])+ str((int(endEdge[1])+1)%2)
nextLength = distance(turtle.xcor(), turtle.ycor(), edges[endEdge][0], edges[endEdge][1])
nextAngle = ((edges[nextEdge][4] - edges[endEdge][4] + angle)%360)
print("jump(" + str(nextEdge) + ", " + str(nextLength) + ", " + str(nextAngle) + ")")
jump(nextEdge, nextLength, nextAngle)
torus(200,200)
jump('A0', 5, 20)