use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Vec2 {
pub x: f32,
pub y: f32,
}
impl Vec2 {
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
pub fn plus(self, o: Self) -> Self {
Self::new(self.x + o.x, self.y + o.y)
}
pub fn minus(self, o: Self) -> Self {
Self::new(self.x - o.x, self.y - o.y)
}
pub fn scale(self, k: f32) -> Self {
Self::new(self.x * k, self.y * k)
}
pub fn lerp(self, o: Self, t: f32) -> Self {
Self::new(self.x + (o.x - self.x) * t, self.y + (o.y - self.y) * t)
}
pub fn length(self) -> f32 {
self.x.hypot(self.y)
}
pub fn distance(self, o: Self) -> f32 {
self.minus(o).length()
}
pub fn snap(self, grid: f32) -> Self {
if grid <= 0.0 {
return self;
}
Self::new(
(self.x / grid).round() * grid,
(self.y / grid).round() * grid,
)
}
}
impl std::ops::Add for Vec2 {
type Output = Self;
fn add(self, o: Self) -> Self {
self.plus(o)
}
}
impl std::ops::Sub for Vec2 {
type Output = Self;
fn sub(self, o: Self) -> Self {
self.minus(o)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Size2 {
pub w: f32,
pub h: f32,
}
impl Size2 {
pub const fn new(w: f32, h: f32) -> Self {
Self { w, h }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Rect {
pub min: Vec2,
pub max: Vec2,
}
impl Rect {
pub fn from_origin_size(origin: Vec2, size: Size2) -> Self {
Self {
min: origin,
max: Vec2::new(origin.x + size.w, origin.y + size.h),
}
}
pub fn from_corners(a: Vec2, b: Vec2) -> Self {
Self {
min: Vec2::new(a.x.min(b.x), a.y.min(b.y)),
max: Vec2::new(a.x.max(b.x), a.y.max(b.y)),
}
}
pub fn from_points(points: &[Vec2]) -> Option<Self> {
let first = *points.first()?;
Some(points.iter().skip(1).fold(
Self {
min: first,
max: first,
},
|r, p| r.union(Self { min: *p, max: *p }),
))
}
pub fn width(&self) -> f32 {
self.max.x - self.min.x
}
pub fn height(&self) -> f32 {
self.max.y - self.min.y
}
pub fn center(&self) -> Vec2 {
self.min.lerp(self.max, 0.5)
}
pub fn contains(&self, p: Vec2) -> bool {
p.x >= self.min.x && p.x <= self.max.x && p.y >= self.min.y && p.y <= self.max.y
}
pub fn contains_rect(&self, o: &Rect) -> bool {
self.contains(o.min) && self.contains(o.max)
}
pub fn intersects(&self, o: &Rect) -> bool {
self.min.x <= o.max.x
&& self.max.x >= o.min.x
&& self.min.y <= o.max.y
&& self.max.y >= o.min.y
}
pub fn union(&self, o: Rect) -> Rect {
Rect {
min: Vec2::new(self.min.x.min(o.min.x), self.min.y.min(o.min.y)),
max: Vec2::new(self.max.x.max(o.max.x), self.max.y.max(o.max.y)),
}
}
pub fn inflate(&self, d: f32) -> Rect {
Rect {
min: self.min.minus(Vec2::new(d, d)),
max: self.max.plus(Vec2::new(d, d)),
}
}
}
pub fn distance_to_segment(p: Vec2, a: Vec2, b: Vec2) -> f32 {
let ab = b.minus(a);
let len2 = ab.x * ab.x + ab.y * ab.y;
if len2 <= f32::EPSILON {
return p.distance(a);
}
let t = (((p.x - a.x) * ab.x + (p.y - a.y) * ab.y) / len2).clamp(0.0, 1.0);
p.distance(a.lerp(b, t))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rect_from_corners_normalises() {
let r = Rect::from_corners(Vec2::new(10.0, 10.0), Vec2::new(0.0, 5.0));
assert_eq!(r.min, Vec2::new(0.0, 5.0));
assert_eq!(r.max, Vec2::new(10.0, 10.0));
}
#[test]
fn snap_rounds_to_grid() {
assert_eq!(Vec2::new(14.0, 26.0).snap(20.0), Vec2::new(20.0, 20.0));
assert_eq!(Vec2::new(3.3, 4.4).snap(0.0), Vec2::new(3.3, 4.4));
}
#[test]
fn segment_distance() {
let d = distance_to_segment(Vec2::new(5.0, 3.0), Vec2::ZERO, Vec2::new(10.0, 0.0));
assert!((d - 3.0).abs() < 1e-5);
let d = distance_to_segment(Vec2::new(-4.0, 3.0), Vec2::ZERO, Vec2::new(10.0, 0.0));
assert!((d - 5.0).abs() < 1e-5);
}
}