165 lignes
3.8 KiB
Rust
165 lignes
3.8 KiB
Rust
//! Integer geometry primitives optimized for 2D UI and compositor coordinate systems.
|
|
|
|
use std::fmt;
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Default, Hash)]
|
|
pub struct Point {
|
|
pub x: i32,
|
|
pub y: i32,
|
|
}
|
|
|
|
impl Point {
|
|
pub const ZERO: Self = Self { x: 0, y: 0 };
|
|
|
|
pub const fn new(x: i32, y: i32) -> Self {
|
|
Self { x, y }
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for Point {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "Point({}, {})", self.x, self.y)
|
|
}
|
|
}
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Default, Hash)]
|
|
pub struct Size {
|
|
pub width: u32,
|
|
pub height: u32,
|
|
}
|
|
|
|
impl Size {
|
|
pub const ZERO: Self = Self { width: 0, height: 0 };
|
|
|
|
pub const fn new(width: u32, height: u32) -> Self {
|
|
Self { width, height }
|
|
}
|
|
|
|
pub fn area(&self) -> u64 {
|
|
self.width as u64 * self.height as u64
|
|
}
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
self.width == 0 || self.height == 0
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for Size {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "Size({}x{})", self.width, self.height)
|
|
}
|
|
}
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Default, Hash)]
|
|
pub struct Rect {
|
|
pub x: i32,
|
|
pub y: i32,
|
|
pub width: u32,
|
|
pub height: u32,
|
|
}
|
|
|
|
impl Rect {
|
|
pub const ZERO: Self = Self {
|
|
x: 0,
|
|
y: 0,
|
|
width: 0,
|
|
height: 0,
|
|
};
|
|
|
|
pub const fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
|
|
Self { x, y, width, height }
|
|
}
|
|
|
|
pub const fn from_origin_size(origin: Point, size: Size) -> Self {
|
|
Self {
|
|
x: origin.x,
|
|
y: origin.y,
|
|
width: size.width,
|
|
height: size.height,
|
|
}
|
|
}
|
|
|
|
pub fn origin(&self) -> Point {
|
|
Point::new(self.x, self.y)
|
|
}
|
|
|
|
pub fn size(&self) -> Size {
|
|
Size::new(self.width, self.height)
|
|
}
|
|
|
|
pub fn right(&self) -> i32 {
|
|
self.x.saturating_add(self.width as i32)
|
|
}
|
|
|
|
pub fn bottom(&self) -> i32 {
|
|
self.y.saturating_add(self.height as i32)
|
|
}
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
self.width == 0 || self.height == 0
|
|
}
|
|
|
|
pub fn contains(&self, p: Point) -> bool {
|
|
p.x >= self.x && p.x < self.right() && p.y >= self.y && p.y < self.bottom()
|
|
}
|
|
|
|
pub fn intersects(&self, other: &Rect) -> bool {
|
|
if self.is_empty() || other.is_empty() {
|
|
return false;
|
|
}
|
|
self.x < other.right()
|
|
&& self.right() > other.x
|
|
&& self.y < other.bottom()
|
|
&& self.bottom() > other.y
|
|
}
|
|
|
|
pub fn intersection(&self, other: &Rect) -> Option<Rect> {
|
|
if !self.intersects(other) {
|
|
return None;
|
|
}
|
|
|
|
let x1 = self.x.max(other.x);
|
|
let y1 = self.y.max(other.y);
|
|
let x2 = self.right().min(other.right());
|
|
let y2 = self.bottom().min(other.bottom());
|
|
|
|
Some(Rect::new(x1, y1, (x2 - x1) as u32, (y2 - y1) as u32))
|
|
}
|
|
|
|
pub fn bounding_box(&self, other: &Rect) -> Rect {
|
|
if self.is_empty() {
|
|
return *other;
|
|
}
|
|
if other.is_empty() {
|
|
return *self;
|
|
}
|
|
|
|
let x1 = self.x.min(other.x);
|
|
let y1 = self.y.min(other.y);
|
|
let x2 = self.right().max(other.right());
|
|
let y2 = self.bottom().max(other.bottom());
|
|
|
|
Rect::new(x1, y1, (x2 - x1) as u32, (y2 - y1) as u32)
|
|
}
|
|
|
|
/// Translates the rectangle by an offset.
|
|
pub fn offset(&self, dx: i32, dy: i32) -> Rect {
|
|
Rect::new(
|
|
self.x.saturating_add(dx),
|
|
self.y.saturating_add(dy),
|
|
self.width,
|
|
self.height,
|
|
)
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for Rect {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(
|
|
f,
|
|
"Rect(x: {}, y: {}, w: {}, h: {})",
|
|
self.x, self.y, self.width, self.height
|
|
)
|
|
}
|
|
}
|