@@ -35,6 +35,13 @@ impl Rect2 {
35
35
Self { position, size }
36
36
}
37
37
38
+ /// Create a new `Rect2` with the first corner at `position` and the opposite corner at `end`.
39
+ #[ inline]
40
+ pub fn from_corners ( position : Vector2 , end : Vector2 ) -> Self {
41
+ // Cannot use floating point arithmetic in const functions.
42
+ Self :: new ( position, end - position)
43
+ }
44
+
38
45
/// Create a new `Rect2` from four reals representing position `(x,y)` and size `(width,height)`.
39
46
///
40
47
/// _Godot equivalent: `Rect2(float x, float y, float width, float height)`_
@@ -46,9 +53,18 @@ impl Rect2 {
46
53
}
47
54
}
48
55
49
- /// Returns a rectangle with the same geometry, with top-left corner as `position` and non-negative size .
56
+ /// Create a new `Rect2` from a `Rect2i`, using `as` for `i32` to `real` conversions .
50
57
///
51
- /// _Godot equivalent: `Rect2.abs()`_
58
+ /// _Godot equivalent: `Rect2(Rect2i from)`_
59
+ #[ inline]
60
+ pub const fn from_rect2i ( rect : Rect2i ) -> Self {
61
+ Self {
62
+ position : Vector2 :: from_vector2i ( rect. position ) ,
63
+ size : Vector2 :: from_vector2i ( rect. size ) ,
64
+ }
65
+ }
66
+
67
+ /// Returns a rectangle with the same geometry, with top-left corner as `position` and non-negative size.
52
68
#[ inline]
53
69
pub fn abs ( & self ) -> Self {
54
70
Self {
@@ -58,8 +74,6 @@ impl Rect2 {
58
74
}
59
75
60
76
/// Whether `self` covers at least the entire area of `b` (and possibly more).
61
- ///
62
- /// _Godot equivalent: `Rect2.encloses(Rect2 b)`_
63
77
#[ inline]
64
78
pub fn encloses ( & self , b : Rect2 ) -> bool {
65
79
let end = self . end ( ) ;
@@ -75,8 +89,6 @@ impl Rect2 {
75
89
///
76
90
/// Note: This method is not reliable for `Rect2` with a negative size. Use [`abs`][Self::abs]
77
91
/// to get a positive sized equivalent rectangle for expanding.
78
- ///
79
- /// _Godot equivalent: `Rect2.expand(Vector2 to)`_
80
92
#[ inline]
81
93
pub fn expand ( & self , to : Vector2 ) -> Self {
82
94
self . merge ( Rect2 :: new ( to, Vector2 :: ZERO ) )
@@ -86,8 +98,6 @@ impl Rect2 {
86
98
///
87
99
/// Note: This method is not reliable for `Rect2` with a negative size. Use [`abs`][Self::abs]
88
100
/// to get a positive sized equivalent rectangle for merging.
89
- ///
90
- /// _Godot equivalent: `Rect2.merge(Rect2 b)`_
91
101
#[ inline]
92
102
pub fn merge ( & self , b : Self ) -> Self {
93
103
let position = self . position . coord_min ( b. position ) ;
@@ -97,26 +107,18 @@ impl Rect2 {
97
107
}
98
108
99
109
/// Returns the area of the rectangle.
100
- ///
101
- /// _Godot equivalent: `Rect2.get_area()`_
102
- #[ doc( alias = "get_area" ) ]
103
110
#[ inline]
104
111
pub fn area ( & self ) -> real {
105
112
self . size . x * self . size . y
106
113
}
107
114
108
115
/// Returns the center of the Rect2, which is equal to `position + (size / 2)`.
109
- ///
110
- /// _Godot equivalent: `Rect2.get_center()`_
111
- #[ doc( alias = "get_center" ) ]
112
116
#[ inline]
113
117
pub fn center ( & self ) -> Vector2 {
114
118
self . position + ( self . size / 2.0 )
115
119
}
116
120
117
121
/// Returns a copy of the Rect2 grown by the specified `amount` on all sides.
118
- ///
119
- /// _Godot equivalent: `Rect2.grow(float amount)`_
120
122
#[ inline]
121
123
#[ must_use]
122
124
pub fn grow ( & self , amount : real ) -> Self {
@@ -127,8 +129,6 @@ impl Rect2 {
127
129
}
128
130
129
131
/// Returns a copy of the Rect2 grown by the specified amount on each side individually.
130
- ///
131
- /// _Godot equivalent: `Rect2.grow_individual(float left, float top, float right, float bottom)`_
132
132
#[ inline]
133
133
pub fn grow_individual ( & self , left : real , top : real , right : real , bottom : real ) -> Self {
134
134
Self :: from_components (
@@ -143,8 +143,6 @@ impl Rect2 {
143
143
///
144
144
/// `amount` may be negative, but care must be taken: If the resulting `size` has
145
145
/// negative components the computation may be incorrect.
146
- ///
147
- /// _Godot equivalent: `Rect2.grow_side(int side, float amount)`_
148
146
#[ inline]
149
147
pub fn grow_side ( & self , side : RectSide , amount : real ) -> Self {
150
148
match side {
@@ -156,8 +154,6 @@ impl Rect2 {
156
154
}
157
155
158
156
/// Returns `true` if the Rect2 has area, and `false` if the Rect2 is linear, empty, or has a negative size. See also `get_area`.
159
- ///
160
- /// _Godot equivalent: `Rect2.has_area()`_
161
157
#[ inline]
162
158
pub fn has_area ( & self ) -> bool {
163
159
self . size . x > 0.0 && self . size . y > 0.0
@@ -166,8 +162,6 @@ impl Rect2 {
166
162
/// Returns `true` if the Rect2 contains a point. By convention, the right and bottom edges of the Rect2 are considered exclusive, so points on these edges are not included.
167
163
///
168
164
/// Note: This method is not reliable for Rect2 with a negative size. Use `abs` to get a positive sized equivalent rectangle to check for contained points.
169
- ///
170
- /// _Godot equivalent: `Rect2.has_area()`_
171
165
#[ inline]
172
166
pub fn has_point ( & self , point : Vector2 ) -> bool {
173
167
let point = point - self . position ;
@@ -176,11 +170,9 @@ impl Rect2 {
176
170
}
177
171
178
172
/// Returns the intersection of this Rect2 and `b`. If the rectangles do not intersect, an empty Rect2 is returned.
179
- ///
180
- /// _Godot equivalent: `Rect2.intersection(Rect2 b)`_
181
173
#[ inline]
182
174
pub fn intersection ( & self , b : Self ) -> Option < Self > {
183
- if !self . intersects ( b, true ) {
175
+ if !self . intersects ( b) {
184
176
return None ;
185
177
}
186
178
@@ -194,68 +186,53 @@ impl Rect2 {
194
186
Some ( rect)
195
187
}
196
188
197
- /// Returns `true` if the Rect2 overlaps with `b` (i.e. they have at least one point in common) .
189
+ /// Checks whether two rectangles have at least one point in common.
198
190
///
199
- /// If `include_borders` is `true`, they will also be considered overlapping if their borders touch, even without intersection.
191
+ /// Also returns `true` if the rects only touch each other (share a point/edge).
192
+ /// See [`intersects_exclude_borders`][Self::intersects_exclude_borders] if you want to return `false` in that case.
200
193
///
201
- /// _Godot equivalent: `Rect2.intersects(Rect2 b, bool include_borders)`_
194
+ /// _Godot equivalent: `Rect2.intersects(Rect2 b, bool include_borders = true )`_
202
195
#[ inline]
203
- pub fn intersects ( & self , b : Self , include_borders : bool ) -> bool {
196
+ pub fn intersects ( & self , b : Self ) -> bool {
204
197
let end = self . end ( ) ;
205
198
let end_b = b. end ( ) ;
206
199
207
- if include_borders {
208
- self . position . x <= end_b. x
209
- && end. x >= b. position . x
210
- && self . position . y <= end_b. y
211
- && end. y >= b. position . y
212
- } else {
213
- self . position . x < end_b. x
214
- && end. x > b. position . x
215
- && self . position . y < end_b. y
216
- && end. y > b. position . y
217
- }
200
+ self . position . x <= end_b. x
201
+ && end. x >= b. position . x
202
+ && self . position . y <= end_b. y
203
+ && end. y >= b. position . y
218
204
}
219
205
220
- /// Returns `true` if this Rect2 is finite, by calling `@GlobalScope.is_finite` on each component .
206
+ /// Checks whether two rectangles have at least one _inner_ point in common (not on the borders) .
221
207
///
222
- /// _Godot equivalent: `Rect2.is_finite()`_
223
- pub fn is_finite ( & self ) -> bool {
224
- self . position . is_finite ( ) && self . size . is_finite ( )
225
- }
226
-
227
- /// Create a new `Rect2` from a `Rect2i`, using `as` for `i32` to `real` conversions.
208
+ /// Returns `false` if the rects only touch each other (share a point/edge).
209
+ /// See [`intersects`][Self::intersects] if you want to return `true` in that case.
228
210
///
229
- /// _Godot equivalent: `Rect2(Rect2i from )`_
211
+ /// _Godot equivalent: `Rect2.intersects(AABB b, bool include_borders = false )`_
230
212
#[ inline]
231
- pub const fn from_rect2i ( rect : Rect2i ) -> Self {
232
- Self {
233
- position : Vector2 :: from_vector2i ( rect. position ) ,
234
- size : Vector2 :: from_vector2i ( rect. size ) ,
235
- }
213
+ pub fn intersects_exclude_borders ( & self , b : Self ) -> bool {
214
+ let end = self . end ( ) ;
215
+ let end_b = b. end ( ) ;
216
+
217
+ self . position . x < end_b. x
218
+ && end. x > b. position . x
219
+ && self . position . y < end_b. y
220
+ && end. y > b. position . y
236
221
}
237
222
238
- /// Create a new `Rect2` with the first corner at `position` and the opposite corner at `end` .
223
+ /// Returns `true` if this Rect2 is finite, by calling `@GlobalScope.is_finite` on each component .
239
224
#[ inline]
240
- pub fn from_corners ( position : Vector2 , end : Vector2 ) -> Self {
241
- Self {
242
- position,
243
- size : end - position,
244
- }
225
+ pub fn is_finite ( & self ) -> bool {
226
+ self . position . is_finite ( ) && self . size . is_finite ( )
245
227
}
246
228
247
229
/// The end of the `Rect2` calculated as `position + size`.
248
- ///
249
- /// _Godot equivalent: `Rect2.size` property_
250
- #[ doc( alias = "size" ) ]
251
230
#[ inline]
252
231
pub fn end ( & self ) -> Vector2 {
253
232
self . position + self . size
254
233
}
255
234
256
235
/// Set size based on desired end-point.
257
- ///
258
- /// _Godot equivalent: `Rect2.size` property_
259
236
#[ inline]
260
237
pub fn set_end ( & mut self , end : Vector2 ) {
261
238
self . size = end - self . position
0 commit comments