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
use crate::geometry::{F64Ext, Point, Transform, Transformation};
use std::cmp::Ordering;
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
pub struct LineSegment {
pub p0: Point,
pub p1: Point,
}
impl LineSegment {
pub fn new(p0: Point, p1: Point) -> LineSegment {
LineSegment { p0, p1 }
}
pub fn compare_to_point(self, p: Point) -> Option<Ordering> {
(p - self.p0).cross(self.p1 - p).partial_cmp(&0.0)
}
pub fn intersect_with_vertical_line(self, x: f64) -> Option<Point> {
let dx = self.p1.x - self.p0.x;
if dx == 0.0 {
return None;
}
let dx1 = x - self.p0.x;
let dx2 = self.p1.x - x;
Some(Point {
x,
y: if dx1 <= dx2 {
self.p0.y.ext_lerp(self.p1.y, dx1 / dx)
} else {
self.p1.y.ext_lerp(self.p0.y, dx2 / dx)
},
})
}
}
impl Transform for LineSegment {
fn transform<T>(self, t: &T) -> LineSegment
where
T: Transformation,
{
LineSegment::new(self.p0.transform(t), self.p1.transform(t))
}
fn transform_mut<T>(&mut self, t: &T)
where
T: Transformation,
{
*self = self.transform(t);
}
}