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
use std::ops::{Add, AddAssign, Mul, Sub, SubAssign}; use num::Zero; use crate::core::{Point_, Size_, ValidPointType, ValidSizeType}; valid_types!(ValidRectType, i32, f32, f64); #[repr(C)] #[derive(Copy, Clone, Debug, PartialEq)] pub struct Rect_<T: ValidRectType> { pub x: T, pub y: T, pub width: T, pub height: T, } impl<T: ValidRectType> Rect_<T> { pub fn new(x: T, y: T, width: T, height: T) -> Self { Self { x, y, width, height } } } impl<T: ValidRectType + ValidPointType> Rect_<T> { #[inline] pub fn tl(&self) -> Point_<T> { Point_::new(self.x, self.y) } } impl<T: ValidRectType + ValidPointType + Add<T, Output=T>> Rect_<T> { #[inline] pub fn br(&self) -> Point_<T> { Point_::new(self.x + self.width, self.y + self.height) } } impl<T: ValidRectType + ValidSizeType> Rect_<T> { #[inline] pub fn size(&self) -> Size_<T> { Size_::new(self.width, self.height) } } impl<T: ValidRectType + Mul<T, Output=T>> Rect_<T> { #[inline] pub fn area(&self) -> T { self.width * self.height } } impl<T: ValidRectType + Zero + PartialOrd> Rect_<T> { #[inline] pub fn empty(&self) -> bool { self.width <= T::zero() || self.height <= T::zero() } } impl<T: ValidRectType + ValidPointType + Add<T, Output=T> + PartialOrd> Rect_<T> { #[inline] pub fn contains(&self, pt: Point_<T>) -> bool { self.x <= pt.x && pt.x < self.x + self.width && self.y <= pt.y && pt.y < self.y + self.height } } impl<T: ValidRectType + Default> Default for Rect_<T> { fn default() -> Self { Self { x: Default::default(), y: Default::default(), width: Default::default(), height: Default::default() } } } impl<P, R> Add<Point_<P>> for Rect_<R> where P: ValidPointType, R: ValidRectType + AddAssign<P> { type Output = Rect_<R>; fn add(mut self, rhs: Point_<P>) -> Self::Output { self += rhs; self } } impl<P, R> Sub<Point_<P>> for Rect_<R> where P: ValidPointType, R: ValidRectType + SubAssign<P> { type Output = Rect_<R>; fn sub(mut self, rhs: Point_<P>) -> Self::Output { self -= rhs; self } } impl<S, R> Add<Size_<S>> for Rect_<R> where S: ValidSizeType, R: ValidRectType + AddAssign<S> { type Output = Rect_<R>; fn add(mut self, rhs: Size_<S>) -> Self::Output { self += rhs; self } } impl<S, R> Sub<Size_<S>> for Rect_<R> where S: ValidSizeType, R: ValidRectType + SubAssign<S> { type Output = Rect_<R>; fn sub(mut self, rhs: Size_<S>) -> Self::Output { self -= rhs; self } } impl<P, R> AddAssign<Point_<P>> for Rect_<R> where P: ValidPointType, R: ValidRectType + AddAssign<P> { fn add_assign(&mut self, rhs: Point_<P>) { self.x += rhs.x; self.y += rhs.y; } } impl<P, R> SubAssign<Point_<P>> for Rect_<R> where P: ValidPointType, R: ValidRectType + SubAssign<P> { fn sub_assign(&mut self, rhs: Point_<P>) { self.x -= rhs.x; self.y -= rhs.y; } } impl<S, R> AddAssign<Size_<S>> for Rect_<R> where S: ValidSizeType, R: ValidRectType + AddAssign<S> { fn add_assign(&mut self, rhs: Size_<S>) { self.width += rhs.width; self.height += rhs.height; } } impl<S, R> SubAssign<Size_<S>> for Rect_<R> where S: ValidSizeType, R: ValidRectType + SubAssign<S> { fn sub_assign(&mut self, rhs: Size_<S>) { self.width -= rhs.width; self.height -= rhs.height; } }