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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! Lines.

use std::ops::{Mul, Range};

use arrayvec::ArrayVec;

use crate::{Affine, ParamCurve, ParamCurveArea, ParamCurveArclen, ParamCurveCurvature,
    ParamCurveDeriv, ParamCurveExtrema, ParamCurveNearest, Vec2};
use crate::MAX_EXTREMA;

/// A single line.
#[derive(Clone, Copy)]
pub struct Line {
    pub p0: Vec2,
    pub p1: Vec2,
}

impl Line {
    pub fn new<V: Into<Vec2>>(p0: V, p1: V) -> Line {
        Line { p0: p0.into(), p1: p1.into() }
    }
}

impl ParamCurve for Line {
    fn eval(&self, t: f64) -> Vec2 {
        self.p0.lerp(self.p1, t)
    }

    fn start(&self) -> Vec2 {
        self.p0
    }

    fn end(&self) -> Vec2 {
        self.p1
    }

    fn subsegment(&self, range: Range<f64>) -> Line {
        Line { p0: self.eval(range.start), p1: self.eval(range.end) }
    }
}

impl ParamCurveDeriv for Line {
    type DerivResult = ConstVec2;

    fn deriv(&self) -> ConstVec2 {
        ConstVec2(self.p1 - self.p0)
    }
}

impl ParamCurveArclen for Line {
    fn arclen(&self, _accuracy: f64) -> f64 {
        (self.p1 - self.p0).hypot()
    }
}

impl ParamCurveArea for Line {
    fn signed_area(&self) -> f64 {
        self.p0.cross(self.p1) * 0.5
    }
}

impl ParamCurveNearest for Line {
    fn nearest(&self, p: Vec2, _accuracy: f64) -> (f64, f64) {
        let d = self.p1 - self.p0;
        let dotp = d.dot(p - self.p0);
        let d_squared = d.dot(d);
        if dotp <= 0.0 {
            (0.0, (p - self.p0).hypot2())
        } else if dotp >= d_squared {
            (1.0, (p - self.p1).hypot2())
        } else {
            let t = dotp / d_squared;
            let dist = (p - self.eval(t)).hypot2();
            (t, dist)
        }
    }
}

impl ParamCurveCurvature for Line {
    fn curvature(&self, _t: f64) -> f64 {
        0.0
    }
}

impl ParamCurveExtrema for Line {
    fn extrema(&self) -> ArrayVec<[f64; MAX_EXTREMA]> {
        ArrayVec::new()
    }
}

/// A trivial "curve" that is just a constant.
#[derive(Clone, Copy)]
pub struct ConstVec2(Vec2);

impl ParamCurve for ConstVec2 {
    fn eval(&self, _t: f64) -> Vec2 {
        self.0
    }

    fn subsegment(&self, _range: Range<f64>) -> ConstVec2 {
        *self
    }
}

impl ParamCurveDeriv for ConstVec2 {
    type DerivResult = ConstVec2;

    fn deriv(&self) -> ConstVec2 {
        ConstVec2(Vec2::new(0.0, 0.0))
    }
}

impl ParamCurveArclen for ConstVec2 {
    fn arclen(&self, _accuracy: f64) -> f64 {
        0.0
    }
}

impl Mul<Line> for Affine {
    type Output = Line;

    fn mul(self, other: Line) -> Line {
        Line { p0: self * other.p0, p1: self * other.p1 }
    }
}

#[cfg(test)]
mod tests {
    use crate::{Line, ParamCurveArclen};

    #[test]
    fn line_arclen() {
        let l = Line::new((0.0, 0.0), (1.0, 1.0));
        let true_len = 2.0f64.sqrt();
        let epsilon = 1e-9;
        assert!(l.arclen(epsilon) - true_len < epsilon);

        let t = l.inv_arclen(true_len / 3.0, epsilon);
        assert!((t - 1.0 / 3.0).abs() < epsilon);
        //println!("{}", t);
    }
}