Skip to main content

Rectangle

Struct Rectangle 

Source
pub struct Rectangle<P> { /* private fields */ }
Expand description

An n-dimensional rectangle defined by its two corners.

This rectangle can be directly inserted into an r-tree.

Note: Despite being called rectangle, this struct can be used with more than two dimensions by using an appropriate point type.

§Type parameters

P: The rectangle’s Point type.

Implementations§

Source§

impl<P> Rectangle<P>

Source

pub fn from_corners(corner_1: P, corner_2: P) -> Self
where P: Point,

Creates a new rectangle defined by two corners.

Examples found in repository?
examples/hashmap.rs (line 18)
10fn main() {
11    // A hashmap of 2D rectangles will be the underlying collection.
12    let rect_hashmap: HashMap<i32, Rectangle<(i32, i32)>> = HashMap::new();
13
14    // Wrap `RTreed` around the hashmap.
15    let mut rtreed = RTreed::new(rect_hashmap);
16
17    // Insert two rectangles, recording them in the R-tree.
18    rtreed.insert(1, Rectangle::from_corners((0, 0), (1, 1)));
19    rtreed.insert(2, Rectangle::from_corners((1, 1), (2, 2)));
20
21    // Locate the two rectangles in the R-tree.
22    assert_eq!(
23        rtreed
24            .rtree()
25            .locate_in_envelope(AABB::from_corners((0, 0), (2, 2)))
26            .count(),
27        2
28    );
29
30    // Now remove one of the rectangles, recording this in the R-tree.
31    rtreed.remove(&1);
32
33    // Make the same query to the R-tree as before.
34    // Only one rectangle is now present.
35    assert_eq!(
36        rtreed
37            .rtree()
38            .locate_in_envelope(AABB::from_corners((0, 0), (2, 2)))
39            .count(),
40        1
41    );
42}
More examples
Hide additional examples
examples/vec.rs (line 16)
8fn main() {
9    // A vec of 2D rectangles will be the underlying collection.
10    let rect_vec: Vec<Rectangle<(i32, i32)>> = Vec::new();
11
12    // Wrap `RTreed` around the vec.
13    let mut rtreed = RTreed::new(rect_vec);
14
15    // Push two rectangles, recording them in the R-tree.
16    rtreed.push(Rectangle::from_corners((0, 0), (1, 1)));
17    rtreed.push(Rectangle::from_corners((1, 1), (2, 2)));
18
19    // Locate the two rectangles in the R-tree.
20    assert_eq!(
21        rtreed
22            .rtree()
23            .locate_in_envelope(AABB::from_corners((0, 0), (2, 2)))
24            .count(),
25        2
26    );
27
28    // Access a rectangle by its index in the vec.
29    assert_eq!(
30        rtreed.get(&0),
31        Some(&Rectangle::from_corners((0, 0), (1, 1)))
32    );
33
34    // Push one more rectangle.
35    rtreed.push(Rectangle::from_corners((2, 2), (3, 3)));
36
37    // Swap-remove the first rectangle.
38    assert_eq!(
39        rtreed.swap_remove(&0),
40        Rectangle::from_corners((0, 0), (1, 1))
41    );
42
43    // Now the latest-pushed rectangle occupies the place of the first
44    // rectangle. The second rectangle is as it was previously.
45    assert_eq!(
46        rtreed.get(&0),
47        Some(&Rectangle::from_corners((2, 2), (3, 3)))
48    );
49    assert_eq!(
50        rtreed.get(&1),
51        Some(&Rectangle::from_corners((1, 1), (2, 2)))
52    );
53    assert_eq!(
54        rtreed
55            .rtree()
56            .locate_in_envelope(AABB::from_corners((0, 0), (3, 3)))
57            .count(),
58        2
59    );
60}
examples/undoredo.rs (line 22)
11fn main() {
12    // A hashmap of 2D rectangles will be the underlying container.
13    let rect_hashmap: HashMap<i32, Rectangle<(i32, i32)>> = HashMap::new();
14    // Wrap `RTreed` around the hashmap and then `Recorder` around it.
15    let mut recorder = Recorder::<
16        RTreed<HashMap<i32, Rectangle<(i32, i32)>>>,
17        RTreedHalfDelta<i32, Rectangle<(i32, i32)>>,
18    >::new(RTreed::new(rect_hashmap));
19    let mut undoredo: UndoRedo<RTreedDelta<i32, Rectangle<(i32, i32)>>> = UndoRedo::new();
20
21    // Insert two rectangles, recording them in the R-tree.
22    recorder.insert(1, Rectangle::from_corners((0, 0), (1, 1)));
23    undoredo.commit(&mut recorder);
24
25    recorder.insert(2, Rectangle::from_corners((1, 1), (2, 2)));
26    undoredo.commit(&mut recorder);
27
28    // Locate the two rectangles in the R-tree.
29    assert_eq!(
30        recorder
31            .container()
32            .rtree()
33            .locate_in_envelope(AABB::from_corners((0, 0), (2, 2)))
34            .count(),
35        2
36    );
37
38    undoredo.undo(&mut recorder);
39
40    // After undo, there is now only one rectangle.
41    assert_eq!(
42        recorder
43            .container()
44            .rtree()
45            .locate_in_envelope(AABB::from_corners((0, 0), (2, 2)))
46            .count(),
47        1
48    );
49
50    undoredo.undo(&mut recorder);
51
52    // After another undo, there is no rectangles anymore.
53    assert_eq!(
54        recorder
55            .container()
56            .rtree()
57            .locate_in_envelope(AABB::from_corners((0, 0), (2, 2)))
58            .count(),
59        0
60    );
61
62    undoredo.redo(&mut recorder);
63
64    // After redo, we are back to one rectangle.
65    assert_eq!(
66        recorder
67            .container()
68            .rtree()
69            .locate_in_envelope(AABB::from_corners((0, 0), (2, 2)))
70            .count(),
71        1
72    );
73
74    undoredo.redo(&mut recorder);
75
76    // After another redo, we are back to two rectangles.
77    assert_eq!(
78        recorder
79            .container()
80            .rtree()
81            .locate_in_envelope(AABB::from_corners((0, 0), (2, 2)))
82            .count(),
83        2
84    );
85}
Source

