1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4use use_bounds::Aabb2;
5use use_point::Point2;
6
7#[derive(Debug, Clone, Copy, PartialEq)]
9pub struct Rectangle {
10 bounds: Aabb2,
11}
12
13impl Rectangle {
14 #[must_use]
16 pub const fn from_corners(a: Point2, b: Point2) -> Self {
17 Self {
18 bounds: Aabb2::from_points(a, b),
19 }
20 }
21
22 #[must_use]
24 pub const fn from_bounds(bounds: Aabb2) -> Self {
25 Self { bounds }
26 }
27
28 #[must_use]
30 pub const fn bounds(self) -> Aabb2 {
31 self.bounds
32 }
33
34 #[must_use]
36 pub fn width(self) -> f64 {
37 self.bounds.width()
38 }
39
40 #[must_use]
42 pub fn height(self) -> f64 {
43 self.bounds.height()
44 }
45
46 #[must_use]
48 pub fn area(self) -> f64 {
49 self.bounds.area()
50 }
51
52 #[must_use]
54 pub const fn center(self) -> Point2 {
55 self.bounds.center()
56 }
57}
58
59#[cfg(test)]
60mod tests {
61 use super::Rectangle;
62 use use_point::Point2;
63
64 #[test]
65 fn computes_rectangle_measurements() {
66 let rectangle = Rectangle::from_corners(Point2::new(0.0, 0.0), Point2::new(4.0, 2.0));
67
68 assert_eq!(rectangle.width(), 4.0);
69 assert_eq!(rectangle.height(), 2.0);
70 assert_eq!(rectangle.area(), 8.0);
71 assert_eq!(rectangle.center(), Point2::new(2.0, 1.0));
72 }
73}