#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Point {
pub x: f32,
pub y: f32,
}
impl Point {
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BBox {
pub x0: f32,
pub top: f32,
pub x1: f32,
pub bottom: f32,
}
impl BBox {
pub const fn new(x0: f32, top: f32, x1: f32, bottom: f32) -> Self {
Self {
x0,
top,
x1,
bottom,
}
}
pub fn width(&self) -> f32 {
self.x1 - self.x0
}
pub fn height(&self) -> f32 {
self.bottom - self.top
}
pub fn merge(&self, other: &Self) -> Self {
Self {
x0: self.x0.min(other.x0),
top: self.top.min(other.top),
x1: self.x1.max(other.x1),
bottom: self.bottom.max(other.bottom),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Matrix {
pub a: f32,
pub b: f32,
pub c: f32,
pub d: f32,
pub e: f32,
pub f: f32,
}
impl Matrix {
pub const IDENTITY: Matrix = Matrix {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
e: 0.0,
f: 0.0,
};
pub const fn new(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Self {
Self { a, b, c, d, e, f }
}
pub const fn translation(tx: f32, ty: f32) -> Self {
Self {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
e: tx,
f: ty,
}
}
pub const fn scale(sx: f32, sy: f32) -> Self {
Self {
a: sx,
b: 0.0,
c: 0.0,
d: sy,
e: 0.0,
f: 0.0,
}
}
pub fn then(self, other: Self) -> Self {
Self {
a: self.a * other.a + self.b * other.c,
b: self.a * other.b + self.b * other.d,
c: self.c * other.a + self.d * other.c,
d: self.c * other.b + self.d * other.d,
e: self.e * other.a + self.f * other.c + other.e,
f: self.e * other.b + self.f * other.d + other.f,
}
}
pub fn transform(&self, p: Point) -> Point {
Point {
x: self.a * p.x + self.c * p.y + self.e,
y: self.b * p.x + self.d * p.y + self.f,
}
}
pub fn pre_translate(self, tx: f32, ty: f32) -> Self {
Matrix::translation(tx, ty).then(self)
}
pub fn x_scale(&self) -> f32 {
(self.a * self.a + self.b * self.b).sqrt()
}
pub fn y_scale(&self) -> f32 {
(self.c * self.c + self.d * self.d).sqrt()
}
pub fn is_upright(&self) -> bool {
let scale = self.x_scale().max(self.y_scale());
if !scale.is_finite() || scale <= 1e-6 || self.d == 0.0 {
return false;
}
self.b.abs() / scale < 0.01 && self.c.abs() / scale < 0.01 && self.a > 0.0
}
}
impl Default for Matrix {
fn default() -> Self {
Self::IDENTITY
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn identity_times_identity_is_identity() {
let r = Matrix::IDENTITY.then(Matrix::IDENTITY);
assert_eq!(r, Matrix::IDENTITY);
}
#[test]
fn translation_transforms_point() {
let m = Matrix::translation(3.0, 5.0);
assert_eq!(m.transform(Point::new(1.0, 2.0)), Point::new(4.0, 7.0));
}
#[test]
fn scale_transforms_point() {
let m = Matrix::scale(2.0, 3.0);
assert_eq!(m.transform(Point::new(4.0, 5.0)), Point::new(8.0, 15.0));
}
#[test]
fn then_applies_self_first_then_other() {
let m = Matrix::translation(1.0, 2.0).then(Matrix::scale(2.0, 3.0));
assert_eq!(m.transform(Point::new(1.0, 1.0)), Point::new(4.0, 9.0));
}
#[test]
fn pre_translate_adds_to_translation_column() {
let m = Matrix::scale(2.0, 2.0).pre_translate(1.0, 0.0);
assert_eq!(m.transform(Point::new(0.0, 0.0)), Point::new(2.0, 0.0));
}
#[test]
fn upright_identity_is_upright() {
assert!(Matrix::IDENTITY.is_upright());
}
#[test]
fn rotated_matrix_is_not_upright() {
let s = std::f32::consts::FRAC_1_SQRT_2;
let m = Matrix::new(s, s, -s, s, 0.0, 0.0);
assert!(!m.is_upright());
}
#[test]
fn x_scale_returns_horizontal_factor_when_matrix_is_pure_scale() {
let m = Matrix::scale(3.0, 5.0);
assert!((m.x_scale() - 3.0).abs() < 1e-5);
}
#[test]
fn bbox_merge_returns_enclosing_box() {
let a = BBox::new(0.0, 0.0, 5.0, 5.0);
let b = BBox::new(3.0, 3.0, 10.0, 10.0);
assert_eq!(a.merge(&b), BBox::new(0.0, 0.0, 10.0, 10.0));
}
}