Change `Rect::area` to return zero for negative rectangles (#7305)

Previously a single-negative rectangle (where `min.x > max.x` XOR `min.y
> max.y`) would return a negative area, while a doubly-negative
rectangle (`min.x > max.x` AND `min.y > max.y`) would return a positive
area. Now both return zero instead.
This commit is contained in:
Emil Ernerfeldt 2025-07-07 12:03:03 +02:00 committed by GitHub
parent 3622a03a46
commit 93d562221b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 2 deletions

View File

@ -1,6 +1,6 @@
use std::fmt; use std::fmt;
use crate::{Div, Mul, Pos2, Rangef, Rot2, Vec2, lerp, pos2, vec2}; use crate::{Div, Mul, NumExt as _, Pos2, Rangef, Rot2, Vec2, lerp, pos2, vec2};
/// A rectangular region of space. /// A rectangular region of space.
/// ///
@ -341,11 +341,13 @@ impl Rect {
self.max - self.min self.max - self.min
} }
/// Note: this can be negative.
#[inline(always)] #[inline(always)]
pub fn width(&self) -> f32 { pub fn width(&self) -> f32 {
self.max.x - self.min.x self.max.x - self.min.x
} }
/// Note: this can be negative.
#[inline(always)] #[inline(always)]
pub fn height(&self) -> f32 { pub fn height(&self) -> f32 {
self.max.y - self.min.y self.max.y - self.min.y
@ -373,9 +375,10 @@ impl Rect {
} }
} }
/// This is never negative, and instead returns zero for negative rectangles.
#[inline(always)] #[inline(always)]
pub fn area(&self) -> f32 { pub fn area(&self) -> f32 {
self.width() * self.height() self.width().at_least(0.0) * self.height().at_least(0.0)
} }
/// The distance from the rect to the position. /// The distance from the rect to the position.