pub struct Rect<T> {
    pub lower_left: Point<T>,
    pub upper_right: Point<T>,
}
Expand description

A rectangle which is oriented along the x an y axis and represented by its lower left and upper right corner.

Fields

lower_left: Point<T>

Lower left corner of the rectangle.

upper_right: Point<T>

Upper right corner of the rectangle.

Implementations

Construct the bounding box of the two points. Order does not matter.

Examples
use iron_shapes::prelude::*;

// Create a rectangle based on two corner points.
let rect1 = Rect::new(Point::new(0, 0), Point::new(1, 2));
// Any type that implements `Into<Point<T>>` can be used for the corner points.
let rect2 = Rect::new((1, 2), (0, 0));
// Ordering of the corner points does not matter.
assert_eq!(rect1, rect2);
// Even though `(0, 0)` was passed as second argument it is recognized as lower left corner.
assert_eq!(rect2.lower_left(), Point::new(0, 0));

Get the lower left corner.

Get the upper left corner.

Get the upper right corner.

Get the lower right corner.

Check if rectangle contains the point. Inclusive boundaries.

Example
use iron_shapes::prelude::*;
let rect = Rect::new((0, 0), (10, 20));
// Contains point somewhere in the center.
assert!(rect.contains_point(Point::new(5, 5)));
// Also contains point on the boundaries.
assert!(rect.contains_point(Point::new(0, 0)));
// Does not contain point outside of the rectangle.
assert!(!rect.contains_point(Point::new(10, 21)));

Check if rectangle contains the point. Exclusive boundaries.

Example
use iron_shapes::prelude::*;
let rect = Rect::new((0, 0), (10, 20));
// Contains point somewhere in the center.
assert!(rect.contains_point_exclusive(Point::new(5, 5)));
// Does not contain points on boundaries.
assert!(!rect.contains_point_exclusive(Point::new(0, 0)));
// Does not contain point outside of the rectangle.
assert!(!rect.contains_point_exclusive(Point::new(10, 21)));

Check if rectangle contains other rectangle. Inclusive boundaries.

Example
use iron_shapes::prelude::*;

let outer = Rect::new((0, 0), (2, 2));
let inner = Rect::new((0, 0), (1, 1));
assert!(outer.contains_rectangle(&inner));
assert!(!inner.contains_rectangle(&outer));

Check if rectangle contains other rectangle. Exclusive boundaries.

Example
use iron_shapes::prelude::*;

let outer = Rect::new((0, 0), (3, 3));
let inner = Rect::new((1, 1), (2, 2));
assert!(outer.contains_rectangle_exclusive(&inner));
assert!(!inner.contains_rectangle_exclusive(&outer));

let not_inner = Rect::new((0, 0), (1, 1)); // This shares the boundary with `outer`.
assert!(!outer.contains_rectangle_exclusive(&not_inner));

Test if the both rectangles touch each other, i.e. if they either share a boundary or are overlapping.

Compute the boolean intersection of two rectangles. This function excludes the boundaries, hence a zero-area intersection is considered None. See intersection_inclusive_bounds() zero-area intersections should be returned as Some(rectangle).

Example
use iron_shapes::prelude::*;

// Create two overlapping rectangles.
let a = Rect::new((0, 0), (2, 2));
let b = Rect::new((1, 1), (3, 3));

// Compute the intersection.
assert_eq!(a.intersection(&b), Some(Rect::new((1, 1), (2, 2))));

// Create a non-overlapping rectangle.
let c = Rect::new((100, 100), (200, 200));
// The intersection with a non-overlapping rectangle is `None`.
assert_eq!(a.intersection(&c), None);

Compute the boolean intersection of two rectangles and include the boundaries. This allows to get zero-area intersection results for example if the two rectangles touch on a boundary or one of the rectangle is already zero-area.

Example
use iron_shapes::prelude::*;

// Create two rectangles which intersect in a single point.
let a = Rect::new((0, 0), (2, 2));
let b = Rect::new((2, 2), (3, 3));

// Compute the intersection.
assert_eq!(a.intersection_inclusive_bounds(&b), Some(Rect::new((2, 2), (2, 2))));

Compute the width of the rectangle.

Compute the height of the rectangle.

Create the smallest Rect that contains the original Rect and the point.

Example
use iron_shapes::prelude::*;

let r1 = Rect::new((0,0), (1,2));

let r2 = r1.add_point(Point::new(10, 11));

assert_eq!(r2, Rect::new((0,0), (10,11)));

Get the smallest Rect that contains both rectangles self and rect.

Example
use iron_shapes::prelude::*;

let r1 = Rect::new((0,0), (1,2));
let r2 = Rect::new((4,5), (6,7));

let r3 = r1.add_rect(&r2);

assert_eq!(r3, Rect::new((0,0), (6,7)));

Get the center point of the rectangle. When using integer coordinates the resulting coordinates will be truncated to the next integers.

Create an enlarged copy of this rectangle. The vertical boundaries will be shifted towards the outside by add_x. The horizontal boundaries will be shifted towards the outside by add_y.

Trait Implementations

Get bounding box of rectangle (which is equal to the rectangle itself).

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Calculate doubled oriented area of rectangle.

Create a polygon from a rectangle.

Converts to this type from the input type.

Converts to this type from the input type.

Create a polygon from a rectangle.

Converts to this type from the input type.

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

Type of edge which will be returned.

Iterator type.

Get an iterator over edges.

Iterate over all points of the rectangle. Starts with the lower left corner and iterates counter clock-wise.

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Iterate over all points of the rectangle. Starts with the lower left corner and iterates counter clock-wise.

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Type of the points.

Iterator over points.

Iterate over points.

Type which represents the segments.

Iterator over segments.

Iterate over segments/edges of a polygon.

Point wise transformation of the two corner points.

Point wise transformation.

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

This method tests for !=.

Set points from an iterator.

Iterator over alternating x/y coordinates of the points. Starts with an x coordinate.

Iterate over alternating x/y coordinates of the polygon vertices. Start with an x coordinate.

Point type used for the vertices.

Type used for the polygon segments.

Iterator over all points.

Get number of polygons.

Add the point p to all vertices.

Add the point p to all vertices.

Multiply all vertices with a factor.

Multiply all vertices with a factor.

Iterate over all vertices.

Get the number of holes.

Type used for representing a one-dimensional interval.

Get the interval which is spanned by the rectangle in the given orientation.

Convert the geometric object into a polygon.

Get bounding box of rectangle (always exists).

Output type of the cast. This is likely the same geometrical type just with other coordinate types. Read more

Try to cast to target data type. Read more

Cast to target data type. Read more

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.

Return the geometrical object mirrored at the x axis.

Return the geometrical object mirrored at the y axis.

Rotate the geometrical shape by a multiple of 90 degrees.

Scale the geometrical shape. Scaling center is the origin (0, 0).

The resulting type after obtaining ownership.

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

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

Translate the geometrical object by a vector v.

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.