41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
# tests/test_geometry.py
|
|
|
|
import pytest
|
|
from tools.geometry import Point, Line, Polygon, Coordinate
|
|
|
|
|
|
def test_coordinate_to_point():
|
|
coord = Coordinate(lat=56.2006, long=12.5553)
|
|
p1 = coord.to_Point()
|
|
assert p1.x == 56.2006
|
|
assert p1.y == 12.5553
|
|
|
|
p2 = coord.to_Point(reversed=True)
|
|
assert p2.x == 12.5553
|
|
assert p2.y == 56.2006
|
|
|
|
|
|
def test_line_distance_to_point():
|
|
# A straight line from (0,0) to (10,0)
|
|
line = Line(Point(0, 0), Point(10, 0))
|
|
|
|
# Point exactly in the middle, 5 units above
|
|
dist1 = line.distance_to_point(Point(5, 5))
|
|
assert dist1 == 5.0
|
|
|
|
# Point way past the end of the line (closest point is the end (10,0))
|
|
dist2 = line.distance_to_point(Point(13, 4))
|
|
assert dist2 == 5.0 # 3-4-5 right triangle from (10,0) to (13,4)
|
|
|
|
|
|
def test_polygon_contains():
|
|
# A simple 10x10 square
|
|
square = Polygon([[0, 0], [10, 0], [10, 10], [0, 10]])
|
|
|
|
# Inside
|
|
assert square.contains(Point(5, 5)) is True
|
|
# Outside
|
|
assert square.contains(Point(15, 5)) is False
|
|
# On the line (Edge case)
|
|
assert square.contains(Point(10, 5)) is True
|