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
type Point = geo::Coordinate<f64>;
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum EdgeError {
VerticesOverlap,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Edge {
pub current: Point,
pub next: Point,
}
impl Edge {
pub fn new(current: &Point, next: &Point) -> Self {
Self {
current: *current,
next: *next,
}
}
pub fn new_with_offset(current: &Point, next: &Point, dx: f64, dy: f64) -> Self {
Self {
current: (current.x + dx, current.y + dy).into(),
next: (next.x + dx, next.y + dy).into(),
}
}
pub fn inwards_normal(&self) -> Result<Point, EdgeError> {
let dx = self.next.x - self.current.x;
let dy = self.next.y - self.current.y;
let edge_length = (dx * dx + dy * dy).sqrt();
let x = -dy / edge_length;
let y = dx / edge_length;
if x.is_finite() && y.is_finite() {
Ok((x, y).into())
} else {
Err(EdgeError::VerticesOverlap)
}
}
pub fn outwards_normal(&self) -> Result<Point, EdgeError> {
let inwards = self.inwards_normal()?;
Ok((-inwards.x, -inwards.y).into())
}
pub fn with_offset(&self, dx: f64, dy: f64) -> Self {
Self::new_with_offset(&self.current, &self.next, dx, dy)
}
pub fn inverse_with_offset(&self, dx: f64, dy: f64) -> Self {
Self::new_with_offset(&self.next, &self.current, dx, dy)
}
pub fn inverse(&self) -> Self {
Self::new(&self.next, &self.current)
}
}