primitives/foundation/box2d.rs
1use super::Point;
2
3/// A 2d axis aligned rectangle represented by its minimum and maximum coordinates.
4///
5/// # Representation
6///
7/// This struct is similar to [`Rect`], but stores rectangle as two endpoints
8/// instead of origin point and size. Such representation has several advantages over
9/// [`Rect`] representation:
10/// - Several operations are more efficient with `Box2D`, including [`intersection`],
11/// [`union`], and point-in-rect.
12/// - The representation is less susceptible to overflow. With [`Rect`], computation
13/// of second point can overflow for a large range of values of origin and size.
14/// However, with `Box2D`, computation of [`size`] cannot overflow if the coordinates
15/// are signed and the resulting size is unsigned.
16///
17/// A known disadvantage of `Box2D` is that translating the rectangle requires translating
18/// both points, whereas translating [`Rect`] only requires translating one point.
19///
20/// # Empty box
21///
22/// A box is considered empty (see [`is_empty`]) if any of the following is true:
23/// - it's area is empty,
24/// - it's area is negative (`min.x > max.x` or `min.y > max.y`),
25/// - it contains NaNs.
26///
27/// [`Rect`]: struct.Rect.html
28/// [`intersection`]: #method.intersection
29/// [`is_empty`]: #method.is_empty
30/// [`union`]: #method.union
31/// [`size`]: #method.size
32#[repr(C)]
33#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
34#[cfg_attr(
35 feature = "serde",
36 serde(bound(serialize = "T: Serialize", deserialize = "T: Deserialize<'de>"))
37)]
38pub struct Box2D<T> {
39 /// Represents min point
40 pub min: Point<T>,
41 /// Represents max point
42 pub max: Point<T>,
43}