Skip to main content

Rect

Struct Rect 

Source
pub struct Rect {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}
Expand description

A rectangle in document space.

Fields§

§x: f32

X coordinate of top-left corner

§y: f32

Y coordinate of top-left corner

§width: f32

Width of rectangle

§height: f32

Height of rectangle

Implementations§

Source§

impl Rect

Source

pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self

Create a new rectangle from position and dimensions. Normalizes dimensions so width and height are always non-negative.

§Examples
use xberg_native_pdf::geometry::Rect;

let rect = Rect::new(0.0, 50.0, 100.0, -50.0);
assert_eq!(rect.y, 0.0);
assert_eq!(rect.height, 50.0);
Source

pub fn normalize(self) -> Self

Ensure width and height are non-negative.

Source

pub fn from_points(x0: f32, y0: f32, x1: f32, y1: f32) -> Self

Create a rectangle from two corner points.

§Examples
use xberg_native_pdf::geometry::Rect;

let rect = Rect::from_points(10.0, 20.0, 110.0, 70.0);
assert_eq!(rect.x, 10.0);
assert_eq!(rect.y, 20.0);
assert_eq!(rect.width, 100.0);
assert_eq!(rect.height, 50.0);
Source

pub fn left(&self) -> f32

Get the left edge x-coordinate.

Source

pub fn right(&self) -> f32

Get the right edge x-coordinate.

Source

pub fn top(&self) -> f32

Get the top edge y-coordinate.

Source

pub fn bottom(&self) -> f32

Get the bottom edge y-coordinate.

Source

pub fn center(&self) -> Point

Get the center point of the rectangle.

§Examples
use xberg_native_pdf::geometry::Rect;

let rect = Rect::new(0.0, 0.0, 100.0, 50.0);
let center = rect.center();
assert_eq!(center.x, 50.0);
assert_eq!(center.y, 25.0);
Source

pub fn intersects(&self, other: &Rect) -> bool

Check if this rectangle intersects with another.

§Examples
use xberg_native_pdf::geometry::Rect;

let r1 = Rect::new(0.0, 0.0, 100.0, 100.0);
let r2 = Rect::new(50.0, 50.0, 100.0, 100.0);
let r3 = Rect::new(200.0, 200.0, 100.0, 100.0);

assert!(r1.intersects(&r2));
assert!(!r1.intersects(&r3));
Source

pub fn intersection(&self, other: &Rect) -> Option<Rect>

Compute the intersection of this rectangle with another.

Returns the overlapping region as a new rectangle, or None if they don’t overlap.

§Examples
use xberg_native_pdf::geometry::Rect;

let r1 = Rect::new(0.0, 0.0, 100.0, 100.0);
let r2 = Rect::new(50.0, 50.0, 100.0, 100.0);
let r3 = Rect::new(200.0, 200.0, 100.0, 100.0);

let inter = r1.intersection(&r2).unwrap();
assert_eq!(inter.x, 50.0);
assert_eq!(inter.y, 50.0);
assert_eq!(inter.width, 50.0);
assert_eq!(inter.height, 50.0);

assert!(r1.intersection(&r3).is_none());
Source

pub fn contains_point(&self, p: &Point) -> bool

Check if this rectangle contains a point.

§Examples
use xberg_native_pdf::geometry::{Rect, Point};

let rect = Rect::new(0.0, 0.0, 100.0, 100.0);
let p1 = Point::new(50.0, 50.0);
let p2 = Point::new(150.0, 150.0);

assert!(rect.contains_point(&p1));
assert!(!rect.contains_point(&p2));
Source

pub fn contains_rect(&self, other: &Rect) -> bool

Check if this rectangle fully contains another rectangle.

§Examples
use xberg_native_pdf::geometry::Rect;

let r1 = Rect::new(0.0, 0.0, 100.0, 100.0);
let r2 = Rect::new(10.0, 10.0, 50.0, 50.0);
let r3 = Rect::new(50.0, 50.0, 100.0, 100.0);

assert!(r1.contains_rect(&r2));
assert!(!r1.contains_rect(&r3));
Source

pub fn union(&self, other: &Rect) -> Rect

Compute the union of this rectangle with another.

Returns the smallest rectangle that contains both rectangles.

§Examples
use xberg_native_pdf::geometry::Rect;

let r1 = Rect::new(0.0, 0.0, 50.0, 50.0);
let r2 = Rect::new(25.0, 25.0, 50.0, 50.0);
let union = r1.union(&r2);

assert_eq!(union.x, 0.0);
assert_eq!(union.y, 0.0);
assert_eq!(union.right(), 75.0);
assert_eq!(union.bottom(), 75.0);
Source

pub fn area(&self) -> f32

Compute the area of the rectangle.

§Examples
use xberg_native_pdf::geometry::Rect;

let rect = Rect::new(0.0, 0.0, 100.0, 50.0);
assert_eq!(rect.area(), 5000.0);

Trait Implementations§

Source§

impl Clone for Rect

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 Copy for Rect

Source§

impl Debug for Rect

Source§

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

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

impl Default for Rect

Source§

fn default() -> Self

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

impl PartialEq for Rect

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 Serialize for Rect

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 StructuralPartialEq for Rect

Auto Trait Implementations§

§

impl Freeze for Rect

§

impl RefUnwindSafe for Rect

§

impl Send for Rect

§

impl Sync for Rect

§

impl Unpin for Rect

§

impl UnsafeUnpin for Rect

§

impl UnwindSafe for Rect

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more