1use std::fmt;
4
5use super::{CGPoint, CGSize};
6
7#[repr(C)]
22#[derive(Debug, Clone, Copy, PartialEq)]
23pub struct CGRect {
24 pub x: f64,
25 pub y: f64,
26 pub width: f64,
27 pub height: f64,
28}
29
30impl std::hash::Hash for CGRect {
31 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
32 self.x.to_bits().hash(state);
33 self.y.to_bits().hash(state);
34 self.width.to_bits().hash(state);
35 self.height.to_bits().hash(state);
36 }
37}
38
39impl Eq for CGRect {}
40
41impl CGRect {
42 #[must_use]
53 pub const fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
54 Self {
55 x,
56 y,
57 width,
58 height,
59 }
60 }
61
62 #[must_use]
73 pub const fn zero() -> Self {
74 Self::new(0.0, 0.0, 0.0, 0.0)
75 }
76
77 #[must_use]
79 pub const fn with_origin_and_size(origin: CGPoint, size: CGSize) -> Self {
80 Self {
81 x: origin.x,
82 y: origin.y,
83 width: size.width,
84 height: size.height,
85 }
86 }
87
88 #[must_use]
90 pub const fn origin(&self) -> CGPoint {
91 CGPoint::new(self.x, self.y)
92 }
93
94 #[must_use]
96 pub const fn size(&self) -> CGSize {
97 CGSize::new(self.width, self.height)
98 }
99
100 #[must_use]
102 pub const fn center(&self) -> CGPoint {
103 CGPoint::new(self.x + self.width / 2.0, self.y + self.height / 2.0)
104 }
105
106 #[must_use]
108 pub const fn min_x(&self) -> f64 {
109 self.x
110 }
111
112 #[must_use]
114 pub const fn min_y(&self) -> f64 {
115 self.y
116 }
117
118 #[must_use]
120 pub const fn max_x(&self) -> f64 {
121 self.x + self.width
122 }
123
124 #[must_use]
126 pub const fn max_y(&self) -> f64 {
127 self.y + self.height
128 }
129
130 #[must_use]
132 pub const fn mid_x(&self) -> f64 {
133 self.x + self.width / 2.0
134 }
135
136 #[must_use]
138 pub const fn mid_y(&self) -> f64 {
139 self.y + self.height / 2.0
140 }
141
142 #[must_use]
143 pub fn is_empty(&self) -> bool {
144 self.width <= 0.0 || self.height <= 0.0
145 }
146
147 #[must_use]
149 pub const fn is_null(&self) -> bool {
150 self.x == 0.0 && self.y == 0.0 && self.width == 0.0 && self.height == 0.0
151 }
152}
153
154impl Default for CGRect {
155 fn default() -> Self {
156 Self::zero()
157 }
158}
159
160impl fmt::Display for CGRect {
161 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
162 write!(
163 f,
164 "({}, {}, {}, {})",
165 self.x, self.y, self.width, self.height
166 )
167 }
168}