-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiral.py
More file actions
137 lines (115 loc) · 4.81 KB
/
Copy pathspiral.py
File metadata and controls
137 lines (115 loc) · 4.81 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from tkinter import *
from math import *
import time
import numpy as np
root = Tk()
root.title("Спиральная поверхность")
root.geometry("640x600")
root.resizable(False, False)
PRECISION = 20
WIDTH = 640
HEIGHT = 481
INTENSITY = 5
def changeAlpha(val):
global polys
polys = genPolys(spiral, 4 * pi, 2 * pi, PRECISION, float(alpha.get()), float(beta.get()))
draw()
def changeBeta(val):
global polys
polys = genPolys(spiral, 4 * pi, 2 * pi, PRECISION, float(alpha.get()), float(beta.get()))
draw()
canvas = Canvas(bg="white", width=WIDTH, height=HEIGHT)
alpha = Scale(orient=HORIZONTAL, length=200, from_=1.0, to=5.0, resolution=0.2, command=changeAlpha)
beta = Scale(orient=HORIZONTAL, length=200, from_=0.2, to=1.5, resolution=0.1, command=changeBeta)
alpha.pack(anchor=CENTER, expand=1)
beta.pack(anchor=CENTER, expand=1)
canvas.pack(anchor=CENTER, expand=1)
def spiral(u, v, alpha, beta):
return np.array([(alpha + beta * cos(v) ) * cos(u), (alpha + beta * cos(v)) * sin(u), beta * sin(v) + alpha * u])
def genPolys(f, maxU, maxV, precision, alpha, beta):
polys = []
for t1 in range(precision):
for t2 in range(precision):
polys.append((
f(((t1) * maxU / precision), ((t2) * maxV / precision), alpha, beta),
f(((t1) * maxU / precision), ((t2 + 1) * maxV / precision), alpha, beta),
f(((t1+1) * maxU / precision), ((t2 + 1) * maxV / precision), alpha, beta),
))
polys.append((
f(((t1) * maxU / precision), ((t2) * maxV / precision), alpha, beta),
f(((t1+ 1) * maxU / precision), ((t2) * maxV / precision), alpha, beta),
f(((t1+1) * maxU / precision), ((t2 + 1) * maxV / precision), alpha, beta),
))
return polys
polys = genPolys(spiral, 4 * pi, 2 * pi, PRECISION, float(alpha.get()), float(beta.get()))
def translate(point, vec):
trMat = np.eye(4)
trMat[0][3] = vec[0]
trMat[1][3] = vec[1]
trMat[2][3] = vec[2]
return trMat.dot(point)
def scale(point, vec):
trMat = np.eye(4)
trMat[0][0] = vec[0]
trMat[1][1] = vec[1]
trMat[2][2] = vec[2]
return trMat.dot(np.append(point, 1)[:, np.newaxis])
def rotateX(vec, phi):
rMat = np.eye(4)
rMat[1][1] = cos(phi)
rMat[1][2] = -sin(phi)
rMat[2][1] = sin(phi)
rMat[2][2] = cos(phi)
return rMat.dot(vec)
def rotateY(vec, phi):
rMat = np.eye(4)
rMat[0][0] = cos(phi)
rMat[0][2] = sin(phi)
rMat[2][0] = -sin(phi)
rMat[2][2] = cos(phi)
return rMat.dot(vec)
def rotateZ(vec, phi):
rMat = np.eye(4)
rMat[0][0] = cos(phi)
rMat[0][1] = -sin(phi)
rMat[1][0] = sin(phi)
rMat[1][1] = cos(phi)
return rMat.dot(vec)
tMat = 64*np.eye(4)
def drawGrid():
for i in range(21):
Xline = [[-10, -10 + i, 0, 1],[10,-10 + i,0, 1]]
Yline = [[10 - i, -10, 0, 1],[10 - i,10,0, 1]]
Zline = [[0, 0, 10, 1],[0,0,-10, 1]]
canvas.create_line(int(tMat.dot(Xline[0])[0] + WIDTH / 2),int(tMat.dot(Xline[0])[1] + HEIGHT / 2),int(tMat.dot(Xline[1])[0] + WIDTH / 2),int(tMat.dot(Xline[1])[1] + HEIGHT / 2))
canvas.create_line(int(tMat.dot(Yline[0])[0] + WIDTH / 2),int(tMat.dot(Yline[0])[1] + HEIGHT / 2),int(tMat.dot(Yline[1])[0] + WIDTH / 2),int(tMat.dot(Yline[1])[1] + HEIGHT / 2))
canvas.create_line(int(tMat.dot(Zline[0])[0] + WIDTH / 2),int(tMat.dot(Zline[0])[1] + HEIGHT / 2),int(tMat.dot(Zline[1])[0] + WIDTH / 2),int(tMat.dot(Zline[1])[1] + HEIGHT / 2))
for i in range(-10, 11):
ZStroke = [[-0.125, 0, i,1], [0.125, 0, i,1]]
canvas.create_line(int(tMat.dot(ZStroke[0])[0] + WIDTH / 2),int(tMat.dot(ZStroke[0])[1] + HEIGHT / 2),int(tMat.dot(ZStroke[1])[0] + WIDTH / 2),int(tMat.dot(ZStroke[1])[1] + HEIGHT / 2))
def draw():
canvas.delete("all")
drawGrid()
global polys
polys = sorted(polys, key = lambda x : tMat.dot(np.append(x[1], 1))[2])
for poly in polys:
polyProj = list(map(lambda x : tMat.dot(np.append(x, 1)[:, np.newaxis]), poly))
canvas.create_polygon(int(polyProj[0][0] + WIDTH / 2), int(polyProj[0][1] + HEIGHT / 2), int(polyProj[1][0] + WIDTH / 2), int(polyProj[1][1] + HEIGHT / 2), int(polyProj[2][0] + WIDTH / 2), int(polyProj[2][1] + HEIGHT / 2), fill='#0000' + "{0:02X}".format(min(255, abs(int(INTENSITY * polyProj[0][2])))), outline='#000000')
draw()
def keydown(e):
global tMat
if e.char == "w":
tMat *= 1.1
elif e.char == "s":
tMat *= 1/1.1
elif e.keycode == 111:
tMat = rotateX(tMat, (pi / 16))
elif e.keycode == 116:
tMat = rotateX(tMat, (-pi / 16))
elif e.keycode == 113:
tMat = rotateY(tMat, (pi / 16))
elif e.keycode == 114:
tMat = rotateY(tMat, (-pi / 16))
draw()
root.bind("<KeyPress>", keydown)
root.mainloop()