pub struct Rectangle {
    pub top_left: Point,
    pub size: Size,
}
Expand description

Rectangle primitive

§Examples

§Create some rectangles with different styles

use embedded_graphics::{
    pixelcolor::Rgb565, prelude::*, primitives::{Rectangle, PrimitiveStyleBuilder},
};

// Rectangle with red 3 pixel wide stroke and green fill with the top left corner at (30, 20) and
// a size of (10, 15)
let style = PrimitiveStyleBuilder::new()
    .stroke_color(Rgb565::RED)
    .stroke_width(3)
    .fill_color(Rgb565::GREEN)
    .build();

Rectangle::new(Point::new(30, 20), Size::new(10, 15))
    .into_styled(style)
    .draw(&mut display)?;

// Rectangle with translation applied
Rectangle::new(Point::new(30, 20), Size::new(10, 15))
    .translate(Point::new(-20, -10))
    .into_styled(style)
    .draw(&mut display)?;

Fields§

§top_left: Point

Top left point of the rectangle.

§size: Size

Size of the rectangle.

Implementations§

source§

impl Rectangle

source

pub const fn new(top_left: Point, size: Size) -> Self

Creates a new rectangle from the top left point and the size.

source

pub fn with_corners(corner_1: Point, corner_2: Point) -> Self

Creates a new rectangle from two corners.

source

pub fn with_center(center: Point, size: Size) -> Self

Creates a new rectangle from the center point and the size.

For rectangles with even width and/or height the top left corner doesn’t align with the pixel grid. Because of this the coordinates of the top left corner will be rounded up to the nearest integer coordinate.

source

pub const fn zero() -> Rectangle

Returns a zero sized rectangle.

source

pub fn center(&self) -> Point

Returns the center of this rectangle.

For rectangles with even width and/or height the returned value is rounded down to the nearest integer coordinate.

source

pub fn bottom_right(&self) -> Option<Point>

Returns the bottom right corner of this rectangle.

Because the smallest rectangle that can be represented by its corners has a size of 1 x 1 pixels, this function returns None if the width or height of the rectangle is zero.

source

pub fn contains(&self, point: Point) -> bool

Return whether the rectangle contains a given point.

source

pub fn intersection(&self, other: &Rectangle) -> Rectangle

Returns a new Rectangle containing the intersection of self and other.

If no intersection is present, this method will return a zero sized rectangle.

§Examples
§Intersection

This example draws two rectangles to a mock display using the . character, along with their intersection shown with # characters.

use embedded_graphics::{
    mock_display::MockDisplay, pixelcolor::BinaryColor, prelude::*,
    primitives::{Rectangle, PrimitiveStyle},
};

let mut display = MockDisplay::new();

let rect1 = Rectangle::new(Point::zero(), Size::new(7, 8));
let rect2 = Rectangle::new(Point::new(2, 3), Size::new(10, 7));

let intersection = rect1.intersection(&rect2);

rect1
    .into_styled(PrimitiveStyle::with_stroke(BinaryColor::Off, 1))
    .draw(&mut display)?;

rect2
    .into_styled(PrimitiveStyle::with_stroke(BinaryColor::Off, 1))
    .draw(&mut display)?;

intersection
    .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1))
    .draw(&mut display)?;

display.assert_pattern(&[
    ".......     ",
    ".     .     ",
    ".     .     ",
    ". #####.....",
    ". #   #    .",
    ". #   #    .",
    ". #   #    .",
    "..#####    .",
    "  .        .",
    "  ..........",
]);
§No intersection

This example creates two rectangles with no intersection between them. In this case, intersection returns a zero-sized rectangle.

use embedded_graphics::{prelude::*, primitives::{Rectangle, PrimitiveStyle}};

let rect1 = Rectangle::new(Point::zero(), Size::new(7, 8));
let rect2 = Rectangle::new(Point::new(10, 15), Size::new(10, 7));

let intersection = rect1.intersection(&rect2);

assert!(intersection.is_zero_sized());
source

pub fn resized(&self, size: Size, anchor_point: AnchorPoint) -> Self

Returns a resized copy of this rectangle.

The rectangle is resized relative to the given anchor point.

