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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//! Lines.

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

use arrayvec::ArrayVec;

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

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

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

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

    #[inline]
    fn start(&self) -> Vec2 {
        self.p0
    }

    #[inline]
    fn end(&self) -> Vec2 {
        self.p1
    }

    #[inline]
    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;

    #[inline]
    fn deriv(&self) -> ConstVec2 {
        ConstVec2(self.p1 - self.p0)
    }
}

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

impl ParamCurveArea for Line {
    #[inline]
    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 {
    #[inline]
    fn curvature(&self, _t: f64) -> f64 {
        0.0
    }
}

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

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

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

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

impl ParamCurveDeriv for ConstVec2 {
    type DerivResult = ConstVec2;

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

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

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

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

/// An iterator yielding the path for a single line.
pub struct LinePathIter {
    line: Line,
    ix: usize,
}

impl Shape for Line {
    type BezPathIter = LinePathIter;

    #[inline]
    fn to_bez_path(&self, _tolerance: f64) -> LinePathIter {
        LinePathIter { line: *self, ix: 0 }
    }

    /// Returning zero here is consistent with the contract (area is
    /// only meaningful for closed shapes), but an argument can be made
    /// that the contract should be tightened to include the Green's
    /// theorem contribution.
    fn area(&self) -> f64 {
        0.0
    }

    #[inline]
    fn perimeter(&self, _accuracy: f64) -> f64 {
        (self.p1 - self.p0).hypot()
    }

    /// Same consideration as `area`.
    fn winding(&self, _pt: Vec2) -> i32 {
        0
    }

    #[inline]
    fn bounding_box(&self) -> Rect {
        Rect::from_points(self.p0, self.p1)
    }

    #[inline]
    fn as_line(&self) -> Option<Line> {
        Some(*self)
    }
}

impl Iterator for LinePathIter {
    type Item = PathEl;

    fn next(&mut self) -> Option<PathEl> {
        self.ix += 1;
        match self.ix {
            1 => Some(PathEl::Moveto(self.line.p0)),
            2 => Some(PathEl::Lineto(self.line.p1)),
            _ => None,
        }
    }
}

#[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);
    }
}