Struct embedded_graphics::primitives::rectangle::Rectangle
source · 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
impl Rectangle
sourcepub const fn new(top_left: Point, size: Size) -> Rectangle
pub const fn new(top_left: Point, size: Size) -> Rectangle
Creates a new rectangle from the top left point and the size.
sourcepub fn with_corners(corner_1: Point, corner_2: Point) -> Rectangle
pub fn with_corners(corner_1: Point, corner_2: Point) -> Rectangle
Creates a new rectangle from two corners.
sourcepub const fn with_center(center: Point, size: Size) -> Rectangle
pub const fn with_center(center: Point, size: Size) -> Rectangle
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.
sourcepub fn center(&self) -> Point
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.
sourcepub fn bottom_right(&self) -> Option<Point>
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.
sourcepub fn contains(&self, point: Point) -> bool
pub fn contains(&self, point: Point) -> bool
Return whether the rectangle contains a given point.
sourcepub fn intersection(&self, other: &Rectangle) -> Rectangle
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());
sourcepub fn resized(&self, size: Size, anchor_point: AnchorPoint) -> Rectangle
pub fn resized(&self, size: Size, anchor_point: AnchorPoint) -> Rectangle
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))
);
sourcepub fn resized_width(&self, width: u32, anchor_x: AnchorX) -> Rectangle
pub fn resized_width(&self, width: u32, anchor_x: AnchorX) -> Rectangle
Returns a new rectangle with the given width, resized relative to the given anchor edge.
Examples
use embedded_graphics::{
prelude::*,
primitives::rectangle::Rectangle,
geometry::AnchorX,
};
let rect = Rectangle::new(Point::new(20, 20), Size::new(10, 20));
let resized = rect.resized_width(20, AnchorX::Center);
assert_eq!(
resized,
Rectangle::new(Point::new(15, 20), Size::new(20, 20))
);
sourcepub fn resized_height(&self, height: u32, anchor_y: AnchorY) -> Rectangle
pub fn resized_height(&self, height: u32, anchor_y: AnchorY) -> Rectangle
Returns a new rectangle with the given height, resized relative to the given anchor edge.
Examples
use embedded_graphics::{
prelude::*,
primitives::rectangle::Rectangle,
geometry::AnchorY,
};
let rect = Rectangle::new(Point::new(20, 20), Size::new(10, 20));
let resized = rect.resized_height(10, AnchorY::Center);
assert_eq!(
resized,
Rectangle::new(Point::new(20, 25), Size::new(10, 10))
);
sourcepub fn offset(&self, offset: i32) -> Rectangle
pub fn offset(&self, offset: i32) -> Rectangle
Offset the rectangle by a given value.
Negative values will shrink the rectangle.
sourcepub fn anchor_point(&self, anchor_point: AnchorPoint) -> Point
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)
);
sourcepub fn anchor_x(&self, anchor_x: AnchorX) -> i32
pub fn anchor_x(&self, anchor_x: AnchorX) -> i32
Returns the X coordinate of a given anchor edge of the rectangle.
Examples
use embedded_graphics::{
prelude::*,
primitives::rectangle::Rectangle,
geometry::AnchorX,
};
let mut rect = Rectangle::new(Point::new(20, 20), Size::new(11, 21));
assert_eq!(rect.anchor_x(AnchorX::Left), 20);
assert_eq!(rect.anchor_x(AnchorX::Center), 25);
sourcepub fn anchor_y(&self, anchor_y: AnchorY) -> i32
pub fn anchor_y(&self, anchor_y: AnchorY) -> i32
Returns the Y coordinate of a given anchor edge of the rectangle.
Examples
use embedded_graphics::{
prelude::*,
primitives::rectangle::Rectangle,
geometry::AnchorY,
};
let mut rect = Rectangle::new(Point::new(20, 20), Size::new(11, 21));
assert_eq!(rect.anchor_y(AnchorY::Top), 20);
assert_eq!(rect.anchor_y(AnchorY::Bottom), 40);
sourcepub fn rows(&self) -> Range<i32>
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
}
}
sourcepub fn columns(&self) -> Range<i32>
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
}
}
sourcepub const fn is_zero_sized(&self) -> bool
pub const 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 ContainsPoint for Rectangle
impl ContainsPoint for Rectangle
source§impl Dimensions for Rectangle
impl Dimensions for Rectangle
source§fn bounding_box(&self) -> Rectangle
fn bounding_box(&self) -> Rectangle
source§impl OffsetOutline for Rectangle
impl OffsetOutline for Rectangle
source§impl Ord for Rectangle
impl Ord for Rectangle
source§impl PartialEq<Rectangle> for Rectangle
impl PartialEq<Rectangle> for Rectangle
source§impl PartialOrd<Rectangle> for Rectangle
impl PartialOrd<Rectangle> for Rectangle
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PointsIter for Rectangle
impl PointsIter for Rectangle
source§impl Primitive for Rectangle
impl Primitive for Rectangle
source§fn into_styled<S>(self, style: S) -> Styled<Self, S>where
Self: Sized,
fn into_styled<S>(self, style: S) -> Styled<Self, S>where Self: Sized,
Styled
.source§impl<C: PixelColor> StyledDimensions<PrimitiveStyle<C>> for Rectangle
impl<C: PixelColor> StyledDimensions<PrimitiveStyle<C>> for Rectangle
source§fn styled_bounding_box(&self, style: &PrimitiveStyle<C>) -> Rectangle
fn styled_bounding_box(&self, style: &PrimitiveStyle<C>) -> Rectangle
source§impl<C: PixelColor> StyledDrawable<PrimitiveStyle<C>> for Rectangle
impl<C: PixelColor> StyledDrawable<PrimitiveStyle<C>> for Rectangle
source§fn draw_styled<D>(
&self,
style: &PrimitiveStyle<C>,
target: &mut D
) -> Result<Self::Output, D::Error>where
D: DrawTarget<Color = C>,
fn draw_styled<D>( &self, style: &PrimitiveStyle<C>, target: &mut D ) -> Result<Self::Output, D::Error>where D: DrawTarget<Color = C>,
source§impl Transform for Rectangle
impl Transform for Rectangle
source§fn translate(&self, by: Point) -> Self
fn translate(&self, by: Point) -> Self
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));
source§fn translate_mut(&mut self, by: Point) -> &mut Self
fn translate_mut(&mut self, by: Point) -> &mut Self
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
impl Eq for Rectangle
impl StructuralEq for Rectangle
impl StructuralPartialEq for Rectangle
Auto Trait Implementations§
impl RefUnwindSafe for Rectangle
impl Send for Rectangle
impl Sync for Rectangle
impl Unpin for Rectangle
impl UnwindSafe for Rectangle
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
source§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere Dst: LosslessTryFrom<Src>,
source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere Dst: LossyFrom<Src>,
source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where T: OverflowingCast<Dst>,
source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere Src: OverflowingCast<Dst>,
source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere T: SaturatingCast<Dst>,
source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere Src: SaturatingCast<Dst>,
source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.