File size: 1,848 Bytes
b516e00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import math
import pytest

from naramotion.path import Path
from naramotion.motion import trim_path, PathFollower, PathMorph


def test_trim_path_line():
    p = Path()
    p.move_to(0, 0)
    p.line_to(100, 0)

    # Trim 0.0 -> 0.5 (halfway line)
    trimmed = trim_path(p, 0.0, 0.5)
    assert not trimmed.is_empty()
    verts = trimmed.get_vertices()
    assert len(verts) >= 2
    assert math.isclose(verts[-1][1], 50.0, abs_tol=1.0)


def test_trim_path_circle_reveal():
    p = Path()
    p.add_circle(100, 100, 50)

    # 3/4 circle reveal
    trimmed = trim_path(p, 0.0, 0.75)
    assert not trimmed.is_empty()
    assert trimmed.size > 5


def test_path_follower():
    p = Path()
    p.move_to(0, 0)
    p.line_to(100, 0)
    p.line_to(100, 100)

    follower = PathFollower(p)
    # Start (0, 0)
    p0 = follower.get_position(0.0)
    assert math.isclose(p0[0], 0.0, abs_tol=1e-3)
    assert math.isclose(p0[1], 0.0, abs_tol=1e-3)

    # Midpoint (100, 0) at t=0.5
    p_mid = follower.get_position(0.5)
    assert math.isclose(p_mid[0], 100.0, abs_tol=1.0)
    assert math.isclose(p_mid[1], 0.0, abs_tol=1.0)

    # End (100, 100) at t=1.0
    p_end = follower.get_position(1.0)
    assert math.isclose(p_end[0], 100.0, abs_tol=1.0)
    assert math.isclose(p_end[1], 100.0, abs_tol=1.0)

    # Heading angle
    angle_start = follower.get_tangent_angle(0.1)
    assert math.isclose(angle_start, 0.0, abs_tol=0.1) # Heading East


def test_path_morph():
    # Square
    p1 = Path()
    p1.add_rect(0, 0, 100, 100)

    # Circle
    p2 = Path()
    p2.add_circle(50, 50, 50)

    morph = PathMorph(p1, p2, num_samples=50)

    # Midpoint shape
    p_mid = morph(0.5)
    assert isinstance(p_mid, Path)
    assert not p_mid.is_empty()
    assert p_mid.size >= 50