cleanup Engine API and generalize Inset/Outset

This commit is contained in:
🪞👃🪞 2024-09-13 21:27:40 +03:00
parent 4e0eb0c335
commit 0737769232
6 changed files with 118 additions and 91 deletions

View file

@ -1,18 +1,18 @@
use crate::*;
pub trait Point<N: Number> {
pub trait Size<N: Number> {
fn x (&self) -> N;
fn y (&self) -> N;
fn w (&self) -> N { self.x() }
fn h (&self) -> N { self.y() }
}
impl<N: Number> Point<N> for (N, N) {
impl<N: Number> Size<N> for (N, N) {
fn x (&self) -> N { self.0 }
fn y (&self) -> N { self.1 }
}
impl<N: Number> Point<N> for [N;2] {
impl<N: Number> Size<N> for [N;2] {
fn x (&self) -> N { self[0] }
fn y (&self) -> N { self[1] }
}
@ -22,8 +22,11 @@ pub trait Area<N: Number>: Copy {
fn y (&self) -> N;
fn w (&self) -> N;
fn h (&self) -> N;
fn x2 (&self) -> N { self.x() + self.w() - 1.into() }
fn y2 (&self) -> N { self.y() + self.h() - 1.into() }
fn x2 (&self) -> N { self.x() + self.w() }
fn y2 (&self) -> N { self.y() + self.h() }
fn wh (&self) -> [N;2] {
[self.w(), self.h()]
}
fn xywh (&self) -> [N;4] {
[self.x(), self.y(), self.w(), self.h()]
}
@ -37,6 +40,9 @@ pub trait Area<N: Number>: Copy {
Ok(self)
}
}
fn clip (&self, wh: impl Size<N>) -> [N;4] {
[self.x(), self.y(), wh.w(), wh.h()]
}
}
impl<N: Number> Area<N> for (N, N, N, N) {