-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
358 lines (296 loc) · 9.83 KB
/
draw.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from display import *
from matrix import *
from math import *
from gmath import *
import random
def add_polygon( polygons, x0, y0, z0, x1, y1, z1, x2, y2, z2 ):
add_point(polygons, x0, y0, z0);
add_point(polygons, x1, y1, z1);
add_point(polygons, x2, y2, z2);
def draw_polygons( matrix, screen, color ):
if len(matrix) < 2:
print 'Need at least 3 points to draw'
return
point = 0
while point < len(matrix) - 2:
normal = calculate_normal(matrix, point)[:]
if normal[2] > 0:
variablecolor = [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)]
scan_line([matrix[point], matrix[point+1], matrix[point+2]], screen, variablecolor)
draw_line( int(matrix[point][0]),
int(matrix[point][1]),
int(matrix[point+1][0]),
int(matrix[point+1][1]),
screen, color)
draw_line( int(matrix[point+2][0]),
int(matrix[point+2][1]),
int(matrix[point+1][0]),
int(matrix[point+1][1]),
screen, color)
draw_line( int(matrix[point][0]),
int(matrix[point][1]),
int(matrix[point+2][0]),
int(matrix[point+2][1]),
screen, color)
point += 3
def scan_line(pts, screen, color):
pts = pts[:3]
#find b, m, t
bmt = sorted(pts, key = lambda x: x[1])
b = bmt[0]
m = bmt[1]
t = bmt[2]
dx1 = (b[0]-m[0])/(b[1]-m[1])
dx2 = (m[0]-t[0])/(m[1]-t[1])
dxr = (b[0]-t[0])/(b[1]-t[1])
cy = b[1]
cx1 = b[0]
cx2 = b[0]
while cy < m[1]:
cy += 1
cx1 += dx1
cx2 += dxr
cx1 = m[0]
cy = m[1]
while cy <= t[1]:
draw_line(int(cx1), int(cy), int(cx2), int(cy), screen, color)
cy += 1
cx1 += dx2
cx2 += dxr
def add_box( polygons, x, y, z, width, height, depth ):
x1 = x + width
y1 = y - height
z1 = z - depth
#front
add_polygon(polygons, x, y, z, x1, y1, z, x1, y, z);
add_polygon(polygons, x, y, z, x, y1, z, x1, y1, z);
#back
add_polygon(polygons, x1, y, z1, x, y1, z1, x, y, z1);
add_polygon(polygons, x1, y, z1, x1, y1, z1, x, y1, z1);
#right side
add_polygon(polygons, x1, y, z, x1, y1, z1, x1, y, z1);
add_polygon(polygons, x1, y, z, x1, y1, z, x1, y1, z1);
#left side
add_polygon(polygons, x, y, z1, x, y1, z, x, y, z);
add_polygon(polygons, x, y, z1, x, y1, z1, x, y1, z);
#top
add_polygon(polygons, x, y, z1, x1, y, z, x1, y, z1);
add_polygon(polygons, x, y, z1, x, y, z, x1, y, z);
#bottom
add_polygon(polygons, x, y1, z, x1, y1, z1, x1, y1, z);
add_polygon(polygons, x, y1, z, x, y1, z1, x1, y1, z1);
def add_sphere( edges, cx, cy, cz, r, step ):
points = generate_sphere(cx, cy, cz, r, step)
num_steps = int(1/step+0.1)
lat_start = 0
lat_stop = num_steps
longt_start = 0
longt_stop = num_steps
num_steps+= 1
for lat in range(lat_start, lat_stop):
for longt in range(longt_start, longt_stop):
p0 = lat * (num_steps) + longt
p1 = p0+1
p2 = (p1+num_steps) % (num_steps * (num_steps-1))
p3 = (p0+num_steps) % (num_steps * (num_steps-1))
if longt != num_steps - 2:
add_polygon( edges, points[p0][0],
points[p0][1],
points[p0][2],
points[p1][0],
points[p1][1],
points[p1][2],
points[p2][0],
points[p2][1],
points[p2][2])
if longt != 0:
add_polygon( edges, points[p0][0],
points[p0][1],
points[p0][2],
points[p2][0],
points[p2][1],
points[p2][2],
points[p3][0],
points[p3][1],
points[p3][2])
def generate_sphere( cx, cy, cz, r, step ):
points = []
num_steps = int(1/step+0.1)
rot_start = 0
rot_stop = num_steps
circ_start = 0
circ_stop = num_steps
for rotation in range(rot_start, rot_stop):
rot = step * rotation
for circle in range(circ_start, circ_stop+1):
circ = step * circle
x = r * math.cos(math.pi * circ) + cx
y = r * math.sin(math.pi * circ) * math.cos(2*math.pi * rot) + cy
z = r * math.sin(math.pi * circ) * math.sin(2*math.pi * rot) + cz
points.append([x, y, z])
#print 'rotation: %d\tcircle%d'%(rotation, circle)
return points
def add_torus( edges, cx, cy, cz, r0, r1, step ):
points = generate_torus(cx, cy, cz, r0, r1, step)
num_steps = int(1/step+0.1)
lat_start = 0
lat_stop = num_steps
longt_start = 0
longt_stop = num_steps
for lat in range(lat_start, lat_stop):
for longt in range(longt_start, longt_stop):
p0 = lat * (num_steps) + longt;
if (longt == num_steps - 1):
p1 = p0 - longt;
else:
p1 = p0 + 1;
p2 = (p1 + num_steps) % (num_steps * num_steps);
p3 = (p0 + num_steps) % (num_steps * num_steps);
add_polygon(edges,
points[p0][0],
points[p0][1],
points[p0][2],
points[p3][0],
points[p3][1],
points[p3][2],
points[p2][0],
points[p2][1],
points[p2][2] )
add_polygon(edges,
points[p0][0],
points[p0][1],
points[p0][2],
points[p2][0],
points[p2][1],
points[p2][2],
points[p1][0],
points[p1][1],
points[p1][2] )
def generate_torus( cx, cy, cz, r0, r1, step ):
points = []
num_steps = int(1/step+0.1)
rot_start = 0
rot_stop = num_steps
circ_start = 0
circ_stop = num_steps
print num_steps
for rotation in range(rot_start, rot_stop):
rot = step * rotation
for circle in range(circ_start, circ_stop):
circ = step * circle
x = math.cos(2*math.pi * rot) * (r0 * math.cos(2*math.pi * circ) + r1) + cx;
y = r0 * math.sin(2*math.pi * circ) + cy;
z = -1*math.sin(2*math.pi * rot) * (r0 * math.cos(2*math.pi * circ) + r1) + cz;
points.append([x, y, z])
return points
def add_circle( points, cx, cy, cz, r, step ):
x0 = r + cx
y0 = cy
t = step
while t <= 1.00001:
x1 = r * math.cos(2*math.pi * t) + cx;
y1 = r * math.sin(2*math.pi * t) + cy;
add_edge(points, x0, y0, cz, x1, y1, cz)
x0 = x1
y0 = y1
t+= step
def add_curve( points, x0, y0, x1, y1, x2, y2, x3, y3, step, curve_type ):
xcoefs = generate_curve_coefs(x0, x1, x2, x3, curve_type)[0]
ycoefs = generate_curve_coefs(y0, y1, y2, y3, curve_type)[0]
t = step
while t <= 1.00001:
x = xcoefs[0] * t*t*t + xcoefs[1] * t*t + xcoefs[2] * t + xcoefs[3]
y = ycoefs[0] * t*t*t + ycoefs[1] * t*t + ycoefs[2] * t + ycoefs[3]
add_edge(points, x0, y0, 0, x, y, 0)
x0 = x
y0 = y
t+= step
def draw_lines( matrix, screen, color ):
if len(matrix) < 2:
print 'Need at least 2 points to draw'
return
point = 0
while point < len(matrix) - 1:
draw_line( int(matrix[point][0]),
int(matrix[point][1]),
int(matrix[point+1][0]),
int(matrix[point+1][1]),
screen, color)
point+= 2
def add_edge( matrix, x0, y0, z0, x1, y1, z1 ):
add_point(matrix, x0, y0, z0)
add_point(matrix, x1, y1, z1)
def add_point( matrix, x, y, z=0 ):
matrix.append( [x, y, z, 1] )
def draw_line( x0, y0, x1, y1, screen, color ):
#swap points if going right -> left
if x0 > x1:
xt = x0
yt = y0
x0 = x1
y0 = y1
x1 = xt
y1 = yt
x = x0
y = y0
A = 2 * (y1 - y0)
B = -2 * (x1 - x0)
#octants 1 and 8
if ( abs(x1-x0) >= abs(y1 - y0) ):
#octant 1
if A > 0:
d = A + B/2
while x < x1:
plot(screen, color, x, y)
if d > 0:
y+= 1
d+= B
x+= 1
d+= A
#end octant 1 while
plot(screen, color, x1, y1)
#end octant 1
#octant 8
else:
d = A - B/2
while x < x1:
plot(screen, color, x, y)
if d < 0:
y-= 1
d-= B
x+= 1
d+= A
#end octant 8 while
plot(screen, color, x1, y1)
#end octant 8
#end octants 1 and 8
#octants 2 and 7
else:
#octant 2
if A > 0:
d = A/2 + B
while y < y1:
plot(screen, color, x, y)
if d < 0:
x+= 1
d+= A
y+= 1
d+= B
#end octant 2 while
plot(screen, color, x1, y1)
#end octant 2
#octant 7
else:
d = A/2 - B;
while y > y1:
plot(screen, color, x, y)
if d > 0:
x+= 1
d+= A
y-= 1
d-= B
#end octant 7 while
plot(screen, color, x1, y1)
#end octant 7
#end octants 2 and 7
#end draw_line