1use num::{Num, NumCast};
4
5#[derive(Default, Clone, Copy, Debug)]
6pub struct Rect<N: Num + NumCast + Copy + PartialOrd> {
7 pub x: N,
8 pub y: N,
9 pub w: N,
10 pub h: N,
11}
12
13#[inline]
14fn _cast<N: NumCast>(value: f32) -> N {
15 num::cast::<f32, N>(value).unwrap()
16}
17
18impl<N: Num + NumCast + Copy + PartialOrd> Rect<N> {
19 pub fn test(&self, pos_x: N, pos_y: N) -> bool {
20 pos_x >= self.x && pos_x <= self.x + self.w && pos_y >= self.y && pos_y <= self.y + self.h
21 }
22
23 pub fn adjusted_by(&self, dx: N, dy: N, dw: N, dh: N) -> Rect<N> {
24 Rect {
25 x: self.x + dx,
26 y: self.y + dy,
27 w: self.w + dw,
28 h: self.h + dh
29 }
30 }
31
32 pub fn retracted_by(&self, retract_x: N, retract_y: N) -> Rect<N> {
33 self.adjusted_by(retract_x, retract_y, retract_x * _cast(-2.0), retract_y * _cast(-2.0))
34 }
35
36 pub fn expanded_by(&self, expand_x: N, expand_y: N) -> Rect<N> {
37 self.adjusted_by(expand_x * _cast(-1.0), expand_x * _cast(-1.0), expand_y * _cast(2.0), expand_y * _cast(2.0))
38 }
39
40 pub fn position(&self) -> cgmath::Point2<N> {
41 cgmath::Point2::new(self.x, self.y)
42 }
43
44 pub fn size(&self) -> cgmath::Vector2<N> {
45 cgmath::Vector2::new(self.x, self.y)
46 }
47}