#[repr(C)]
pub struct Box2D<T, U> { pub min: Point2D<T, U>, pub max: Point2D<T, U>, }
Expand description

A 2d axis aligned rectangle represented by its minimum and maximum coordinates.

Representation

This struct is similar to Rect, but stores rectangle as two endpoints instead of origin point and size. Such representation has several advantages over Rect representation:

  • Several operations are more efficient with Box2D, including intersection, union, and point-in-rect.
  • The representation is less susceptible to overflow. With Rect, computation of second point can overflow for a large range of values of origin and size. However, with Box2D, computation of size cannot overflow if the coordinates are signed and the resulting size is unsigned.

A known disadvantage of Box2D is that translating the rectangle requires translating both points, whereas translating Rect only requires translating one point.

Empty box

A box is considered empty (see is_empty) if any of the following is true:

  • it’s area is empty,
  • it’s area is negative (min.x > max.x or min.y > max.y),
  • it contains NaNs.

Fields

min: Point2D<T, U>max: Point2D<T, U>

Implementations

Constructor.

Constructor.

Creates a Box2D of the given size, at offset zero.

Returns true if the box has a negative area.

The common interpretation for a negative box is to consider it empty. It can be obtained by calculating the intersection of two boxes that do not intersect.

Returns true if the size is zero, negative or NaN.

Returns true if the two boxes intersect.

Returns true if this box contains the point. Points are considered in the box if they are on the front, left or top faces, but outside if they are on the back, right or bottom faces.

Returns true if this box contains the interior of the other box. Always returns true if other is empty, and always returns false if other is nonempty but this box is empty.

Computes the intersection of two boxes, returning None if the boxes do not intersect.

Computes the intersection of two boxes without check whether they do intersect.

The result is a negative box if the boxes do not intersect. This can be useful for computing the intersection of more than two boxes, as it is possible to chain multiple intersection_unchecked calls and check for empty/negative result at the end.

Computes the union of two boxes.

If either of the boxes is empty, the other one is returned.

Returns the same box, translated by a vector.

Change the size of the box by adjusting the max endpoint without modifying the min endpoint.

Inflates the box by the specified sizes on each side respectively.

Calculate the size and position of an inner box.

Subtracts the side offsets from all sides. The horizontal, vertical and applicate offsets must not be larger than the original side length.

Calculate the b and position of an outer box.

Add the offsets to all sides. The expanded box is returned.

Returns the smallest box containing all of the provided points.

Linearly interpolate between this box and another box.

Constructor, setting all sides to zero.

Drop the units, preserving only the numeric value.

Tag a unitless value with units.

Cast the unit

Cast from one numeric representation to another, preserving the units.

When casting from floating point to integer coordinates, the decimals are truncated as one would expect from a simple cast, but this behavior does not always make sense geometrically. Consider using round(), round_in or round_out() before casting.

Fallible cast from one numeric representation to another, preserving the units.

When casting from floating point to integer coordinates, the decimals are truncated as one would expect from a simple cast, but this behavior does not always make sense geometrically. Consider using round(), round_in or round_out() before casting.

Cast into an f32 box.

Cast into an f64 box.

Cast into an usize box, truncating decimals if any.

When casting from floating point boxes, it is worth considering whether to round(), round_in() or round_out() before the cast in order to obtain the desired conversion behavior.

Cast into an u32 box, truncating decimals if any.

When casting from floating point boxes, it is worth considering whether to round(), round_in() or round_out() before the cast in order to obtain the desired conversion behavior.

Cast into an i32 box, truncating decimals if any.

When casting from floating point boxes, it is worth considering whether to round(), round_in() or round_out() before the cast in order to obtain the desired conversion behavior.

Cast into an i64 box, truncating decimals if any.

When casting from floating point boxes, it is worth considering whether to round(), round_in() or round_out() before the cast in order to obtain the desired conversion behavior.

Returns true if all members are finite.

Return a box with edges rounded to integer coordinates, such that the returned box has the same set of pixel centers as the original one. Values equal to 0.5 round up. Suitable for most places where integral device coordinates are needed, but note that any translation should be applied first to avoid pixel rounding errors. Note that this is not rounding to nearest integer if the values are negative. They are always rounding as floor(n + 0.5).

Return a box with faces/edges rounded to integer coordinates, such that the original box contains the resulting box.

Return a box with faces/edges rounded to integer coordinates, such that the original box is contained in the resulting box.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Converts to this type from the input type.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.