silk.geometry.rect.
Axis-aligned rectangles stored as their minimum and maximum corners, over any number type.
When to use#
Use Rect for a bounding box, a clip region, a viewport at a position, or the border box of a
layout node. Build one from two corners with new or fromPoints, or from a position and
an extent with fromOriginSize. Use silk.geometry.size.Size2 for an extent with no
position.
Details#
A rectangle is a Copy struct with the public fields xMin, yMin, xMax, and yMax,
generic over its number type T. Edges are inclusive: contains accepts a point on an edge,
and two rectangles that share an edge have a zero-area intersection. Bound computations such
as unionWith, include, and intersection need T: Order, so they work for integers,
fixed-point types, and floats. &a == &b compares every field when T: Equal.
A valid rectangle has xMin <= xMax and yMin <= yMax. Fields are public, so a rectangle can
be built reversed; isValid tests for that and normalize fixes it.
Gotchas#
For a float type, Order follows the IEEE 754 total order: negative zero is below positive
zero, and a NaN with the sign bit clear is above every number. Test isFinite first when the
input can hold NaN.
Examples#
Grow a box and clip it#
import silk.geometry.insets { Insets }
import silk.geometry.point { Point2 }
import silk.geometry.rect { Rect }
import silk.geometry.size { Size2 }
import silk.core.option { Option }
pub fn main() -> i32 {
let card = Rect<f64>.fromOriginSize(Point2<f64>.new(10.0, 10.0), Size2<f64>.new(100.0, 50.0))
let shadow = card.inflate(Insets<f64>.uniform(4.0))
let viewport = Rect<f64>.new(0.0, 0.0, 80.0, 80.0)
let visible = match shadow.intersection(viewport) {
Option<Rect<f64>>.Some { value } => value
Option<Rect<f64>>.None => {
return 1
}
}
let corner = Point2<f64>.new(110.0, 60.0)
if visible.width() != 74.0 || visible.height() != 58.0 || !card.contains(corner) {
return 2
}
return 0
}Import as Rect with import silk.geometry.rect { Rect }.
Public declarations: 1.
Rect#
pub struct Rect<T>An axis-aligned rectangle of number type T, stored as its minimum and maximum corners.
When to use#
Use Rect for bounds, clips, and boxes at a position. Use silk.geometry.size.Size2 for an
extent alone.
Details#
The fields are public. A valid rectangle has xMin <= xMax and yMin <= yMax; a rectangle can
have zero width or height and still has a position. Edges are inclusive. &a == &b compares all
four fields when T: Equal.
Field xMin#
pub xMin: TThe minimum x coordinate: the left edge in a y-down space.
Field yMin#
pub yMin: TThe minimum y coordinate: the top edge in a y-down space.
Field xMax#
pub xMax: TThe maximum x coordinate: the right edge in a y-down space.
Field yMax#
pub yMax: TThe maximum y coordinate: the bottom edge in a y-down space.
Implementation Copy for Rect<T>#
impl<T: Copy> Copy for Rect<T>Implementation Rect<T>#
impl<T> Rect<T>Associated function new#
pub fn new(xMin: T, yMin: T, xMax: T, yMax: T) -> Rect<T>Returns the rectangle with the given edges, as written.
Gotchas#
new does not reorder its arguments. Use fromPoints when the corners can come in any
order.
Method map#
pub fn map<U>(self: Self, transform: mut fn(T) -> U) -> Rect<U>Returns a rectangle whose fields are transform applied to xMin, yMin, xMax, then
yMax.
Gotchas#
A transform that reverses order, such as negation, gives a reversed rectangle. Call
normalize on the result in that case.
Implementation Rect<T>#
impl<T: Copy> Rect<T>Method origin#
pub fn origin(self: Self) -> Point2<T>Returns the minimum corner (xMin, yMin): the top-left corner in a y-down space.
Method maxCorner#
pub fn maxCorner(self: Self) -> Point2<T>Returns the maximum corner (xMax, yMax): the bottom-right corner in a y-down space.
Method edges#
pub fn edges(self: Self) -> [T; 4]Returns the edges as the array [xMin, yMin, xMax, yMax].
Implementation Rect<T>#
impl<T: Copy + Add<T, T>> Rect<T>Associated function fromOriginSize#
pub fn fromOriginSize(origin: Point2<T>, size: Size2<T>) -> Rect<T>Returns the rectangle whose minimum corner is origin and whose extent is size.
Gotchas#
A negative width or height gives a reversed rectangle. Call normalize on the result when
the size can be negative.
Method translate#
pub fn translate(self: Self, offset: Vector2<T>) -> Rect<T>Returns the rectangle moved by offset, with the same size.
Implementation Rect<T>#
impl<T: Copy + Subtract<T, T>> Rect<T>Method width#
pub fn width(self: Self) -> TReturns xMax - xMin. A reversed rectangle gives a negative width.
Method height#
pub fn height(self: Self) -> TReturns yMax - yMin. A reversed rectangle gives a negative height.
Method size#
pub fn size(self: Self) -> Size2<T>Returns the extent (xMax - xMin, yMax - yMin).
Implementation Rect<T>#
impl<T: Copy + Add<T, T> + Subtract<T, T>> Rect<T>Method inflate#
pub fn inflate(self: Self, insets: Insets<T>) -> Rect<T>Returns the rectangle grown by insets on each side.
Details#
insets.left moves xMin down, insets.top moves yMin down, insets.right moves xMax
up, and insets.bottom moves yMax up. Negative insets shrink the rectangle.
Gotchas#
Insets that shrink by more than the size give a reversed rectangle. Test the result with
isValid when the insets can be negative.
Implementation Rect<T>#
impl<T: Copy + One + Add<T, T> + Divide<T, T>> Rect<T>Method center#
pub fn center(self: Self) -> Point2<T>Returns the point halfway between the minimum and the maximum corner.
Implementation Rect<T>#
impl<T: Copy + Order> Rect<T>Associated function fromPoints#
pub fn fromPoints(first: Point2<T>, second: Point2<T>) -> Rect<T>Returns the smallest rectangle that holds both first and second, in any order.
Method isValid#
pub fn isValid(self: &Self) -> boolReturns true when xMin <= xMax and yMin <= yMax.
Method isEmpty#
pub fn isEmpty(self: &Self) -> boolReturns true when the rectangle has no area: its width or height is zero or negative.
Details#
A zero-width or zero-height rectangle still has a position, so unionWith and
intersection use its edges. Use isEmpty to skip drawing, not to mark absent bounds; use
Option<Rect<T>> for that.
Method contains#
pub fn contains(self: &Self, point: Point2<T>) -> boolReturns true when point is inside the rectangle or on one of its edges.
Method normalize#
pub fn normalize(self: Self) -> Rect<T>Returns the rectangle with xMin/xMax and yMin/yMax swapped where they are reversed.
Method unionWith#
pub fn unionWith(self: Self, other: Rect<T>) -> Rect<T>Returns the smallest rectangle that holds both self and other.
Details#
A zero-area rectangle still counts: its edges extend the result. Both inputs should be valid; a reversed input gives a result that holds neither.
Method include#
pub fn include(self: Self, point: Point2<T>) -> Rect<T>Returns the smallest rectangle that holds self and point.
Method intersection#
pub fn intersection(self: Self, other: Rect<T>) -> Option<Rect<T>>Returns the region that self and other share, or None when they do not overlap.
Details#
Edges are inclusive, so two rectangles that touch along an edge or at a corner give a
zero-area rectangle there, not None.
Implementation Rect<T>#
impl<T: Copy + Float> Rect<T>Method isFinite#
pub fn isFinite(self: Self) -> boolReturns true when all four edges are neither infinite nor NaN.
Implementation Equal for Rect<T>#
impl<T: Equal> Equal for Rect<T>Operation equals#
fn equals(left: &Self, right: &Self) -> boolCategory: Geometry.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.