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
use fj_math::{Point, Segment};
pub struct HorizontalRayToTheRight<const D: usize> {
pub origin: Point<D>,
}
impl HorizontalRayToTheRight<2> {
pub fn hits_segment(
&self,
segment: impl Into<Segment<2>>,
) -> Option<RaySegmentHit> {
let [a, b] = segment.into().points();
let [lower, upper] = if a.v <= b.v { [a, b] } else { [b, a] };
let right = if a.u > b.u { a } else { b };
if self.origin.v > upper.v {
return None;
}
if self.origin.v < lower.v {
return None;
}
if self.origin.v == lower.v && lower.v == upper.v {
if self.origin.u > right.u {
return None;
}
return Some(RaySegmentHit::Parallel);
}
let pa = robust::Coord {
x: lower.u,
y: lower.v,
};
let pb = robust::Coord {
x: upper.u,
y: upper.v,
};
let pc = robust::Coord {
x: self.origin.u,
y: self.origin.v,
};
if robust::orient2d(pa, pb, pc) >= 0. {
if self.origin.v == upper.v {
return Some(RaySegmentHit::UpperVertex);
}
if self.origin.v == lower.v {
return Some(RaySegmentHit::LowerVertex);
}
return Some(RaySegmentHit::Segment);
}
None
}
}
impl<P, const D: usize> From<P> for HorizontalRayToTheRight<D>
where
P: Into<Point<D>>,
{
fn from(point: P) -> Self {
Self {
origin: point.into(),
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RaySegmentHit {
Segment,
LowerVertex,
UpperVertex,
Parallel,
}
#[cfg(test)]
mod tests {
use super::{HorizontalRayToTheRight, RaySegmentHit};
#[test]
fn hits_segment_right() {
let ray = HorizontalRayToTheRight::from([0., 2.]);
let below = [[1., 0.], [1., 1.]];
let above = [[1., 3.], [1., 4.]];
let same_level = [[1., 1.], [1., 3.]];
assert!(ray.hits_segment(below).is_none());
assert!(ray.hits_segment(above).is_none());
assert!(matches!(
ray.hits_segment(same_level),
Some(RaySegmentHit::Segment)
));
}
#[test]
fn hits_segment_left() {
let ray = HorizontalRayToTheRight::from([1., 2.]);
let same_level = [[0., 1.], [0., 3.]];
assert!(ray.hits_segment(same_level).is_none());
}
#[test]
fn hits_segment_overlapping() {
let ray = HorizontalRayToTheRight::from([1., 1.]);
let no_hit = [[0., 0.], [2., 3.]];
let hit_segment = [[0., 0.], [3., 2.]];
let hit_upper = [[0., 0.], [2., 1.]];
let hit_lower = [[0., 2.], [2., 1.]];
assert!(ray.hits_segment(no_hit).is_none());
assert!(matches!(
ray.hits_segment(hit_segment),
Some(RaySegmentHit::Segment)
));
assert!(matches!(
ray.hits_segment(hit_upper),
Some(RaySegmentHit::UpperVertex),
));
assert!(matches!(
ray.hits_segment(hit_lower),
Some(RaySegmentHit::LowerVertex),
));
}
#[test]
fn hits_segment_on_segment() {
let ray = HorizontalRayToTheRight::from([1., 1.]);
let hit_segment = [[0., 0.], [2., 2.]];
let hit_upper = [[0., 0.], [1., 1.]];
let hit_lower = [[1., 1.], [2., 2.]];
assert!(matches!(
ray.hits_segment(hit_segment),
Some(RaySegmentHit::Segment)
));
assert!(matches!(
ray.hits_segment(hit_upper),
Some(RaySegmentHit::UpperVertex),
));
assert!(matches!(
ray.hits_segment(hit_lower),
Some(RaySegmentHit::LowerVertex),
));
}
#[test]
fn hits_segment_parallel() {
let ray = HorizontalRayToTheRight::from([2., 0.]);
let left = [[0., 0.], [1., 0.]];
let overlapping = [[1., 0.], [3., 0.]];
let right = [[3., 0.], [4., 0.]];
assert!(ray.hits_segment(left).is_none());
assert!(matches!(
ray.hits_segment(overlapping),
Some(RaySegmentHit::Parallel)
));
assert!(matches!(
ray.hits_segment(right),
Some(RaySegmentHit::Parallel)
));
}
}