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
use crate::{
geometry::{Point, PointExt},
primitives::{
common::{LineSide, LinearEquation},
Line,
},
};
#[derive(Copy, Clone, Debug)]
pub enum Intersection {
Point {
point: Point,
outer_side: LineSide,
},
Colinear,
}
#[derive(Debug, Copy, Clone)]
pub struct IntersectionParams<'a> {
line1: &'a Line,
line2: &'a Line,
le1: LinearEquation,
le2: LinearEquation,
denominator: i32,
}
impl<'a> IntersectionParams<'a> {
pub fn from_lines(line1: &'a Line, line2: &'a Line) -> Self {
let le1 = LinearEquation::from_line(line1);
let le2 = LinearEquation::from_line(line2);
let denominator = le1.normal_vector.determinant(le2.normal_vector);
Self {
line1,
line2,
le1,
le2,
denominator,
}
}
pub fn nearly_colinear_has_error(&self) -> bool {
self.denominator.pow(2) < self.line1.delta().dot_product(self.line2.delta())
}
pub fn intersection(&self) -> Intersection {
let Self {
denominator,
le1: line1,
le2: line2,
..
} = *self;
if denominator == 0 {
return Intersection::Colinear;
}
let outer_side = if denominator > 0 {
LineSide::Right
} else {
LineSide::Left
};
let offset = denominator.abs() / 2;
let origin_distances = Point::new(line1.origin_distance, line2.origin_distance);
let numerator =
origin_distances.determinant(Point::new(line1.normal_vector.y, line2.normal_vector.y));
let x_numerator = if numerator < 0 {
numerator - offset
} else {
numerator + offset
};
let numerator =
Point::new(line1.normal_vector.x, line2.normal_vector.x).determinant(origin_distances);
let y_numerator = if numerator < 0 {
numerator - offset
} else {
numerator + offset
};
Intersection::Point {
point: Point::new(x_numerator, y_numerator) / denominator,
outer_side,
}
}
}