§Examples
use embedded_graphics::{
    prelude::*,
    primitives::rectangle::Rectangle,
    geometry::AnchorPoint,
};

let rect = Rectangle::new(Point::new(20, 20), Size::new(10, 20));
let resized = rect.resized(Size::new(20, 10), AnchorPoint::Center);

assert_eq!(
    resized,
    Rectangle::new(Point::new(15, 25), Size::new(20, 10))
);
source

pub fn offset(&self, offset: i32) -> Self

Offset the rectangle by a given value.

Negative values will shrink the rectangle.

source

pub fn anchor_point(&self, anchor_point: AnchorPoint) -> Point

Returns an anchor point.

§Examples
use embedded_graphics::{
    prelude::*,
    primitives::rectangle::Rectangle,
    geometry::AnchorPoint,
};

let mut rect = Rectangle::new(Point::new(20, 20), Size::new(11, 21));

assert_eq!(rect.anchor_point(AnchorPoint::TopLeft), Point::new(20, 20));
assert_eq!(
    rect.anchor_point(AnchorPoint::BottomCenter),
    Point::new(25, 40)
);
source

pub fn rows(&self) -> Range<i32>

Returns the range of Y coordinates in this rectangle.

§Examples
use embedded_graphics::{prelude::*, primitives::Rectangle};

let rect = Rectangle::new(Point::new(10, 20), Size::new(3, 4));
assert_eq!(rect.rows(), 20..24);

By combining this method with columns it is possible to iterate over all pixels inside the rectangle. This can be more flexible than using the points iterator, for example, if a different iteration order is required or some operations should be called once per row.

use embedded_graphics::{prelude::*, primitives::Rectangle};

let rect = Rectangle::new(Point::new(10, 20), Size::new(3, 4));

// Iterate over the y coordinates of the rows in reverse order.
for y in rect.rows().rev() {
    for x in rect.columns() {
        // use x, y coordinates
    }
}
source

pub fn columns(&self) -> Range<i32>

Returns the range of X coordinates in this rectangle.

§Examples
use embedded_graphics::{prelude::*, primitives::Rectangle};

let rect = Rectangle::new(Point::new(10, 20), Size::new(3, 4));

assert_eq!(rect.columns(), 10..13);

By combining this method with rows it is possible to iterator over all pixels inside the rectangle. This can be more flexible than using the points iterator, for example, if a different iteration order is required or some operations should be called once per row.

use embedded_graphics::{prelude::*, primitives::Rectangle};

let rect = Rectangle::new(Point::new(10, 20), Size::new(3, 4));

// Iterate over all points starting from the top right corner and advancing downwards.
for x in rect.columns().rev() {
    for y in rect.rows() {
        // use x, y coordinates
    }
}
source

pub fn is_zero_sized(&self) -> bool

Returns true is the rectangle is zero sized.

A rectangle is zero sized if the width or height are zero.

§Examples
use embedded_graphics::{prelude::*, primitives::Rectangle};

let rect = Rectangle::new(Point::new(10, 20), Size::new(10, 20));
assert_eq!(rect.is_zero_sized(), false);

let rect = Rectangle::new(Point::new(10, 20), Size::zero());
assert_eq!(rect.is_zero_sized(), true);

Trait Implementations§

source§

impl Clone for Rectangle

source§

fn clone(&self) -> Rectangle

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Rectangle

source§

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

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

impl Default for Rectangle

source§

fn default() -> Rectangle

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

impl Dimensions for Rectangle

source§

fn bounding_box(&self) -> Rectangle

Returns the bounding box.
source§

impl Hash for Rectangle

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 Ord for Rectangle

source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · source§

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

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

impl PartialEq for Rectangle

source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Rectangle

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · 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 · 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 · 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 · 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 PointsIter for Rectangle

§

type Iter = Points

Iterator over all points inside the primitive.
source§

fn points(&self) -> Self::Iter

Returns an iterator over all points inside the primitive.
source§

impl Copy for Rectangle

source§

impl Eq for Rectangle

source§

impl StructuralPartialEq for Rectangle

Auto Trait Implementations§

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> Az for T

source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
source§

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

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

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

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
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> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
source§

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

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.