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
use crate::{
primitives::{Arc, Line},
BoundingBox,
};
use euclid::{Angle, Point2D};
pub trait Bounded<S> {
fn bounding_box(&self) -> BoundingBox<S>;
}
impl<'a, S, B: Bounded<S> + ?Sized> Bounded<S> for &'a B {
fn bounding_box(&self) -> BoundingBox<S> { (*self).bounding_box() }
}
impl<S> Bounded<S> for BoundingBox<S> {
fn bounding_box(&self) -> BoundingBox<S> { *self }
}
impl<S> Bounded<S> for Point2D<f64, S> {
fn bounding_box(&self) -> BoundingBox<S> { BoundingBox::new(*self, *self) }
}
impl<S> Bounded<S> for Line<S> {
fn bounding_box(&self) -> BoundingBox<S> {
BoundingBox::new(self.start, self.end)
}
}
impl<S> Bounded<S> for Arc<S> {
fn bounding_box(&self) -> BoundingBox<S> {
let (x, y) = self.centre().to_tuple();
let r = self.radius();
let mut bounds = BoundingBox::new(self.start(), self.end());
if self.contains_angle(Angle::zero()) {
let right = Point2D::new(x + r, y);
bounds = BoundingBox::new(bounds.bottom_left(), right);
}
if self.contains_angle(Angle::frac_pi_2()) {
let top = Point2D::new(x, y + r);
bounds = BoundingBox::new(bounds.bottom_left(), top);
}
if self.contains_angle(Angle::pi()) {
let left = Point2D::new(x - r, y);
bounds = BoundingBox::new(bounds.top_right(), left);
}
if self.contains_angle(Angle::pi() + Angle::frac_pi_2()) {
let bottom = Point2D::new(x, y - r);
bounds = BoundingBox::new(bounds.top_right(), bottom);
}
bounds
}
}
#[cfg(test)]
mod tests {
use super::*;
use euclid::default::{Length, Point2D};
#[test]
fn bounding_box_around_line() {
let start = Point2D::<f64>::zero();
let end = Point2D::<f64>::new(3.0, 4.0);
let line = Line::new(start, end);
let bounds = line.bounding_box();
assert_eq!(bounds.width(), Length::new(3.0));
assert_eq!(bounds.height(), Length::new(4.0));
assert_eq!(bounds.bottom_left(), start);
assert_eq!(bounds.top_right(), end);
}
}