use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Vec2 {
pub(crate) x: f32,
pub(crate) y: f32,
}
impl Vec2 {
pub const ZERO: Self = Self::new(0.0, 0.0);
pub const ONE: Self = Self::new(1.0, 1.0);
pub const X: Self = Self::new(1.0, 0.0);
pub const Y: Self = Self::new(0.0, 1.0);
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
pub const fn x(self) -> f32 {
self.x
}
pub const fn y(self) -> f32 {
self.y
}
pub fn splat(value: f32) -> Self {
Self::new(value, value)
}
pub fn length_squared(self) -> f32 {
self.x * self.x + self.y * self.y
}
pub fn length(self) -> f32 {
self.x.hypot(self.y)
}
pub fn dot(self, other: Self) -> f32 {
self.x * other.x + self.y * other.y
}
pub fn normalized(self) -> Self {
if !self.is_finite() {
Self::ZERO
} else {
let maximum_component = self.x.abs().max(self.y.abs());
if maximum_component <= f32::EPSILON {
return Self::ZERO;
}
let scaled = self / maximum_component;
let length = scaled.length();
if length <= f32::EPSILON {
Self::ZERO
} else {
scaled / length
}
}
}
pub fn perp(self) -> Self {
Self::new(-self.y, self.x)
}
pub fn lerp(self, end: Self, amount: f32) -> Self {
if !self.is_finite() || !end.is_finite() || !amount.is_finite() {
return self;
}
Self::new(
stable_lerp_component(self.x, end.x, amount),
stable_lerp_component(self.y, end.y, amount),
)
}
pub fn is_finite(self) -> bool {
self.x.is_finite() && self.y.is_finite()
}
}
#[cfg(test)]
mod tests {
use super::{Rect, Vec2};
#[test]
fn normalization_preserves_large_finite_direction() {
let normalized = Vec2::new(3.0e38, 3.0e38).normalized();
assert!(normalized.is_finite());
assert!((normalized.length() - 1.0).abs() < 0.0001);
assert!(normalized.x > 0.0 && normalized.y > 0.0);
}
#[test]
fn interpolation_and_rect_center_stay_finite_at_extremes() {
let midpoint = Vec2::splat(f32::MAX).lerp(Vec2::splat(-f32::MAX), 0.5);
assert_eq!(midpoint, Vec2::ZERO);
let rect = Rect::new(Vec2::splat(f32::MAX), Vec2::splat(f32::MAX));
assert_eq!(rect.center(), Vec2::splat(f32::MAX));
}
}
impl Add for Vec2 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::new(self.x + rhs.x, self.y + rhs.y)
}
}
impl AddAssign for Vec2 {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
impl Sub for Vec2 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self::new(self.x - rhs.x, self.y - rhs.y)
}
}
impl SubAssign for Vec2 {
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
impl Mul<f32> for Vec2 {
type Output = Self;
fn mul(self, rhs: f32) -> Self::Output {
Self::new(self.x * rhs, self.y * rhs)
}
}
impl MulAssign<f32> for Vec2 {
fn mul_assign(&mut self, rhs: f32) {
*self = *self * rhs;
}
}
impl Div<f32> for Vec2 {
type Output = Self;
fn div(self, rhs: f32) -> Self::Output {
Self::new(self.x / rhs, self.y / rhs)
}
}
impl DivAssign<f32> for Vec2 {
fn div_assign(&mut self, rhs: f32) {
*self = *self / rhs;
}
}
impl Neg for Vec2 {
type Output = Self;
fn neg(self) -> Self::Output {
Self::new(-self.x, -self.y)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
pub(crate) min: Vec2,
pub(crate) max: Vec2,
}
impl Rect {
pub fn new(min: Vec2, max: Vec2) -> Self {
Self { min, max }
}
pub const fn min(self) -> Vec2 {
self.min
}
pub const fn max(self) -> Vec2 {
self.max
}
pub fn from_center_size(center: Vec2, size: Vec2) -> Self {
let half = size * 0.5;
Self::new(center - half, center + half)
}
pub fn from_min_size(min: Vec2, size: Vec2) -> Self {
Self::new(min, min + size)
}
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 {
Vec2::new(self.width(), self.height())
}
pub fn center(self) -> Vec2 {
Vec2::new(
stable_midpoint(self.min.x, self.max.x),
stable_midpoint(self.min.y, self.max.y),
)
}
pub fn expand(self, amount: f32) -> Self {
Self::new(
self.min - Vec2::splat(amount),
self.max + Vec2::splat(amount),
)
}
pub fn normalized(self) -> Self {
Self::new(
Vec2::new(self.min.x.min(self.max.x), self.min.y.min(self.max.y)),
Vec2::new(self.min.x.max(self.max.x), self.min.y.max(self.max.y)),
)
}
}
fn stable_lerp_component(start: f32, end: f32, amount: f32) -> f32 {
let value = f64::from(start) + (f64::from(end) - f64::from(start)) * f64::from(amount);
value.clamp(f64::from(f32::MIN), f64::from(f32::MAX)) as f32
}
fn stable_midpoint(first: f32, second: f32) -> f32 {
((f64::from(first) + f64::from(second)) * 0.5) as f32
}