-
Notifications
You must be signed in to change notification settings - Fork 0
/
bresenhamagain.py
351 lines (289 loc) · 9.37 KB
/
bresenhamagain.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
# Based on info from wiki and http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html
#
# He split it into octants...
#
class Coord:
x = 0;
y = 0;
def __init__(self, x,y):
self.x, self.y = x,y
def __str__(self):
return "<%d,%d>"%(self.x, self.y)
dxdy = lambda start,stop : (stop.x - start.x, stop.y - start.y)
'''
point chosen now is x,y.
x, y are int. xreal and yreal follow:
yreal = M * xreal + b
So now we have:
(x, y) => yreal-y = error
For new points we have 2 possible error values based on 2 possible choices for the new Y:
(x+1, y) => newerror = yreal-y = olderror + M
OR
(x+1, y+1) => newerror = yreal-(y+1) = olderror + M - 1
pick either based on smalles new error.
'''
pt1 = Coord(0,0)
pt2 = Coord(20,5)
def line0(start, stop, debug = False):
dx,dy = dxdy(start, stop)
M = (1.0*dy)/dx
error = { 0 : lambda olderror: olderror + M,
1 : lambda olderror: (olderror + M)-1}
y = start.y
olderror = 0
for x in range(start.x, stop.x):
if debug:
yield x, y, olderror
else:
yield x,y
if error[0](olderror) < 0.5:
olderror = error[0](olderror)
else:
olderror = error[1](olderror)
y = y+1
def line1(start, stop, debug = False):
dx,dy = dxdy(start, stop)
M = (1.0*dy)/dx
error = { 0 : lambda olderror: (olderror + M), # x,y below yreal by error+M
1 : lambda olderror: (olderror + M)-1} # x,y+1 above yreal by 1-(error+M)
y = start.y
olderror = 0
for x in range(start.x, stop.x):
if debug:
yield x, y, olderror
else:
yield x,y
if abs(error[0](olderror)) < 0.5 :
olderror = error[0](olderror)
continue
else:
olderror = error[1](olderror)
y = y+1
continue
#Snippet from M Abrash's code:
# http://www.phatcode.net/res/224/files/html/ch36/36-01.html
# void Octant0(X0, Y0, DeltaX, DeltaY, XDirection)
# unsigned int X0, Y0; /* coordinates of start of the line */
# unsigned int DeltaX, DeltaY; /* length of the line (both > 0) */
# int XDirection; /* 1 if line is drawn left to right,
# -1 if drawn right to left */
# {
# int DeltaYx2;
# int DeltaYx2MinusDeltaXx2;
# int ErrorTerm;
# /* Set up initial error term and values used inside drawing loop */
# DeltaYx2 = DeltaY * 2;
# DeltaYx2MinusDeltaXx2 = DeltaYx2 - (int) ( DeltaX * 2 );
# ErrorTerm = DeltaYx2 - (int) DeltaX;
# /* Draw the line */
# EVGADot(X0, Y0); /* draw the first pixel */
# while ( DeltaX-- ) {
# /* See if it's time to advance the Y coordinate */
# if ( ErrorTerm >= 0 ) {
# /* Advance the Y coordinate & adjust the error term
# back down */
# Y0++;
# ErrorTerm += DeltaYx2MinusDeltaXx2;
# } else {
# /* Add to the error term */
# ErrorTerm += DeltaYx2;
# }
# X0 += XDirection; /* advance the X coordinate */
# EVGADot(X0, Y0); /* draw a pixel */
# }
# }
# let me try something like this...
def oct0(x,y,dx, dy, xdir, debug=False):
M = 1.0*dy/dx # I still use floats... for now.
error = { 0 : lambda olderror: (olderror + M), # x,y below yreal by error+M
1 : lambda olderror: (olderror + M)-1} # x,y+1 above yreal by 1-(error+M)
cur_error=0
while dx > 0:
x += xdir
dx -= 1
if error[0](cur_error) < 0.5:
cur_error = error[0](cur_error)
else:
cur_error = error[1](cur_error)
y += 1
if debug:
yield x, y, cur_error
else:
yield x,y
def oct1(x,y,dx, dy,xdir, debug=False):
M = 1.0*dx/dy
error = { 0 : lambda olderror: (olderror + M), # x,y below yreal by error+M
1 : lambda olderror: (olderror + M)-1} # x,y+1 above yreal by 1-(error+M)
cur_error = 0
while dy > 0:
dy -= 1
if error[0](cur_error) < 0.5:
cur_error = error[0](cur_error)
else:
cur_error = error[1](cur_error)
x += xdir
y += 1
if debug:
yield x, y, cur_error
else:
yield x,y
def line2a(start, stop, debug=False):
dx,dy = dxdy(start, stop)
# xincr = -1 if dx < 0 else 1
if dy < 0:
x,y = stop.x, stop.y
else:
x,y = start.x, start.y
if dx ==0 or dy ==0:
return []
if dx > 0:
if dx > dy:
return oct0(x, y, dx,dy, 1, debug=debug)
else:
return oct1(x, y, dx,dy, 1, debug=debug)
else:
dx = -dx
if dx > dy:
return oct0(x, y, dx,dy, -1, debug=debug)
else:
return oct1(x, y, dx,dy, -1, debug=debug)
# if ( DeltaX > 0 ) {
# if ( DeltaX > DeltaY ) {
# Octant0(X0, Y0, DeltaX, DeltaY, 1);
# } else {
# Octant1(X0, Y0, DeltaX, DeltaY, 1);
# }
# } else {
# DeltaX = -DeltaX; /* absolute value of DeltaX */
# if ( DeltaX > DeltaY ) {
# Octant0(X0, Y0, DeltaX, DeltaY, -1);
# } else {
# Octant1(X0, Y0, DeltaX, DeltaY, -1);
# }
# }
def octant_sub_45_degrees(x,y,dx,dy, xdir, debug=False):
M = 1.0*dx/dy
error = { 0 : lambda olderror: (olderror + M), # x,y below yreal by error+M
1 : lambda olderror: (olderror + M)-1} # x,y+1 above yreal by 1-(error+M)
cur_error =0
while dx > 0:
dx -= 1
x += xdir
if error[0](cur_error) < 0.5:
cur_error = error[0](cur_error)
else:
cur_error = error[1](cur_error)
y += 1
if debug:
yield x, y, cur_error
else:
yield x,y
import pdb; pdb.set_trace()
pts = [ [Coord(20,20), Coord(40,30)],
[Coord(20,20), Coord(0,30)],]
# [Coord(20,20), Coord(40,30)],
# [Coord(20,20), Coord(40,30)]]
for p in pts:
dx,dy = dxdy(*p)
#xdir = -1 if dx < 0 else 1
if dx/dx == dy/dy:
xdir = 1
l = list( octant_sub_45_degrees(x, y, dx,dy, xdir) )
import pdb; pdb.set_trace()
def line2(start, stop, debug=False):
dx,dy = dxdy(start, stop)
# xincr = -1 if dx < 0 else 1
# if dy < 0:
# x,y = stop.x, stop.y
# else:
# x,y = start.x, start.y
# if dx ==0 or dy ==0:
# return []
# if dx > 0:
# if dx > dy:
# return oct0(x, y, dx,dy, 1, debug=debug)
# else:
# return oct1(x, y, dx,dy, 1, debug=debug)
# else:
# dx = -dx
# if dx > dy:
# return oct0(x, y, dx,dy, -1, debug=debug)
# else:
# return oct1(x, y, dx,dy, -1, debug=debug)
from math import sqrt
cases = [ [Coord(20,20), Coord(40,30)],
[Coord(20,20), Coord(40,35)],
[Coord(20,20), Coord(30,40)], # next anticlock octant
[Coord(20,20), Coord(10,40)],
[Coord(20,20), Coord(0,30)]]
swap = lambda testcasepoints: [testcasepoints[1], testcasepoints[0]]
close_enough = lambda actualpnt, calcpnt: sqrt((actualpnt.x-calcpnt.x)**2 + (actualpnt.y-calcpnt.y)**2) < 2
def test(linefunc,testcases, stopidx=-1):
for idx,testcase in enumerate(testcases):
if idx == stopidx:
break;
try:
lastelem = list(linefunc(*testcase, debug=True))[-1]
howclose = "close enough " if close_enough (testcase[-1], Coord(*lastelem[:-1])) else "too far away"
lastelem = list(lastelem) + [howclose]
except Exception,ex:
print ex
lastelem = "no luck for couple", str(testcase[0]), str(testcase[1])
dx, dy = dxdy(*testcase)
M = (1.0*dy)/dx
print testcase[0], testcase[1], M, lastelem
stopidx = 2
test(line0,cases,stopidx=stopidx)
test(line1,cases,stopidx=stopidx)
for i in line1(*cases[1], debug=True):
print i
test(line2,cases)
from math import pi, sin, cos
print "now rotate a few times"
MAX_CORNERS = 6
CORNER_ANGLE = 2*pi/MAX_CORNERS
CENTER = Coord(20,20)
RADIUS = 10
morecases = []
for corner in range(0,MAX_CORNERS):
corner = corner*CORNER_ANGLE
rcs, rsn = CENTER.x+RADIUS*cos(corner), CENTER.y+RADIUS*sin(corner)
morecases.append([CENTER,Coord(rcs, rsn)])
test(line2, morecases)
print "move by 60 degrees"
MAX_CORNERS = 6
offset = 0 # pi/6
CORNER_ANGLE = 2*pi/MAX_CORNERS
CENTER = Coord(20,20)
RADIUS = 10
morecases = []
for corner in range(0,MAX_CORNERS):
corner = corner*CORNER_ANGLE
rcs, rsn = int(round(CENTER.x+RADIUS*cos(offset+corner))), int(round(CENTER.y+RADIUS*sin(offset+corner)))
morecases.append([CENTER,Coord(rcs, rsn)])
test(line2, morecases)
i = 0
#import pdb; pdb.set_trace()
print "number",i
case = morecases[i]
print case[0],case[1],list(line2(*case,debug=True))
i+=1
print "number",i
case = morecases[i]
print case[0],case[1],list(line2(*case,debug=True))
i+=1
print "number",i
case = morecases[i]
print case[0],case[1],list(line2(*case,debug=True))
i+=1
print "number",i
case = morecases[i]
print case[0],case[1],list(line2(*case,debug=True))
i+=1
print "number",i
case = morecases[i]
print case[0],case[1],list(line2(*case,debug=True))
i+=1
print "number",i
case = morecases[i]
print case[0],case[1],list(line2(*case,debug=True))