Skip to content

Commit 42a2a77

Browse files
author
4b796c65
committed
Added bresenham
1 parent a4c6c5d commit 42a2a77

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

tdl/map.py

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -192,38 +192,45 @@ def quickFOV(x, y, callback, fov='PERMISSIVE', radius=7.5, lightWalls=True, sphe
192192
_lib.TCOD_map_delete(tcodMap)
193193
return touched
194194

195-
# def bresenham(x1, y1, x2, y2):
196-
# points = []
197-
# issteep = abs(y2-y1) > abs(x2-x1)
198-
# if issteep:
199-
# x1, y1 = y1, x1
200-
# x2, y2 = y2, x2
201-
# rev = False
202-
# if x1 > x2:
203-
# x1, x2 = x2, x1
204-
# y1, y2 = y2, y1
205-
# rev = True
206-
# deltax = x2 - x1
207-
# deltay = abs(y2-y1)
208-
# error = int(deltax / 2)
209-
# y = y1
210-
# ystep = None
211-
# if y1 < y2:
212-
# ystep = 1
213-
# else:
214-
# ystep = -1
215-
# for x in range(x1, x2 + 1):
216-
# if issteep:
217-
# points.append((y, x))
218-
# else:
219-
# points.append((x, y))
220-
# error -= deltay
221-
# if error < 0:
222-
# y += ystep
223-
# error += deltax
224-
# # Reverse the list if the coordinates were reversed
225-
# if rev:
226-
# points.reverse()
227-
# return points
195+
def bresenham(x1, y1, x2, y2):
196+
"""
197+
Iterate over points in a bresenham line.
198+
199+
Implementation hastily copied from RogueBasin.
200+
201+
@return: Returns an iterator of (x, y) points.
202+
"""
203+
points = []
204+
issteep = abs(y2-y1) > abs(x2-x1)
205+
if issteep:
206+
x1, y1 = y1, x1
207+
x2, y2 = y2, x2
208+
rev = False
209+
if x1 > x2:
210+
x1, x2 = x2, x1
211+
y1, y2 = y2, y1
212+
rev = True
213+
deltax = x2 - x1
214+
deltay = abs(y2-y1)
215+
error = int(deltax / 2)
216+
y = y1
217+
ystep = None
218+
if y1 < y2:
219+
ystep = 1
220+
else:
221+
ystep = -1
222+
for x in range(x1, x2 + 1):
223+
if issteep:
224+
points.append((y, x))
225+
else:
226+
points.append((x, y))
227+
error -= deltay
228+
if error < 0:
229+
y += ystep
230+
error += deltax
231+
# Reverse the list if the coordinates were reversed
232+
if rev:
233+
points.reverse()
234+
return iter(points) # force as iter so I can sleep at night
228235

229236
__all__ = ['AStar', 'quickFOV']

0 commit comments

Comments
 (0)