pub struct Rect {
pub x0: f64,
pub y0: f64,
pub x1: f64,
pub y1: f64,
}
Expand description
A rectangle.
Fields§
§x0: f64
The minimum x coordinate (left edge).
y0: f64
The minimum y coordinate (top edge in y-down spaces).
x1: f64
The maximum x coordinate (right edge).
y1: f64
The maximum y coordinate (bottom edge in y-down spaces).
Implementations§
Source§impl Rect
impl Rect
Sourcepub const fn new(x0: f64, y0: f64, x1: f64, y1: f64) -> Rect
pub const fn new(x0: f64, y0: f64, x1: f64, y1: f64) -> Rect
A new rectangle from minimum and maximum coordinates.
Sourcepub fn from_points(p0: impl Into<Point>, p1: impl Into<Point>) -> Rect
pub fn from_points(p0: impl Into<Point>, p1: impl Into<Point>) -> Rect
A new rectangle from two points.
The result will have non-negative width and height.
Sourcepub fn from_origin_size(origin: impl Into<Point>, size: impl Into<Size>) -> Rect
pub fn from_origin_size(origin: impl Into<Point>, size: impl Into<Size>) -> Rect
A new rectangle from origin and size.
The result will have non-negative width and height.
Sourcepub fn from_center_size(center: impl Into<Point>, size: impl Into<Size>) -> Rect
pub fn from_center_size(center: impl Into<Point>, size: impl Into<Size>) -> Rect
A new rectangle from center and size.
Sourcepub fn with_origin(self, origin: impl Into<Point>) -> Rect
pub fn with_origin(self, origin: impl Into<Point>) -> Rect
Create a new Rect
with the same size as self
and a new origin.
Sourcepub fn with_size(self, size: impl Into<Size>) -> Rect
pub fn with_size(self, size: impl Into<Size>) -> Rect
Create a new Rect
with the same origin as self
and a new size.
Sourcepub fn height(&self) -> f64
pub fn height(&self) -> f64
The height of the rectangle.
Note: nothing forbids negative height.
Sourcepub fn origin(&self) -> Point
pub fn origin(&self) -> Point
The origin of the rectangle.
This is the top left corner in a y-down space and with non-negative width and height.
Sourcepub fn is_zero_area(&self) -> bool
pub fn is_zero_area(&self) -> bool
Whether this rectangle has zero area.
Sourcepub fn contains(&self, point: impl Into<Point>) -> bool
pub fn contains(&self, point: impl Into<Point>) -> bool
Returns true
if point
lies within self
.
Sourcepub fn abs(&self) -> Rect
pub fn abs(&self) -> Rect
Take absolute value of width and height.
The resulting rect has the same extents as the original, but is guaranteed to have non-negative width and height.
Sourcepub fn union(&self, other: Rect) -> Rect
pub fn union(&self, other: Rect) -> Rect
The smallest rectangle enclosing two rectangles.
Results are valid only if width and height are non-negative.
Sourcepub fn union_pt(&self, pt: impl Into<Point>) -> Rect
pub fn union_pt(&self, pt: impl Into<Point>) -> Rect
Compute the union with one point.
This method includes the perimeter of zero-area rectangles.
Thus, a succession of union_pt
operations on a series of
points yields their enclosing rectangle.
Results are valid only if width and height are non-negative.
Sourcepub fn intersect(&self, other: Rect) -> Rect
pub fn intersect(&self, other: Rect) -> Rect
The intersection of two rectangles.
The result is zero-area if either input has negative width or height. The result always has non-negative width and height.
If you want to determine whether two rectangles intersect, use the
overlaps
method instead.
Sourcepub fn overlaps(&self, other: Rect) -> bool
pub fn overlaps(&self, other: Rect) -> bool
Determines whether this rectangle overlaps with another in any way.
Note that the edge of the rectangle is considered to be part of itself, meaning that two rectangles that share an edge are considered to overlap.
Returns true
if the rectangles overlap, false
otherwise.
If you want to compute the intersection of two rectangles, use the
intersect
method instead.
§Examples
use kurbo::Rect;
let rect1 = Rect::new(0.0, 0.0, 10.0, 10.0);
let rect2 = Rect::new(5.0, 5.0, 15.0, 15.0);
assert!(rect1.overlaps(rect2));
let rect1 = Rect::new(0.0, 0.0, 10.0, 10.0);
let rect2 = Rect::new(10.0, 0.0, 20.0, 10.0);
assert!(rect1.overlaps(rect2));
Sourcepub fn contains_rect(&self, other: Rect) -> bool
pub fn contains_rect(&self, other: Rect) -> bool
Returns whether this rectangle contains another rectangle.
A rectangle is considered to contain another rectangle if the other rectangle is fully enclosed within the bounds of this rectangle.
§Examples
use kurbo::Rect;
let rect1 = Rect::new(0.0, 0.0, 10.0, 10.0);
let rect2 = Rect::new(2.0, 2.0, 4.0, 4.0);
assert!(rect1.contains_rect(rect2));
Two equal rectangles are considered to contain each other.
use kurbo::Rect;
let rect = Rect::new(0.0, 0.0, 10.0, 10.0);
assert!(rect.contains_rect(rect));
Sourcepub fn inflate(&self, width: f64, height: f64) -> Rect
pub fn inflate(&self, width: f64, height: f64) -> Rect
Expand a rectangle by a constant amount in both directions.
The logic simply applies the amount in each direction. If rectangle area or added dimensions are negative, this could give odd results.
Sourcepub fn ceil(self) -> Rect
pub fn ceil(self) -> Rect
Returns a new Rect
,
with each coordinate value rounded up to the nearest integer,
unless they are already an integer.
§Examples
use kurbo::Rect;
let rect = Rect::new(3.3, 3.6, 3.0, -3.1).ceil();
assert_eq!(rect.x0, 4.0);
assert_eq!(rect.y0, 4.0);
assert_eq!(rect.x1, 3.0);
assert_eq!(rect.y1, -3.0);
Sourcepub fn floor(self) -> Rect
pub fn floor(self) -> Rect
Returns a new Rect
,
with each coordinate value rounded down to the nearest integer,
unless they are already an integer.
§Examples
use kurbo::Rect;
let rect = Rect::new(3.3, 3.6, 3.0, -3.1).floor();
assert_eq!(rect.x0, 3.0);
assert_eq!(rect.y0, 3.0);
assert_eq!(rect.x1, 3.0);
assert_eq!(rect.y1, -4.0);
Sourcepub fn expand(self) -> Rect
pub fn expand(self) -> Rect
Returns a new Rect
,
with each coordinate value rounded away from the center of the Rect
to the nearest integer, unless they are already an integer.
That is to say this function will return the smallest possible Rect
with integer coordinates that is a superset of self
.
§Examples
use kurbo::Rect;
// In positive space
let rect = Rect::new(3.3, 3.6, 5.6, 4.1).expand();
assert_eq!(rect.x0, 3.0);
assert_eq!(rect.y0, 3.0);
assert_eq!(rect.x1, 6.0);
assert_eq!(rect.y1, 5.0);
// In both positive and negative space
let rect = Rect::new(-3.3, -3.6, 5.6, 4.1).expand();
assert_eq!(rect.x0, -4.0);
assert_eq!(rect.y0, -4.0);
assert_eq!(rect.x1, 6.0);
assert_eq!(rect.y1, 5.0);
// In negative space
let rect = Rect::new(-5.6, -4.1, -3.3, -3.6).expand();
assert_eq!(rect.x0, -6.0);
assert_eq!(rect.y0, -5.0);
assert_eq!(rect.x1, -3.0);
assert_eq!(rect.y1, -3.0);
// Inverse orientation
let rect = Rect::new(5.6, -3.6, 3.3, -4.1).expand();
assert_eq!(rect.x0, 6.0);
assert_eq!(rect.y0, -3.0);
assert_eq!(rect.x1, 3.0);
assert_eq!(rect.y1, -5.0);
Sourcepub fn trunc(self) -> Rect
pub fn trunc(self) -> Rect
Returns a new Rect
,
with each coordinate value rounded towards the center of the Rect
to the nearest integer, unless they are already an integer.
That is to say this function will return the biggest possible Rect
with integer coordinates that is a subset of self
.
§Examples
use kurbo::Rect;
// In positive space
let rect = Rect::new(3.3, 3.6, 5.6, 4.1).trunc();
assert_eq!(rect.x0, 4.0);
assert_eq!(rect.y0, 4.0);
assert_eq!(rect.x1, 5.0);
assert_eq!(rect.y1, 4.0);
// In both positive and negative space
let rect = Rect::new(-3.3, -3.6, 5.6, 4.1).trunc();
assert_eq!(rect.x0, -3.0);
assert_eq!(rect.y0, -3.0);
assert_eq!(rect.x1, 5.0);
assert_eq!(rect.y1, 4.0);
// In negative space
let rect = Rect::new(-5.6, -4.1, -3.3, -3.6).trunc();
assert_eq!(rect.x0, -5.0);
assert_eq!(rect.y0, -4.0);
assert_eq!(rect.x1, -4.0);
assert_eq!(rect.y1, -4.0);
// Inverse orientation
let rect = Rect::new(5.6, -3.6, 3.3, -4.1).trunc();
assert_eq!(rect.x0, 5.0);
assert_eq!(rect.y0, -4.0);
assert_eq!(rect.x1, 4.0);
assert_eq!(rect.y1, -4.0);
Sourcepub fn scale_from_origin(self, factor: f64) -> Rect
pub fn scale_from_origin(self, factor: f64) -> Rect
Scales the Rect
by factor
with respect to the origin (the point (0, 0)
).
§Examples
use kurbo::Rect;
let rect = Rect::new(2., 2., 4., 6.).scale_from_origin(2.);
assert_eq!(rect.x0, 4.);
assert_eq!(rect.x1, 8.);
Sourcepub fn to_rounded_rect(self, radii: impl Into<RoundedRectRadii>) -> RoundedRect
pub fn to_rounded_rect(self, radii: impl Into<RoundedRectRadii>) -> RoundedRect
Creates a new RoundedRect
from this Rect
and the provided
corner radius.
Sourcepub fn to_ellipse(self) -> Ellipse
pub fn to_ellipse(self) -> Ellipse
Returns the Ellipse
that is bounded by this Rect
.
Sourcepub fn aspect_ratio_width(self) -> f64
pub fn aspect_ratio_width(self) -> f64
The aspect ratio of this Rect
.
This is defined as the width divided by the height. It measures the
“squareness” of the rectangle (a value of 1
is square).
If the height is 0
, the output will be sign(self.width) * infinity
.
If the width and height are both 0
, then the output will be NaN
.
Sourcepub fn aspect_ratio(&self) -> f64
👎Deprecated since 0.12.0: You should use aspect_ratio_width
instead, as this method returns a potentially unexpected value.
pub fn aspect_ratio(&self) -> f64
aspect_ratio_width
instead, as this method returns a potentially unexpected value.The inverse of the aspect ratio of this Rect
.
Aspect ratios are usually defined as the ratio of the width to the height, but
this method incorrectly returns the ratio of height to width.
You should generally prefer aspect_ratio_width
.
If the width is 0
the output will be sign(y1 - y0) * infinity
.
If the width and height are both 0
, the result will be NaN
.
Sourcepub fn inscribed_rect_with_aspect_ratio(&self, aspect_ratio: f64) -> Rect
pub fn inscribed_rect_with_aspect_ratio(&self, aspect_ratio: f64) -> Rect
Returns the largest possible Rect
with the given aspect_ratio
that is fully contained in self
.
The aspect ratio is specified fractionally, as width / height
.
The resulting rectangle will be centered if it is smaller than this rectangle.
§Examples
let outer = Rect::new(0.0, 0.0, 10.0, 20.0);
let inner = outer.inscribed_rect_with_aspect_ratio(1.0);
// The new `Rect` is a square centered at the center of `outer`.
assert_eq!(inner, Rect::new(0.0, 5.0, 10.0, 15.0));
Sourcepub fn contained_rect_with_aspect_ratio(
&self,
inverse_aspect_ratio: f64,
) -> Rect
👎Deprecated since 0.12.0: You should use inscribed_rect_with_aspect_ratio
instead, as this method expects an unusually defined parameter.
pub fn contained_rect_with_aspect_ratio( &self, inverse_aspect_ratio: f64, ) -> Rect
inscribed_rect_with_aspect_ratio
instead, as this method expects an unusually defined parameter.Returns the largest possible Rect
with the given inverse_aspect_ratio
that is fully contained in self
.
Aspect ratios are usually defined as the ratio of the width to the height, but
this method accepts an aspect ratio specified fractionally as height / width
.
You should generally prefer
inscribed_rect_with_aspect_ratio
, which
takes a “normal” aspect ratio.
The resulting rectangle will be centered if it is smaller than this rectangle.
Sourcepub fn get_coords(self, axis: Axis) -> (f64, f64)
pub fn get_coords(self, axis: Axis) -> (f64, f64)
Get the members matching the given axis.
Sourcepub fn get_coords_mut(&mut self, axis: Axis) -> (&mut f64, &mut f64)
pub fn get_coords_mut(&mut self, axis: Axis) -> (&mut f64, &mut f64)
Get a mutable reference to the members matching the given axis.
Sourcepub fn set_coords(&mut self, axis: Axis, v0: f64, v1: f64)
pub fn set_coords(&mut self, axis: Axis, v0: f64, v1: f64)
Set the members matching the given axis to the given values.
Trait Implementations§
Source§impl Shape for Rect
impl Shape for Rect
Source§fn winding(&self, pt: Point) -> i32
fn winding(&self, pt: Point) -> i32
Note: this function is carefully designed so that if the plane is tiled with rectangles, the winding number will be nonzero for exactly one of them.
Source§type PathElementsIter<'iter> = RectPathIter
type PathElementsIter<'iter> = RectPathIter
path_elements
method.