File size: 2,124 Bytes
9f084b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Baseline operations — for text line baselines.

A baseline is defined by a sequence of points, typically from left to right.
"""

from __future__ import annotations

BaselinePoints = list[tuple[float, float]]


def baseline_length(points: BaselinePoints) -> float:
    """Compute the total length of a baseline polyline."""
    if len(points) < 2:
        return 0.0
    total = 0.0
    for i in range(len(points) - 1):
        dx = points[i + 1][0] - points[i][0]
        dy = points[i + 1][1] - points[i][1]
        total += (dx * dx + dy * dy) ** 0.5
    return total


def baseline_angle(points: BaselinePoints) -> float:
    """Compute the angle (in degrees) of a baseline from start to end.

    Returns 0.0 for a horizontal baseline, positive for downward slope.
    Returns 0.0 if the baseline has fewer than 2 points.
    """
    if len(points) < 2:
        return 0.0
    import math

    dx = points[-1][0] - points[0][0]
    dy = points[-1][1] - points[0][1]
    if dx == 0 and dy == 0:
        return 0.0
    return math.degrees(math.atan2(dy, dx))


def interpolate_baseline(points: BaselinePoints, t: float) -> tuple[float, float]:
    """Interpolate a point along the baseline at parameter t in [0, 1].

    t=0 returns the start, t=1 returns the end.
    For multi-segment baselines, t is proportional to total arc length.
    """
    if len(points) < 2:
        if points:
            return points[0]
        raise ValueError("Cannot interpolate an empty baseline")

    t = max(0.0, min(1.0, t))
    total_len = baseline_length(points)
    if total_len == 0:
        return points[0]

    target = t * total_len
    accumulated = 0.0
    for i in range(len(points) - 1):
        dx = points[i + 1][0] - points[i][0]
        dy = points[i + 1][1] - points[i][1]
        seg_len = (dx * dx + dy * dy) ** 0.5
        if accumulated + seg_len >= target:
            frac = (target - accumulated) / seg_len if seg_len > 0 else 0.0
            return (
                points[i][0] + frac * dx,
                points[i][1] + frac * dy,
            )
        accumulated += seg_len

    return points[-1]