#[repr(C)]pub struct Rect {
pub min: Pos2,
pub max: Pos2,
}
Expand description
A rectangular region of space.
Usually a Rect
has a positive (or zero) size,
and then Self::min
<=
Self::max
.
In these cases Self::min
is the left-top corner
and Self::max
is the right-bottom corner.
A rectangle is allowed to have a negative size, which happens when the order
of min
and max
are swapped. These are usually a sign of an error.
Normally the unit is points (logical pixels) in screen space coordinates.
Rect
does NOT implement Default
, because there is no obvious default value.
Rect::ZERO
may seem reasonable, but when used as a bounding box, Rect::NOTHING
is a better default - so be explicit instead!
Fields§
§min: Pos2
One of the corners of the rectangle, usually the left top one.
max: Pos2
The other corner, opposing Self::min
. Usually the right bottom one.
Implementations§
source§impl Rect
impl Rect
sourcepub const EVERYTHING: Self = _
pub const EVERYTHING: Self = _
Infinite rectangle that contains every point.
sourcepub const NOTHING: Self = _
pub const NOTHING: Self = _
The inverse of Self::EVERYTHING
: stretches from positive infinity to negative infinity.
Contains no points.
This is useful as the seed for bounding boxes.
§Example:
let mut rect = Rect::NOTHING;
assert!(rect.size() == Vec2::splat(-f32::INFINITY));
assert!(rect.contains(pos2(0.0, 0.0)) == false);
rect.extend_with(pos2(2.0, 1.0));
rect.extend_with(pos2(0.0, 3.0));
assert_eq!(rect, Rect::from_min_max(pos2(0.0, 1.0), pos2(2.0, 3.0)))
pub const fn from_min_max(min: Pos2, max: Pos2) -> Self
sourcepub fn from_min_size(min: Pos2, size: Vec2) -> Self
pub fn from_min_size(min: Pos2, size: Vec2) -> Self
left-top corner plus a size (stretching right-down).
pub fn from_center_size(center: Pos2, size: Vec2) -> Self
pub fn from_x_y_ranges( x_range: impl Into<Rangef>, y_range: impl Into<Rangef>, ) -> Self
sourcepub fn from_two_pos(a: Pos2, b: Pos2) -> Self
pub fn from_two_pos(a: Pos2, b: Pos2) -> Self
Returns the bounding rectangle of the two points.
sourcepub fn from_points(points: &[Pos2]) -> Self
pub fn from_points(points: &[Pos2]) -> Self
Bounding-box around the points.
sourcepub fn everything_right_of(left_x: f32) -> Self
pub fn everything_right_of(left_x: f32) -> Self
A Rect
that contains every point to the right of the given X coordinate.
sourcepub fn everything_left_of(right_x: f32) -> Self
pub fn everything_left_of(right_x: f32) -> Self
A Rect
that contains every point to the left of the given X coordinate.
sourcepub fn everything_below(top_y: f32) -> Self
pub fn everything_below(top_y: f32) -> Self
A Rect
that contains every point below a certain y coordinate
sourcepub fn everything_above(bottom_y: f32) -> Self
pub fn everything_above(bottom_y: f32) -> Self
A Rect
that contains every point above a certain y coordinate
pub fn with_min_x(self, min_x: f32) -> Self
pub fn with_min_y(self, min_y: f32) -> Self
pub fn with_max_x(self, max_x: f32) -> Self
pub fn with_max_y(self, max_y: f32) -> Self
sourcepub fn expand(self, amnt: f32) -> Self
pub fn expand(self, amnt: f32) -> Self
Expand by this much in each direction, keeping the center
sourcepub fn expand2(self, amnt: Vec2) -> Self
pub fn expand2(self, amnt: Vec2) -> Self
Expand by this much in each direction, keeping the center
sourcepub fn scale_from_center(self, scale_factor: f32) -> Self
pub fn scale_from_center(self, scale_factor: f32) -> Self
Scale up by this factor in each direction, keeping the center
sourcepub fn scale_from_center2(self, scale_factor: Vec2) -> Self
pub fn scale_from_center2(self, scale_factor: Vec2) -> Self
Scale up by this factor in each direction, keeping the center
sourcepub fn shrink(self, amnt: f32) -> Self
pub fn shrink(self, amnt: f32) -> Self
Shrink by this much in each direction, keeping the center
sourcepub fn shrink2(self, amnt: Vec2) -> Self
pub fn shrink2(self, amnt: Vec2) -> Self
Shrink by this much in each direction, keeping the center
pub fn translate(self, amnt: Vec2) -> Self
pub fn intersects(self, other: Self) -> bool
sourcepub fn set_height(&mut self, h: f32)
pub fn set_height(&mut self, h: f32)
keep min
sourcepub fn set_center(&mut self, center: Pos2)
pub fn set_center(&mut self, center: Pos2)
Keep size
pub fn contains(&self, p: Pos2) -> bool
pub fn contains_rect(&self, other: Self) -> bool
sourcepub fn clamp(&self, p: Pos2) -> Pos2
pub fn clamp(&self, p: Pos2) -> Pos2
Return the given points clamped to be inside the rectangle
Panics if Self::is_negative
.
pub fn extend_with(&mut self, p: Pos2)
sourcepub fn extend_with_x(&mut self, x: f32)
pub fn extend_with_x(&mut self, x: f32)
Expand to include the given x coordinate
sourcepub fn extend_with_y(&mut self, y: f32)
pub fn extend_with_y(&mut self, y: f32)
Expand to include the given y coordinate
sourcepub fn union(self, other: Self) -> Self
pub fn union(self, other: Self) -> Self
The union of two bounding rectangle, i.e. the minimum Rect
that contains both input rectangles.
sourcepub fn intersect(self, other: Self) -> Self
pub fn intersect(self, other: Self) -> Self
The intersection of two Rect
, i.e. the area covered by both.
pub fn center(&self) -> Pos2
pub fn width(&self) -> f32
pub fn height(&self) -> f32
sourcepub fn aspect_ratio(&self) -> f32
pub fn aspect_ratio(&self) -> f32
Width / height
aspect_ratio < 1
: portrait / highaspect_ratio = 1
: squareaspect_ratio > 1
: landscape / wide
sourcepub fn square_proportions(&self) -> Vec2
pub fn square_proportions(&self) -> Vec2
[2, 1]
for wide screen, and [1, 2]
for portrait, etc.
At least one dimension = 1, the other >= 1
Returns the proportions required to letter-box a square view area.
pub fn area(&self) -> f32
sourcepub fn distance_to_pos(&self, pos: Pos2) -> f32
pub fn distance_to_pos(&self, pos: Pos2) -> f32
The distance from the rect to the position.
The distance is zero when the position is in the interior of the rectangle.
Negative rectangles always return f32::INFINITY
.
sourcepub fn distance_sq_to_pos(&self, pos: Pos2) -> f32
pub fn distance_sq_to_pos(&self, pos: Pos2) -> f32
The distance from the rect to the position, squared.
The distance is zero when the position is in the interior of the rectangle.
Negative rectangles always return f32::INFINITY
.
sourcepub fn signed_distance_to_pos(&self, pos: Pos2) -> f32
pub fn signed_distance_to_pos(&self, pos: Pos2) -> f32
Signed distance to the edge of the box.
Negative inside the box.
Negative rectangles always return f32::INFINITY
.
let rect = Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0));
assert_eq!(rect.signed_distance_to_pos(pos2(0.50, 0.50)), -0.50);
assert_eq!(rect.signed_distance_to_pos(pos2(0.75, 0.50)), -0.25);
assert_eq!(rect.signed_distance_to_pos(pos2(1.50, 0.50)), 0.50);
sourcepub fn lerp_inside(&self, t: Vec2) -> Pos2
pub fn lerp_inside(&self, t: Vec2) -> Pos2
sourcepub fn lerp_towards(&self, other: &Self, t: f32) -> Self
pub fn lerp_towards(&self, other: &Self, t: f32) -> Self
Linearly self towards other rect.
pub fn x_range(&self) -> Rangef
pub fn y_range(&self) -> Rangef
pub fn bottom_up_range(&self) -> Rangef
sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
width < 0 || height < 0
sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
width > 0 && height > 0
source§impl Rect
impl Rect
§Convenience functions (assumes origin is towards left top):
sourcepub fn bottom_mut(&mut self) -> &mut f32
pub fn bottom_mut(&mut self) -> &mut f32
max.y
sourcepub fn set_bottom(&mut self, y: f32)
pub fn set_bottom(&mut self, y: f32)
max.y
pub fn left_top(&self) -> Pos2
pub fn center_top(&self) -> Pos2
pub fn right_top(&self) -> Pos2
pub fn left_center(&self) -> Pos2
pub fn right_center(&self) -> Pos2
pub fn left_bottom(&self) -> Pos2
pub fn center_bottom(&self) -> Pos2
pub fn right_bottom(&self) -> Pos2
sourcepub fn split_left_right_at_fraction(&self, t: f32) -> (Self, Self)
pub fn split_left_right_at_fraction(&self, t: f32) -> (Self, Self)
Split rectangle in left and right halves. t
is expected to be in the (0,1) range.
sourcepub fn split_left_right_at_x(&self, split_x: f32) -> (Self, Self)
pub fn split_left_right_at_x(&self, split_x: f32) -> (Self, Self)
Split rectangle in left and right halves at the given x
coordinate.
sourcepub fn split_top_bottom_at_fraction(&self, t: f32) -> (Self, Self)
pub fn split_top_bottom_at_fraction(&self, t: f32) -> (Self, Self)
Split rectangle in top and bottom halves. t
is expected to be in the (0,1) range.
sourcepub fn split_top_bottom_at_y(&self, split_y: f32) -> (Self, Self)
pub fn split_top_bottom_at_y(&self, split_y: f32) -> (Self, Self)
Split rectangle in top and bottom halves at the given y
coordinate.
Trait Implementations§
source§impl Mul<Rect> for TSTransform
impl Mul<Rect> for TSTransform
Transforms the rectangle.
source§impl PartialEq for Rect
impl PartialEq for Rect
impl Copy for Rect
impl Eq for Rect
impl Pod for Rect
impl StructuralPartialEq for Rect
Auto Trait Implementations§
impl Freeze for Rect
impl RefUnwindSafe for Rect
impl Send for Rect
impl Sync for Rect
impl Unpin for Rect
impl UnwindSafe for Rect
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
§type Bits = T
type Bits = T
Self
must have the same layout as the specified Bits
except for
the possible invalid bit patterns being checked during
is_valid_bit_pattern
.source§fn is_valid_bit_pattern(_bits: &T) -> bool
fn is_valid_bit_pattern(_bits: &T) -> bool
bits
as &Self
.