Skip to main content

gust_render/
rect.rs

1//
2//  Rust file | 2018
3//  Author: Alexandre Fourcat
4//  rect.rs
5//  module:
6//! Rectangle struct and all fonction
7
8use nalgebra::Scalar;
9use std::ops::{Add, Div, Mul, MulAssign, Sub};
10
11/// Rect define a rectangle with down/left coord and width/height
12#[derive(Debug, Clone, Copy, PartialEq)]
13pub struct Rect<T: Scalar> {
14    pub top: T,
15    pub left: T,
16    pub width: T,
17    pub height: T,
18}
19
20impl<T: Scalar + Add<Output = T>> Add<Rect<T>> for Rect<T> {
21    type Output = Rect<T>;
22
23    fn add(self, rhs: Rect<T>) -> Self::Output {
24        Rect {
25            top: self.top + rhs.top,
26            left: self.left + rhs.left,
27            height: self.width + rhs.width,
28            width: self.height + rhs.height,
29        }
30    }
31}
32
33impl<T: Scalar + Div<Output = T>> Div<Rect<T>> for Rect<T> {
34    type Output = Rect<T>;
35
36    fn div(self, rhs: Rect<T>) -> Self::Output {
37        Rect {
38            top: self.top / rhs.top,
39            left: self.left / rhs.left,
40            height: self.width / rhs.width,
41            width: self.height / rhs.height,
42        }
43    }
44}
45
46impl<T: Scalar + Sub<Output = T>> Sub<Rect<T>> for Rect<T> {
47    type Output = Rect<T>;
48
49    fn sub(self, rhs: Rect<T>) -> Self::Output {
50        Rect {
51            top: self.top - rhs.top,
52            left: self.left - rhs.left,
53            height: self.width - rhs.width,
54            width: self.height - rhs.height,
55        }
56    }
57}
58
59impl<T: Scalar + Mul<Output = T>> Mul<Rect<T>> for Rect<T> {
60    type Output = Rect<T>;
61
62    fn mul(self, rhs: Rect<T>) -> Self::Output {
63        Rect {
64            top: self.top * rhs.top,
65            left: self.left * rhs.left,
66            height: self.width * rhs.width,
67            width: self.height * rhs.height,
68        }
69    }
70}
71
72impl<T: Scalar + MulAssign<T>> MulAssign<Rect<T>> for Rect<T> {
73    fn mul_assign(&mut self, rhs: Rect<T>) {
74        self.left *= rhs.left;
75        self.top *= rhs.top;
76        self.width *= rhs.width;
77        self.height *= rhs.height;
78    }
79}
80
81//------------------
82//
83//              Rect impl
84//
85//------------------
86
87impl<T> Rect<T>
88where
89    T: Scalar + Add<Output = T> + PartialOrd,
90{
91    pub fn new(left: T, top: T, width: T, height: T) -> Rect<T> {
92        Rect {
93            top,
94            left,
95            width,
96            height,
97        }
98    }
99
100    pub fn contain(&self, point: super::Point<T>) -> bool {
101        point.x > self.left
102            && point.x < self.left + self.width
103            && point.y > self.top
104            && point.y < self.top + self.height
105    }
106}
107
108impl From<Rect<u32>> for Rect<f32> {
109    fn from(this: Rect<u32>) -> Rect<f32> {
110        Rect {
111            top: this.top as f32,
112            left: this.left as f32,
113            width: this.width as f32,
114            height: this.height as f32,
115        }
116    }
117}
118
119impl<T> Default for Rect<T>
120where
121    T: Default + Scalar,
122{
123    fn default() -> Rect<T> {
124        Rect {
125            top: T::default(),
126            left: T::default(),
127            width: T::default(),
128            height: T::default(),
129        }
130    }
131}