Skip to content
Snippets Groups Projects
Commit 50c2165f authored by Raphael's avatar Raphael
Browse files

slopes greater than 1 for bresenham algorithm

parent 71ab7a04
Branches
No related tags found
3 merge requests!12version 0.2a,!10Resolve "Image creation bugs with 0 size windows",!9Resolve "Creation of images from AIS"
This commit is part of merge request !9. Comments created here will be created in the context of that merge request.
...@@ -18,6 +18,13 @@ class TestGeometry(unittest.TestCase): ...@@ -18,6 +18,13 @@ class TestGeometry(unittest.TestCase):
self.assertListEqual(result, expected) self.assertListEqual(result, expected)
def test_bresenham_inverted_2(self):
result = bresenham(16, 4, 3, 9)
expected = [(3, 9), (4, 9), (5, 8), (6, 8), (7, 7), (8, 7), (9, 7), (10, 6), (11, 6), (12, 6), (13, 5), (14, 5),
(15, 4), (16, 4)]
self.assertListEqual(result, expected)
def test_bresenham_same_line(self): def test_bresenham_same_line(self):
result = bresenham(3, 4, 10, 4) result = bresenham(3, 4, 10, 4)
expected = [(3, 4), (4, 4), (5, 4), (6, 4), (7, 4), (8, 4), (9, 4), (10, 4)] expected = [(3, 4), (4, 4), (5, 4), (6, 4), (7, 4), (8, 4), (9, 4), (10, 4)]
......
...@@ -3,28 +3,44 @@ def bresenham(x1, y1, x2, y2): ...@@ -3,28 +3,44 @@ def bresenham(x1, y1, x2, y2):
tmp = x2 tmp = x2
x2 = x1 x2 = x1
x1 = tmp x1 = tmp
if y1 > y2:
tmp = y2 tmp = y2
y2 = y1 y2 = y1
y1 = tmp y1 = tmp
pixels = [(x1, y1)] dx = int(x2 - x1)
if x1 == x2: dy = int(y2 - y1)
x = x1
for y in range(y1 + 1, y2 + 1):
pixels.append((x, y))
else:
a = 2*(y2 - y1)
b = a - 2*(x2 - x1)
p = a - (x2 - x1)
sx = sy = 1
if dx < 0:
sx = -1
if dy < 0:
sy = -1
x = x1
y = y1 y = y1
pixels = [(x1, y1)]
if abs(dx) > abs(dy): # slope < 1
p = (2 * abs(dy)) - abs(dx)
for x in range(x1 + 1, x2 + 1): for x in range(x1 + 1, x2 + 1):
if p < 0: if p < 0:
p += a p += 2 * abs(dy)
else:
y += sy
p += (2 * abs(dy)) - (2 * abs(dx))
pixels.append((x, y))
else: # slope >= 1
p = (2 * abs(dx)) - abs(dy)
for y in range(y1 + 1, y2 + 1):
if p < 0:
p += 2 * abs(dx)
else: else:
y += 1 x += sx
p += b p += (2 * abs(dx)) - (2 * abs(dy))
pixels.append((x, y)) pixels.append((x, y))
return pixels return pixels
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment