use std::ops::{Add, Sub};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct PointF {
pub x: f32,
pub y: f32,
}
impl PointF {
pub const ZERO: PointF = PointF::new(0.0, 0.0);
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
impl Add for PointF {
type Output = PointF;
fn add(self, other: PointF) -> PointF {
PointF::new(self.x + other.x, self.y + other.y)
}
}
impl Sub for PointF {
type Output = PointF;
fn sub(self, other: PointF) -> PointF {
PointF::new(self.x - other.x, self.y - other.y)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SizeF {
pub width: f32,
pub height: f32,
}
impl SizeF {
pub const ZERO: SizeF = SizeF::new(0.0, 0.0);
pub const fn new(width: f32, height: f32) -> Self {
Self { width, height }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct RectF {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl RectF {
pub const ZERO: RectF = RectF::new(0.0, 0.0, 0.0, 0.0);
pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
Self { x, y, width, height }
}
pub const fn from_size(size: SizeF) -> Self {
Self::new(0.0, 0.0, size.width, size.height)
}
pub fn right(&self) -> f32 {
self.x + self.width
}
pub fn bottom(&self) -> f32 {
self.y + self.height
}
pub fn origin(&self) -> PointF {
PointF::new(self.x, self.y)
}
pub fn size(&self) -> SizeF {
SizeF::new(self.width, self.height)
}
pub fn center(&self) -> PointF {
PointF::new(self.x + self.width / 2.0, self.y + self.height / 2.0)
}
pub fn is_empty(&self) -> bool {
self.width <= 0.0 || self.height <= 0.0
}
pub fn contains(&self, p: PointF) -> bool {
p.x >= self.x && p.x < self.right() && p.y >= self.y && p.y < self.bottom()
}
pub fn union(&self, other: RectF) -> RectF {
let (x, y) = (self.x.min(other.x), self.y.min(other.y));
let (r, b) = (self.right().max(other.right()), self.bottom().max(other.bottom()));
RectF::new(x, y, r - x, b - y)
}
pub fn contains_rect(&self, other: RectF) -> bool {
other.x >= self.x && other.y >= self.y && other.right() <= self.right() && other.bottom() <= self.bottom()
}
pub fn intersects(&self, other: RectF) -> bool {
self.x < other.right() && other.x < self.right() && self.y < other.bottom() && other.y < self.bottom()
}
pub fn from_points(a: PointF, b: PointF) -> Self {
Self::new(a.x.min(b.x), a.y.min(b.y), (a.x - b.x).abs(), (a.y - b.y).abs())
}
pub fn offset(self, dx: f32, dy: f32) -> Self {
Self::new(self.x + dx, self.y + dy, self.width, self.height)
}
pub fn inflate(self, d: f32) -> Self {
Self::new(self.x - d, self.y - d, self.width + 2.0 * d, self.height + 2.0 * d)
}
pub fn deflate(self, b: Bounds) -> Self {
Self::new(
self.x + b.left,
self.y + b.top,
(self.width - b.horizontal()).max(0.0),
(self.height - b.vertical()).max(0.0),
)
}
pub fn intersect(self, other: RectF) -> Option<RectF> {
let x = self.x.max(other.x);
let y = self.y.max(other.y);
let r = self.right().min(other.right());
let b = self.bottom().min(other.bottom());
(r > x && b > y).then(|| RectF::new(x, y, r - x, b - y))
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Bounds {
pub left: f32,
pub top: f32,
pub right: f32,
pub bottom: f32,
}
impl Bounds {
pub const ZERO: Bounds = Bounds::new(0.0, 0.0, 0.0, 0.0);
pub const fn new(left: f32, top: f32, right: f32, bottom: f32) -> Self {
Self { left, top, right, bottom }
}
pub const fn all(v: f32) -> Self {
Self::new(v, v, v, v)
}
pub const fn symmetric(h: f32, v: f32) -> Self {
Self::new(h, v, h, v)
}
pub fn scaled(self, factor: f32) -> Self {
Self::new(self.left * factor, self.top * factor, self.right * factor, self.bottom * factor)
}
pub fn horizontal(&self) -> f32 {
self.left + self.right
}
pub fn vertical(&self) -> f32 {
self.top + self.bottom
}
}
impl From<f32> for Bounds {
fn from(v: f32) -> Self {
Bounds::all(v)
}
}
impl From<(f32, f32)> for Bounds {
fn from((h, v): (f32, f32)) -> Self {
Bounds::symmetric(h, v)
}
}
impl From<[f32; 4]> for Bounds {
fn from([l, t, r, b]: [f32; 4]) -> Self {
Bounds::new(l, t, r, b)
}
}