appy/types/
rect.rs

1//use std::ops::{Add,Neg};
2
3/// Holds information about a rectangle.
4#[derive(Clone)]
5pub struct Rect {
6    pub x: f32,
7    pub y: f32,
8    pub w: f32,
9    pub h: f32,
10}
11
12impl Rect {
13    pub fn abs(&self, x: f32, y: f32, w: f32, h: f32) -> Rect {
14        Self {
15            x: self.x + x,
16            y: self.y + y,
17            w,
18            h,
19        }
20    }
21
22    pub fn contains(&self, x: f32, y: f32) -> bool {
23        if x >= self.x && y >= self.y && x < self.x + self.w && y < self.y + self.h {
24            return true;
25        }
26
27        false
28    }
29
30    pub fn vflip(self: Self) -> Rect {
31        Self {
32            x: self.x,
33            y: self.y + self.h,
34            w: self.w,
35            h: -self.h,
36        }
37    }
38
39    pub fn hflip(self: Self) -> Rect {
40        Self {
41            x: self.x + self.w,
42            y: self.y,
43            w: -self.w,
44            h: self.h,
45        }
46    }
47
48    pub fn hvflip(self: Self) -> Rect {
49        self.vflip().hflip()
50    }
51
52    pub fn edge(self: Self, edge: u32, size: f32) -> Self {
53        match edge {
54            0 => Self {
55                x: self.x,
56                w: self.w,
57                y: self.y,
58                h: size,
59            },
60            1 => Self {
61                x: self.x + self.w,
62                w: -size,
63                y: self.y,
64                h: self.h,
65            },
66            2 => Self {
67                x: self.x,
68                w: self.w,
69                y: self.y + self.h,
70                h: -size,
71            },
72            3 => Self {
73                x: self.x,
74                w: size,
75                y: self.y,
76                h: self.h,
77            },
78            _ => {
79                panic!("unknown edge")
80            }
81        }
82    }
83
84    /*pub fn empty()->Self {
85        Self {x:0 as T, y:0 as T, w:0 as T, h:0 as T}
86    }*/
87}