-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy path3d_box.py
executable file
·132 lines (103 loc) · 3.33 KB
/
3d_box.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
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-18 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK
"""
Rotating 3D box wireframe & color dithering.
Adapted from:
http://codentronix.com/2011/05/12/rotating-3d-cube-using-python-and-pygame/
"""
import sys
import math
from operator import itemgetter
from demo_opts import get_device
from luma.core.render import canvas
from luma.core.sprite_system import framerate_regulator
def radians(degrees):
return degrees * math.pi / 180
class point(object):
def __init__(self, x, y, z):
self.coords = (x, y, z)
self.xy = (x, y)
self.z = z
def rotate_x(self, angle):
x, y, z = self.coords
rad = radians(angle)
c = math.cos(rad)
s = math.sin(rad)
return point(x, y * c - z * s, y * s + z * c)
def rotate_y(self, angle):
x, y, z = self.coords
rad = radians(angle)
c = math.cos(rad)
s = math.sin(rad)
return point(z * s + x * c, y, z * c - x * s)
def rotate_z(self, angle):
x, y, z = self.coords
rad = radians(angle)
c = math.cos(rad)
s = math.sin(rad)
return point(x * c - y * s, x * s + y * c, z)
def project(self, size, fov, viewer_distance):
x, y, z = self.coords
factor = fov / (viewer_distance + z)
return point(x * factor + size[0] / 2, -y * factor + size[1] / 2, z)
def sine_wave(min, max, step=1):
angle = 0
diff = max - min
diff2 = diff / 2
offset = min + diff2
while True:
yield angle, offset + math.sin(radians(angle)) * diff2
angle += step
def main(num_iterations=sys.maxsize):
regulator = framerate_regulator(fps=30)
vertices = [
point(-1, 1, -1),
point(1, 1, -1),
point(1, -1, -1),
point(-1, -1, -1),
point(-1, 1, 1),
point(1, 1, 1),
point(1, -1, 1),
point(-1, -1, 1)
]
faces = [
((0, 1, 2, 3), "red"),
((1, 5, 6, 2), "green"),
((0, 4, 5, 1), "blue"),
((5, 4, 7, 6), "magenta"),
((4, 0, 3, 7), "yellow"),
((3, 2, 6, 7), "cyan")
]
a, b, c = 0, 0, 0
for angle, dist in sine_wave(8, 40, 1.5):
with regulator:
num_iterations -= 1
if num_iterations == 0:
break
t = [v.rotate_x(a).rotate_y(b).rotate_z(c).project(device.size, 256, dist)
for v in vertices]
depth = []
for idx, face in enumerate(faces):
v1, v2, v3, v4 = face[0]
avg_z = (t[v1].z + t[v2].z + t[v3].z + t[v4].z) / 4.0
depth.append((idx, avg_z))
with canvas(device, dither=True) as draw:
for idx, depth in sorted(depth, key=itemgetter(1), reverse=True)[3:]:
(v1, v2, v3, v4), color = faces[idx]
if angle // 720 % 2 == 0:
fill, outline = color, color
else:
fill, outline = "black", "white"
draw.polygon(t[v1].xy + t[v2].xy + t[v3].xy + t[v4].xy, fill, outline)
a += 0.3
b -= 1.1
c += 0.85
if __name__ == "__main__":
try:
device = get_device()
main()
except KeyboardInterrupt:
pass