File size: 1,105 Bytes
19faf57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import math
from alphageometry.modules import utils


def test_polygon_area_and_centroid():
    square = [(0, 0), (1, 0), (1, 1), (0, 1)]
    assert math.isclose(utils.polygon_area(square), 1.0, rel_tol=1e-9)
    cx, cy = utils.polygon_centroid(square)
    assert math.isclose(cx, 0.5)
    assert math.isclose(cy, 0.5)


def test_convex_hull_simple():
    pts = [(0, 0), (1, 0), (0.5, 0.5), (0, 1), (1, 1)]
    hull = utils.convex_hull(pts)
    # hull should be the rectangle corners in CCW order
    assert set(hull) == {(0, 0), (1, 0), (1, 1), (0, 1)}


def test_point_in_polygon_and_on_edge():
    tri = [(0, 0), (2, 0), (1, 2)]
    assert utils.point_in_polygon((1, 1), tri)
    # point on edge
    assert utils.point_in_polygon((1, 0), tri)


def test_rdp_closed_and_safe_earclip():
    poly = [(0, 0), (2, 0), (2, 2), (1, 1.5), (0, 2), (0, 0)]
    simplified = utils.rdp_simplify_closed(poly, 0.5)
    assert len(simplified) >= 3
    tris = utils.safe_earclip_triangulate(poly[:-1])
    # triangulation should produce at least one triangle
    assert len(tris) >= 1