Struct embedded_graphics::primitives::rectangle::Rectangle[][src]

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

impl Rectangle[src]

pub const fn new(top_left: Point, size: Size) -> Rectangle[src]

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

pub fn with_corners(corner_1: Point, corner_2: Point) -> Rectangle[src]

Creates a new rectangle from two corners.

pub fn with_center(center: Point, size: Size) -> Rectangle[src]

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.

pub const fn zero() -> Rectangle[src]

Returns a zero sized rectangle.

pub fn center(&self) -> Point[src]

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.

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

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.

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

Return whether the rectangle contains a given point.

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

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());

pub fn resized(&self, size: Size, anchor_point: AnchorPoint) -> Rectangle[src]

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))
);

pub fn offset(&self, offset: i32) -> Rectangle[src]

Offset the rectangle by a given value.

Negative values will shrink the rectangle.

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

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)
);

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

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
    }
}

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

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
    }
}

pub fn is_zero_sized(&self) -> bool[src]

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

impl Clone for Rectangle[src]

pub fn clone(&self) -> Rectangle[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl ContainsPoint for Rectangle[src]

fn contains(&self, point: Point) -> bool[src]

Returns true if the given point is inside the shape.

impl Debug for Rectangle[src]

pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>[src]

Formats the value using the given formatter. Read more

impl Default for Rectangle[src]

pub fn default() -> Rectangle[src]

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

impl Dimensions for Rectangle[src]

pub fn bounding_box(&self) -> Rectangle[src]

Returns the bounding box.

impl Hash for Rectangle[src]

pub fn hash<__H>(&self, state: &mut __H) where
    __H: Hasher
[src]

Feeds this value into the given Hasher. Read more

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

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

impl OffsetOutline for Rectangle[src]

fn offset(&self, offset: i32) -> Self[src]

Offsets the outline of the shape. Read more

impl Ord for Rectangle[src]

pub fn cmp(&self, other: &Rectangle) -> Ordering[src]

This method returns an Ordering between self and other. Read more

#[must_use]
fn max(self, other: Self) -> Self
1.21.0[src]

Compares and returns the maximum of two values. Read more

#[must_use]
fn min(self, other: Self) -> Self
1.21.0[src]

Compares and returns the minimum of two values. Read more

#[must_use]
fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]

Restrict a value to a certain interval. Read more

impl PartialEq<Rectangle> for Rectangle[src]

pub fn eq(&self, other: &Rectangle) -> bool[src]

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

pub fn ne(&self, other: &Rectangle) -> bool[src]

This method tests for !=.

impl PartialOrd<Rectangle> for Rectangle[src]

pub fn partial_cmp(&self, other: &Rectangle) -> Option<Ordering>[src]

This method returns an ordering between self and other values if one exists. Read more

#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than (for self and other) and is used by the < operator. Read more

#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PointsIter for Rectangle[src]

type Iter = Points

Iterator over all points inside the primitive.

pub fn points(&self) -> <Rectangle as PointsIter>::Iter[src]

Returns an iterator over all points inside the primitive.

impl Primitive for Rectangle[src]

fn into_styled<S>(self, style: S) -> Styled<Self, S> where
    Self: Sized
[src]

Converts this primitive into a Styled.

impl<C: PixelColor> StyledDimensions<PrimitiveStyle<C>> for Rectangle[src]

fn styled_bounding_box(&self, style: &PrimitiveStyle<C>) -> Rectangle[src]

Returns the bounding box using the given style.

impl<C: PixelColor> StyledDrawable<PrimitiveStyle<C>> for Rectangle[src]

type Color = C

Color type.

type Output = ()

Output type.

fn draw_styled<D>(
    &self,
    style: &PrimitiveStyle<C>,
    target: &mut D
) -> Result<Self::Output, D::Error> where
    D: DrawTarget<Color = C>, 
[src]

Draws the primitive using the given style.

impl Transform for Rectangle[src]

fn translate(&self, by: Point) -> Self[src]

Translate the rect from its current position to a new position by (x, y) pixels, returning a new Rectangle. For a mutating transform, see translate_mut.

let rect = Rectangle::new(Point::new(5, 10), Size::new(10, 10));
let moved = rect.translate(Point::new(10, 10));

assert_eq!(moved.top_left, Point::new(15, 20));
assert_eq!(moved.size, Size::new(10, 10));

fn translate_mut(&mut self, by: Point) -> &mut Self[src]

Translate the rect from its current position to a new position by (x, y) pixels.

let mut rect = Rectangle::new(Point::new(5, 10), Size::new(10, 10));
rect.translate_mut(Point::new(10, 10));

assert_eq!(rect.top_left, Point::new(15, 20));
assert_eq!(rect.size, Size::new(10, 10));

impl Copy for Rectangle[src]

impl Eq for Rectangle[src]

impl StructuralEq for Rectangle[src]

impl StructuralPartialEq for Rectangle[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<Src, Dst> LosslessTryInto<Dst> for Src where
    Dst: LosslessTryFrom<Src>, 
[src]

pub fn lossless_try_into(self) -> Option<Dst>[src]

Performs the conversion.

impl<Src, Dst> LossyInto<Dst> for Src where
    Dst: LossyFrom<Src>, 
[src]

pub fn lossy_into(self) -> Dst[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> Scalar for T where
    T: Copy + PartialEq<T> + Debug + Any
[src]

pub fn inlined_clone(&self) -> T[src]

Performance hack: Clone doesn’t get inlined for Copy types in debug mode, so make it inline anyway.

fn is<T>() -> bool where
    T: Scalar
[src]

Tests if Self the same as the type T Read more

impl<SS, SP> SupersetOf<SS> for SP where
    SS: SubsetOf<SP>, 

pub fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more

pub fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).

pub fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.

pub fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

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

recently added

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V