#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[must_use]
pub struct Point {
pub x: f32,
pub y: f32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[must_use]
pub struct Size {
pub w: f32,
pub h: f32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Rect {
pub x: f32,
pub y: f32,
pub w: f32,
pub h: f32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[must_use]
pub struct Insets {
pub left: f32,
pub top: f32,
pub right: f32,
pub bottom: f32,
}
pub const fn pt(x: f32, y: f32) -> Point {
Point { x, y }
}
pub const fn rect(x: f32, y: f32, w: f32, h: f32) -> Rect {
Rect { x, y, w, h }
}
impl Rect {
pub const EMPTY: Rect = rect(0.0, 0.0, 0.0, 0.0);
pub const EVERYTHING: Rect = rect(-1e9, -1e9, 2e9, 2e9);
pub fn right(&self) -> f32 {
self.x + self.w
}
pub fn bottom(&self) -> f32 {
self.y + self.h
}
pub fn size(&self) -> Size {
Size { w: self.w, h: self.h }
}
pub fn is_empty(&self) -> bool {
self.w <= 0.0 || self.h <= 0.0
}
#[inline(always)]
pub fn contains(&self, p: Point) -> bool {
(p.x >= self.x) & (p.x < self.right()) & (p.y >= self.y) & (p.y < self.bottom())
}
#[must_use]
pub fn intersect(&self, o: Rect) -> Rect {
let (x, y) = (self.x.max(o.x), self.y.max(o.y));
rect(x, y, (self.right().min(o.right()) - x).max(0.0), (self.bottom().min(o.bottom()) - y).max(0.0))
}
#[must_use]
pub fn union(&self, o: Rect) -> Rect {
if self.is_empty() {
return o;
}
if o.is_empty() {
return *self;
}
let (x, y) = (self.x.min(o.x), self.y.min(o.y));
rect(x, y, self.right().max(o.right()) - x, self.bottom().max(o.bottom()) - y)
}
#[must_use]
pub fn inset(&self, by: Insets) -> Rect {
rect(self.x + by.left, self.y + by.top, (self.w - by.left - by.right).max(0.0), (self.h - by.top - by.bottom).max(0.0))
}
#[must_use]
pub fn snap_to_pixels(&self, scale: f32) -> Rect {
let (x, y) = ((self.x * scale).floor() / scale, (self.y * scale).floor() / scale);
rect(x, y, (self.right() * scale).ceil() / scale - x, (self.bottom() * scale).ceil() / scale - y)
}
#[must_use]
pub fn grow(&self, by: f32) -> Rect {
rect(self.x - by, self.y - by, self.w + 2.0 * by, self.h + 2.0 * by)
}
#[must_use]
pub fn moved(&self, dx: f32, dy: f32) -> Rect {
rect(self.x + dx, self.y + dy, self.w, self.h)
}
}
impl Insets {
pub const ZERO: Insets = Insets::all(0.0);
pub const fn all(v: f32) -> Self {
Insets { left: v, top: v, right: v, bottom: v }
}
pub const fn xy(x: f32, y: f32) -> Self {
Insets { left: x, top: y, right: x, bottom: y }
}
pub fn horizontal(&self) -> f32 {
self.left + self.right
}
pub fn vertical(&self) -> f32 {
self.top + self.bottom
}
}
impl From<f32> for Insets {
fn from(v: f32) -> Self {
Insets::all(v)
}
}
impl From<(f32, f32)> for Insets {
fn from((x, y): (f32, f32)) -> Self {
Insets::xy(x, y)
}
}
impl From<(f32, f32, f32, f32)> for Insets {
fn from((left, top, right, bottom): (f32, f32, f32, f32)) -> Self {
Insets { left, top, right, bottom }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Corners {
pub top_left: f32,
pub top_right: f32,
pub bottom_right: f32,
pub bottom_left: f32,
}
impl From<f32> for Corners {
fn from(r: f32) -> Self {
Corners { top_left: r, top_right: r, bottom_right: r, bottom_left: r }
}
}
impl From<(f32, f32, f32, f32)> for Corners {
fn from((top_left, top_right, bottom_right, bottom_left): (f32, f32, f32, f32)) -> Self {
Corners { top_left, top_right, bottom_right, bottom_left }
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[must_use]
pub struct Transform(pub [f32; 6]);
impl Default for Transform {
fn default() -> Self {
Transform::new()
}
}
impl Transform {
pub const fn new() -> Self {
Transform([1.0, 0.0, 0.0, 1.0, 0.0, 0.0])
}
pub fn then(self, next: Transform) -> Self {
let ([a, b, c, d, e, f], [p, q, r, s, t, u]) = (self.0, next.0);
Transform([p * a + r * b, q * a + s * b, p * c + r * d, q * c + s * d, p * e + r * f + t, q * e + s * f + u])
}
pub fn rotate(self, degrees: f32) -> Self {
let (sin, cos) = degrees.to_radians().sin_cos();
self.then(Transform([cos, sin, -sin, cos, 0.0, 0.0]))
}
pub fn scale(self, x: f32, y: f32) -> Self {
self.then(Transform([x, 0.0, 0.0, y, 0.0, 0.0]))
}
pub fn translate(self, x: f32, y: f32) -> Self {
self.then(Transform([1.0, 0.0, 0.0, 1.0, x, y]))
}
pub fn skew(self, x: f32, y: f32) -> Self {
self.then(Transform([1.0, y.to_radians().tan(), x.to_radians().tan(), 1.0, 0.0, 0.0]))
}
pub fn inverse(self) -> Option<Self> {
let [a, b, c, d, e, f] = self.0;
let area = a * d - b * c;
area.is_normal().then(|| Transform([d / area, -b / area, -c / area, a / area, (c * f - d * e) / area, (b * e - a * f) / area]))
}
#[inline]
pub fn apply(self, p: Point) -> Point {
let [a, b, c, d, e, f] = self.0;
pt(a * p.x + c * p.y + e, b * p.x + d * p.y + f)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn near(p: Point, q: Point) -> bool {
(p.x - q.x).abs() < 1e-4 && (p.y - q.y).abs() < 1e-4
}
#[test]
fn a_transform_does_its_steps_in_the_order_they_are_said() {
let p = pt(3.0, 1.0);
assert!(near(Transform::new().rotate(90.0).apply(pt(1.0, 0.0)), pt(0.0, 1.0)));
assert!(near(Transform::new().rotate(90.0).apply(pt(0.0, 1.0)), pt(-1.0, 0.0)));
assert!(near(Transform::new().translate(1.0, 2.0).scale(2.0, 2.0).apply(p), pt(8.0, 6.0)));
assert!(near(Transform::new().scale(2.0, 2.0).translate(1.0, 2.0).apply(p), pt(7.0, 4.0)));
let round_middle = Transform::new().translate(-5.0, -5.0).rotate(180.0).translate(5.0, 5.0);
assert!(near(round_middle.apply(pt(6.0, 5.0)), pt(4.0, 5.0)) && near(round_middle.apply(pt(5.0, 5.0)), pt(5.0, 5.0)));
assert!(near(Transform::new().skew(45.0, 0.0).apply(pt(0.0, 2.0)), pt(2.0, 2.0)));
assert!(near(Transform::new().skew(0.0, 45.0).apply(pt(2.0, 0.0)), pt(2.0, 2.0)));
let (one, two) = (Transform::new().rotate(30.0).scale(2.0, 0.5), Transform::new().skew(10.0, 0.0).translate(4.0, -1.0));
let joined = one.then(two);
let stepped = Transform::new().rotate(30.0).scale(2.0, 0.5).skew(10.0, 0.0).translate(4.0, -1.0);
assert!(near(joined.apply(p), two.apply(one.apply(p))) && near(stepped.apply(p), joined.apply(p)));
let back = joined.inverse().unwrap();
assert!([pt(0.0, 0.0), p, pt(-7.0, 12.5)].iter().all(|q| near(back.apply(joined.apply(*q)), *q)));
assert_eq!(Transform::new().scale(1.0, 0.0).inverse(), None);
assert_eq!(Transform::default(), Transform::new());
}
#[test]
fn corners_are_said_by_one_number_or_four() {
assert_eq!(Corners::from(3.0), Corners { top_left: 3.0, top_right: 3.0, bottom_right: 3.0, bottom_left: 3.0 });
assert_eq!(Corners::from((1.0, 2.0, 3.0, 4.0)), Corners { top_left: 1.0, top_right: 2.0, bottom_right: 3.0, bottom_left: 4.0 });
}
}