spans now good (other than direction) + refactor
This commit is contained in:
59
src/layout/vec2.rs
Normal file
59
src/layout/vec2.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use crate::util::{F32Util, impl_op};
|
||||
use std::ops::*;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct Vec2 {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
pub const fn vec2(x: f32, y: f32) -> Vec2 {
|
||||
Vec2::new(x, y)
|
||||
}
|
||||
|
||||
impl Vec2 {
|
||||
pub const ZERO: Self = Self::new(0.0, 0.0);
|
||||
pub const ONE: Self = Self::new(1.0, 1.0);
|
||||
|
||||
pub const fn new(x: f32, y: f32) -> Self {
|
||||
Self { x, y }
|
||||
}
|
||||
|
||||
pub const fn lerp(self, from: impl const Into<Self>, to: impl const Into<Self>) -> Self {
|
||||
let from = from.into();
|
||||
let to = to.into();
|
||||
Self {
|
||||
x: self.x.lerp(from.x, to.x),
|
||||
y: self.y.lerp(from.y, to.y),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl const From<f32> for Vec2 {
|
||||
fn from(v: f32) -> Self {
|
||||
Self { x: v, y: v }
|
||||
}
|
||||
}
|
||||
|
||||
// this version looks kinda cool... is it more readable? more annoying to copy and change though
|
||||
impl_op!(impl Add for Vec2: add x y);
|
||||
impl_op!(Vec2 Sub sub; x y);
|
||||
impl_op!(Vec2 Mul mul; x y);
|
||||
impl_op!(Vec2 Div div; x y);
|
||||
|
||||
impl Neg for Vec2 {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(mut self) -> Self::Output {
|
||||
self.x = -self.x;
|
||||
self.y = -self.y;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(f32, f32)> for Vec2 {
|
||||
fn from((x, y): (f32, f32)) -> Self {
|
||||
Self { x, y }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user