-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
115 lines (91 loc) · 3.06 KB
/
camera.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
import json
import math
import numpy
import itertools
import tkinter
WIDTH = 1024
HEIGHT = 768
MOVE_STEP = 3
ZOOM_STEP = 30
ROTATION_STEP = math.pi / 30
COLORS = ('white', 'red', 'green', 'blue', 'cyan', 'yellow', 'magenta')
with open('state.json') as f:
state = json.load(f)
def zoom(positive):
state['distance'] += ZOOM_STEP if positive else -ZOOM_STEP
def move(vector):
def translate(point):
return list(numpy.sum([point, vector], axis=0))
state['polygons'] = list(map(lambda p: list(map(translate, p)),
state['polygons']))
def turn(axis, direction):
angle = direction * ROTATION_STEP
matrix = {
'x': [
[1, 0, 0, 0],
[0, math.cos(angle), -1 * math.sin(angle), 0],
[0, math.sin(angle), math.cos(angle), 0],
[0, 0, 0, 1]
],
'y': [
[math.cos(angle), 0, math.sin(angle), 0],
[0, 1, 0, 0],
[-1 * math.sin(angle), 0, math.cos(angle), 0],
[0, 0, 0, 1]
],
'z': [
[math.cos(angle), -1 * math.sin(angle), 0, 0],
[math.sin(angle), math.cos(angle), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
]
}.get(axis)
def rotate(point):
return list(numpy.matmul(matrix, point + [1])[:-1])
state['polygons'] = list(map(lambda p: list(map(rotate, p)),
state['polygons']))
root = tkinter.Tk()
root.title('Camera')
canvas = tkinter.Canvas(root, width=WIDTH, height=HEIGHT, bg='black')
def priority(polygon):
return math.sqrt(sum([e**2 for e in numpy.mean(numpy.array(polygon),
axis=0)]))
def project(point):
return (WIDTH / 2 + (state['distance'] * point[0] / point[2]),
HEIGHT / 2 - (state['distance'] * point[1] / point[2]))
def render():
canvas.delete(tkinter.ALL)
polygons = filter(lambda polygon: all(p[2] > 0 for p in polygon),
state['polygons'])
polygons = sorted(polygons, key=priority, reverse=True)
colors = itertools.cycle(COLORS)
for polygon in map(lambda p: list(map(project, p)), polygons):
color = next(colors)
if len(polygon) < 3:
canvas.create_line(polygon, fill=color)
else:
canvas.create_polygon(polygon, fill=color)
canvas.pack()
def key(event):
handler = {
'z': lambda: zoom(True),
'x': lambda: zoom(False),
'a': lambda: move([MOVE_STEP, 0, 0]),
'd': lambda: move([-MOVE_STEP, 0, 0]),
'w': lambda: move([0, 0, -MOVE_STEP]),
's': lambda: move([0, 0, MOVE_STEP]),
'r': lambda: move([0, -MOVE_STEP, 0]),
'f': lambda: move([0, MOVE_STEP, 0]),
'e': lambda: turn('y', -1),
'q': lambda: turn('y', 1),
'k': lambda: turn('x', -1),
'i': lambda: turn('x', 1),
'j': lambda: turn('z', -1),
'l': lambda: turn('z', 1),
}.get(event.char)
if handler:
handler()
render()
root.bind('<Key>', key)
render()
tkinter.mainloop()