pub fn from_aabb(aabb: AABB<P>) -> Self

Creates a new rectangle defined by it’s [axis aligned bounding box(AABB).

Source§

impl<P: Clone> Rectangle<P>

Source

pub fn lower(&self) -> P

Returns the rectangle’s lower corner.

This is the point contained within the rectangle with the smallest coordinate value in each dimension.

Source

pub fn upper(&self) -> P

Returns the rectangle’s upper corner.

This is the point contained within the AABB with the largest coordinate value in each dimension.

Source§

impl<P> Rectangle<P>
where P: Point,

Source

pub fn nearest_point(&self, query_point: &P) -> P

Returns the nearest point within this rectangle to a given point.

If query_point is contained within this rectangle, query_point is returned.

Trait Implementations§

Source§

impl<P: Clone> Clone for Rectangle<P>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P: Copy> Copy for Rectangle<P>

Source§

impl<P: Debug> Debug for Rectangle<P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, P> Deserialize<'de> for Rectangle<P>
where P: Deserialize<'de>,

Available on crate feature serde only.
Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<P: Eq> Eq for Rectangle<P>

Source§

impl<P> From<AABB<P>> for Rectangle<P>

Source§

fn from(aabb: AABB<P>) -> Self

Converts to this type from the input type.
Source§

impl<P: Hash> Hash for Rectangle<P>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl<P: Ord> Ord for Rectangle<P>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl<P: PartialEq> PartialEq for Rectangle<P>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<P: PartialOrd> PartialOrd for Rectangle<P>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<P> PointDistance for Rectangle<P>
where P: Point,

Source§

fn distance_2( &self, point: &<Self::Envelope as Envelope>::Point, ) -> <<Self::Envelope as Envelope>::Point as Point>::Scalar

Returns the squared distance between an object and a point. Read more
Source§

fn contains_point(&self, point: &<Self::Envelope as Envelope>::Point) -> bool

Returns true if a point is contained within this object. Read more
Source§

fn distance_2_if_less_or_equal( &self, point: &<Self::Envelope as Envelope>::Point, max_distance_2: <<Self::Envelope as Envelope>::Point as Point>::Scalar, ) -> Option<<<Self::Envelope as Envelope>::Point as Point>::Scalar>

Returns the squared distance to this object, or None if the distance is larger than a given maximum value. Read more
Source§

impl<P> RTreeObject for Rectangle<P>
where P: Point,

Source§

type Envelope = AABB<P>

The object’s envelope type. Usually, AABB will be the right choice. This type also defines the object’s dimensionality.
Source§

fn envelope(&self) -> Self::Envelope

Returns the object’s envelope. Read more
Source§

impl<P> Serialize for Rectangle<P>
where P: Serialize,

Available on crate feature serde only.
Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<P: PartialEq> StructuralPartialEq for Rectangle<P>

Auto Trait Implementations§

§

impl<P> Freeze for Rectangle<P>
where AABB<P>: Freeze,

§

impl<P> RefUnwindSafe for Rectangle<P>
where AABB<P>: RefUnwindSafe,

§

impl<P> Send for Rectangle<P>
where AABB<P>: Send,

§

impl<P> Sync for Rectangle<P>
where AABB<P>: Sync,

§

impl<P> Unpin for Rectangle<P>
where AABB<P>: Unpin,

§

impl<P> UnsafeUnpin for Rectangle<P>
where AABB<P>: UnsafeUnpin,

§

impl<P> UnwindSafe for Rectangle<P>
where AABB<P>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T, U> CastInto<U> for T
where U: CastFrom<T>,

Source§

fn cast_into(self) -> U

Convert this value’s type into another type to another using Rust’s as keyword casting.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> QuantizeInto<U> for T
where U: QuantizeFrom<T>,

Source§

fn quantize_into(self, multiplier: T) -> U

Multiply a number by a multiplier, round it to an integer, and convert it to another type, or do nothing if input type is integer.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.