use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Vec2 {
pub x: f32,
pub y: f32,
}
pub const fn vec2(x: f32, y: f32) -> Vec2 {
Vec2 { x, y }
}
impl Vec2 {
pub const ZERO: Vec2 = vec2(0.0, 0.0);
pub const fn splat(value: f32) -> Self {
vec2(value, value)
}
pub fn length(self) -> f32 {
self.x.hypot(self.y)
}
pub fn normalized(self) -> Self {
let l = self.length();
if l > 1e-6 { self / l } else { Vec2::ZERO }
}
pub fn dot(self, other: Vec2) -> f32 {
self.x * other.x + self.y * other.y
}
pub fn min(self, other: Vec2) -> Self {
vec2(self.x.min(other.x), self.y.min(other.y))
}
pub fn max(self, other: Vec2) -> Self {
vec2(self.x.max(other.x), self.y.max(other.y))
}
pub fn abs(self) -> Self {
vec2(self.x.abs(), self.y.abs())
}
pub fn lerp(self, other: Vec2, t: f32) -> Self {
self + (other - self) * t
}
}
impl Add for Vec2 {
type Output = Vec2;
fn add(self, other: Vec2) -> Vec2 {
vec2(self.x + other.x, self.y + other.y)
}
}
impl Sub for Vec2 {
type Output = Vec2;
fn sub(self, other: Vec2) -> Vec2 {
vec2(self.x - other.x, self.y - other.y)
}
}
impl Mul<f32> for Vec2 {
type Output = Vec2;
fn mul(self, factor: f32) -> Vec2 {
vec2(self.x * factor, self.y * factor)
}
}
impl Div<f32> for Vec2 {
type Output = Vec2;
fn div(self, divisor: f32) -> Vec2 {
vec2(self.x / divisor, self.y / divisor)
}
}
impl Neg for Vec2 {
type Output = Vec2;
fn neg(self) -> Vec2 {
vec2(-self.x, -self.y)
}
}
impl AddAssign for Vec2 {
fn add_assign(&mut self, other: Vec2) {
*self = *self + other;
}
}
impl SubAssign for Vec2 {
fn sub_assign(&mut self, other: Vec2) {
*self = *self - other;
}
}
pub fn lerp(a: f32, b: f32, t: f32) -> f32 {
a + (b - a) * t
}
pub fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
let t = ((x - edge0) / (edge1 - edge0)).clamp(0.0, 1.0);
t * t * (3.0 - 2.0 * t)
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Rect {
pub min: Vec2,
pub max: Vec2,
}
pub const fn rect(x: f32, y: f32, width: f32, height: f32) -> Rect {
Rect { min: vec2(x, y), max: vec2(x + width, y + height) }
}
impl Rect {
pub const EVERYTHING: Rect = Rect { min: vec2(-1e6, -1e6), max: vec2(1e6, 1e6) };
pub const NOTHING: Rect = Rect { min: vec2(1e6, 1e6), max: vec2(-1e6, -1e6) };
pub fn from_min_size(min: Vec2, size: Vec2) -> Self {
Rect { min, max: min + size }
}
pub fn from_center_size(center: Vec2, size: Vec2) -> Self {
Rect { min: center - size * 0.5, max: center + size * 0.5 }
}
pub fn width(&self) -> f32 {
self.max.x - self.min.x
}
pub fn height(&self) -> f32 {
self.max.y - self.min.y
}
pub fn size(&self) -> Vec2 {
self.max - self.min
}
pub fn center(&self) -> Vec2 {
(self.min + self.max) * 0.5
}
pub fn left(&self) -> f32 {
self.min.x
}
pub fn right(&self) -> f32 {
self.max.x
}
pub fn top(&self) -> f32 {
self.min.y
}
pub fn bottom(&self) -> f32 {
self.max.y
}
pub fn contains(&self, point: Vec2) -> bool {
point.x >= self.min.x && point.x < self.max.x && point.y >= self.min.y && point.y < self.max.y
}
pub fn intersects(&self, other: &Rect) -> bool {
self.min.x < other.max.x
&& other.min.x < self.max.x
&& self.min.y < other.max.y
&& other.min.y < self.max.y
}
pub fn intersect(&self, other: Rect) -> Rect {
Rect { min: self.min.max(other.min), max: self.max.min(other.max) }
}
pub fn union(&self, other: Rect) -> Rect {
Rect { min: self.min.min(other.min), max: self.max.max(other.max) }
}
pub fn is_positive(&self) -> bool {
self.max.x > self.min.x && self.max.y > self.min.y
}
pub fn expand(&self, amount: f32) -> Rect {
self.expand2(Vec2::splat(amount))
}
pub fn expand2(&self, amount: Vec2) -> Rect {
Rect { min: self.min - amount, max: self.max + amount }
}
pub fn shrink(&self, amount: f32) -> Rect {
self.expand(-amount)
}
pub fn shrink2(&self, amount: Vec2) -> Rect {
self.expand2(-amount)
}
pub fn translate(&self, offset: Vec2) -> Rect {
Rect { min: self.min + offset, max: self.max + offset }
}
pub fn scale_from_center(&self, scale_x: f32, scale_y: f32) -> Rect {
Rect::from_center_size(self.center(), vec2(self.width() * scale_x, self.height() * scale_y))
}
pub fn lerp(&self, other: &Rect, t: f32) -> Rect {
Rect { min: self.min.lerp(other.min, t), max: self.max.lerp(other.max, t) }
}
pub fn snap(&self, pixels_per_point: f32) -> Rect {
let round = |v: f32| (v * pixels_per_point).round() / pixels_per_point;
Rect {
min: vec2(round(self.min.x), round(self.min.y)),
max: vec2(round(self.max.x), round(self.max.y)),
}
}
pub fn clamp_inside(&self, outer: Rect) -> Rect {
let mut d = Vec2::ZERO;
if self.max.x > outer.max.x {
d.x = outer.max.x - self.max.x;
}
if self.min.x + d.x < outer.min.x {
d.x = outer.min.x - self.min.x;
}
if self.max.y > outer.max.y {
d.y = outer.max.y - self.max.y;
}
if self.min.y + d.y < outer.min.y {
d.y = outer.min.y - self.min.y;
}
self.translate(d)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn union_with_nothing_is_identity() {
let r = rect(1.0, 2.0, 3.0, 4.0);
assert_eq!(Rect::NOTHING.union(r), r);
}
#[test]
fn clamp_inside_moves_without_resizing() {
let r = rect(90.0, -5.0, 20.0, 10.0).clamp_inside(rect(0.0, 0.0, 100.0, 100.0));
assert_eq!(r, rect(80.0, 0.0, 20.0, 10.0));
}
}