pub struct Rect {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}Expand description
A rectangle in document space.
Fields§
§x: f32X coordinate of top-left corner
y: f32Y coordinate of top-left corner
width: f32Width of rectangle
height: f32Height of rectangle
Implementations§
Source§impl Rect
impl Rect
Sourcepub fn new(x: f32, y: f32, width: f32, height: f32) -> Self
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);Sourcepub fn from_points(x0: f32, y0: f32, x1: f32, y1: f32) -> Self
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);Sourcepub fn center(&self) -> Point
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);Sourcepub fn intersects(&self, other: &Rect) -> bool
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));Sourcepub fn intersection(&self, other: &Rect) -> Option<Rect>
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());Sourcepub fn contains_point(&self, p: &Point) -> bool
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));Sourcepub fn contains_rect(&self, other: &Rect) -> bool
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));Sourcepub fn union(&self, other: &Rect) -> Rect
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);Trait Implementations§
impl Copy for Rect
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> 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
Mutably borrows from an owned value. Read more