Skip to content
Snippets Groups Projects
Commit 9c4e8021 authored by Raphael's avatar Raphael
Browse files

implementation of bresenham

parent fe5d61db
No related branches found
No related tags found
3 merge requests!12version 0.2a,!10Resolve "Image creation bugs with 0 size windows",!9Resolve "Creation of images from AIS"
import unittest
from skais.utils.geometry import bresenham
class TestGeometry(unittest.TestCase):
def test_bresenham(self):
result = bresenham(3, 4, 16, 9)
expected = [(3, 4), (4, 4), (5, 5), (6, 5), (7, 6), (8, 6), (9, 6), (10, 7), (11, 7), (12, 7), (13, 8), (14, 8),
(15, 9), (16, 9)]
self.assertListEqual(result, expected)
def test_bresenham_inverted(self):
result = bresenham(16, 9, 3, 4)
expected = [(3, 4), (4, 4), (5, 5), (6, 5), (7, 6), (8, 6), (9, 6), (10, 7), (11, 7), (12, 7), (13, 8),
(14, 8), (15, 9), (16, 9)]
self.assertListEqual(result, expected)
def test_bresenham_same_line(self):
result = bresenham(3, 4, 10, 4)
expected = [(3, 4), (4, 4), (5, 4), (6, 4), (7, 4), (8, 4), (9, 4), (10, 4)]
self.assertListEqual(result, expected)
def test_bresenham_same_column(self):
result = bresenham(3, 4, 3, 10)
expected = [(3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10)]
self.assertListEqual(result, expected)
if __name__ == '__main__':
unittest.main()
def bresenham(x1, y1, x2, y2):
if x1 > x2:
tmp = x2
x2 = x1
x1 = tmp
if y1 > y2:
tmp = y2
y2 = y1
y1 = tmp
pixels = [(x1, y1)]
if x1 == x2:
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)
y = y1
for x in range(x1+1, x2+1):
if p < 0:
p += a
else:
y += 1
p += b
pixels.append((x, y))
return pixels
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment