use crate::f32::{Vec2Mask, Vec3};
use core::{f32, fmt, ops::*};
const ZERO: Vec2 = const_vec2!([0.0; 2]);
const ONE: Vec2 = const_vec2!([1.0; 2]);
const X_AXIS: Vec2 = const_vec2!([1.0, 0.0]);
const Y_AXIS: Vec2 = const_vec2!([0.0, 1.0]);
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Default)]
#[repr(C)]
pub struct Vec2(pub(crate) f32, pub(crate) f32);
#[inline]
pub fn vec2(x: f32, y: f32) -> Vec2 {
Vec2(x, y)
}
impl Vec2 {
#[inline]
pub fn is_nan(self) -> Vec2Mask {
Vec2Mask::new(self.0.is_nan(), self.1.is_nan())
}
#[deprecated(since = "0.9.5", note = "please use `Vec2::signum` instead")]
#[inline(always)]
pub fn sign(self) -> Self {
self.signum()
}
#[inline]
pub fn signum(self) -> Self {
Self(self.0.signum(), self.1.signum())
}
#[deprecated(since = "0.9.5", note = "please use `Vec2::recip` instead")]
#[inline(always)]
pub fn reciprocal(self) -> Self {
self.recip()
}
#[inline]
pub fn recip(self) -> Self {
Self(self.0.recip(), self.1.recip())
}
#[inline]
pub fn lerp(self, other: Self, s: f32) -> Self {
self + ((other - self) * s)
}
#[inline]
pub fn is_normalized(self) -> bool {
is_normalized!(self)
}
#[inline]
pub fn abs_diff_eq(self, other: Self, max_abs_diff: f32) -> bool {
abs_diff_eq!(self, other, max_abs_diff)
}
#[inline]
pub fn new(x: f32, y: f32) -> Vec2 {
Vec2(x, y)
}
#[inline]
pub const fn zero() -> Vec2 {
ZERO
}
#[inline]
pub const fn one() -> Vec2 {
ONE
}
#[inline]
pub const fn unit_x() -> Vec2 {
X_AXIS
}
#[inline]
pub const fn unit_y() -> Vec2 {
Y_AXIS
}
#[inline]
pub fn splat(v: f32) -> Vec2 {
Vec2(v, v)
}
#[inline]
pub fn extend(self, z: f32) -> Vec3 {
Vec3::new(self.0, self.1, z)
}
#[inline]
pub fn x(self) -> f32 {
self.0
}
#[inline]
pub fn y(self) -> f32 {
self.1
}
#[inline]
pub fn x_mut(&mut self) -> &mut f32 {
&mut self.0
}
#[inline]
pub fn y_mut(&mut self) -> &mut f32 {
&mut self.1
}
#[inline]
pub fn set_x(&mut self, x: f32) {
self.0 = x;
}
#[inline]
pub fn set_y(&mut self, y: f32) {
self.1 = y;
}
#[inline]
pub fn dot(self, other: Vec2) -> f32 {
(self.0 * other.0) + (self.1 * other.1)
}
#[inline]
pub fn length(self) -> f32 {
self.dot(self).sqrt()
}
#[inline]
pub fn length_squared(self) -> f32 {
self.dot(self)
}
#[deprecated(since = "0.9.5", note = "please use `Vec2::length_recip` instead")]
#[inline(always)]
pub fn length_reciprocal(self) -> f32 {
self.length_recip()
}
#[inline]
pub fn length_recip(self) -> f32 {
self.length().recip()
}
#[inline]
pub fn normalize(self) -> Vec2 {
self * self.length_recip()
}
#[inline]
pub fn min(self, other: Vec2) -> Vec2 {
Vec2(self.0.min(other.0), self.1.min(other.1))
}
#[inline]
pub fn max(self, other: Vec2) -> Vec2 {
Vec2(self.0.max(other.0), self.1.max(other.1))
}
#[inline]
pub fn min_element(self) -> f32 {
self.0.min(self.1)
}
#[inline]
pub fn max_element(self) -> f32 {
self.0.max(self.1)
}
#[inline]
pub fn cmpeq(self, other: Vec2) -> Vec2Mask {
Vec2Mask::new(self.0.eq(&other.0), self.1.eq(&other.1))
}
#[inline]
pub fn cmpne(self, other: Vec2) -> Vec2Mask {
Vec2Mask::new(self.0.ne(&other.0), self.1.ne(&other.1))
}
#[inline]
pub fn cmpge(self, other: Vec2) -> Vec2Mask {
Vec2Mask::new(self.0.ge(&other.0), self.1.ge(&other.1))
}
#[inline]
pub fn cmpgt(self, other: Vec2) -> Vec2Mask {
Vec2Mask::new(self.0.gt(&other.0), self.1.gt(&other.1))
}
#[inline]
pub fn cmple(self, other: Vec2) -> Vec2Mask {
Vec2Mask::new(self.0.le(&other.0), self.1.le(&other.1))
}
#[inline]
pub fn cmplt(self, other: Vec2) -> Vec2Mask {
Vec2Mask::new(self.0.lt(&other.0), self.1.lt(&other.1))
}
#[inline]
pub fn from_slice_unaligned(slice: &[f32]) -> Self {
Self(slice[0], slice[1])
}
#[inline]
pub fn write_to_slice_unaligned(self, slice: &mut [f32]) {
slice[0] = self.0;
slice[1] = self.1;
}
#[inline]
pub fn abs(self) -> Self {
Self(self.0.abs(), self.1.abs())
}
#[inline]
pub fn round(self) -> Self {
Self(self.0.round(), self.1.round())
}
#[inline]
pub fn floor(self) -> Self {
Self(self.0.floor(), self.1.floor())
}
#[inline]
pub fn ceil(self) -> Self {
Self(self.0.ceil(), self.1.ceil())
}
#[inline]
pub fn perp_dot(self, other: Vec2) -> f32 {
(self.0 * other.1) - (self.1 * other.0)
}
#[inline]
pub fn angle_between(self, other: Self) -> f32 {
let angle = crate::f32::funcs::scalar_acos(
self.dot(other) / (self.dot(self) * other.dot(other)).sqrt(),
);
if self.perp_dot(other) < 0.0 {
-angle
} else {
angle
}
}
}
impl fmt::Display for Vec2 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}, {}]", self.0, self.1)
}
}
impl Div<Vec2> for Vec2 {
type Output = Self;
#[inline]
fn div(self, other: Vec2) -> Self {
Self(self.0 / other.0, self.1 / other.1)
}
}
impl DivAssign<Vec2> for Vec2 {
#[inline]
fn div_assign(&mut self, other: Vec2) {
self.0 /= other.0;
self.1 /= other.1;
}
}
impl Div<f32> for Vec2 {
type Output = Self;
#[inline]
fn div(self, other: f32) -> Self {
Self(self.0 / other, self.1 / other)
}
}
impl DivAssign<f32> for Vec2 {
#[inline]
fn div_assign(&mut self, other: f32) {
self.0 /= other;
self.1 /= other;
}
}
impl Div<Vec2> for f32 {
type Output = Vec2;
#[inline]
fn div(self, other: Vec2) -> Vec2 {
Vec2(self / other.0, self / other.1)
}
}
impl Mul<Vec2> for Vec2 {
type Output = Self;
#[inline]
fn mul(self, other: Vec2) -> Self {
Self(self.0 * other.0, self.1 * other.1)
}
}
impl MulAssign<Vec2> for Vec2 {
#[inline]
fn mul_assign(&mut self, other: Vec2) {
self.0 *= other.0;
self.1 *= other.1;
}
}
impl Mul<f32> for Vec2 {
type Output = Self;
#[inline]
fn mul(self, other: f32) -> Self {
Self(self.0 * other, self.1 * other)
}
}
impl MulAssign<f32> for Vec2 {
#[inline]
fn mul_assign(&mut self, other: f32) {
self.0 *= other;
self.1 *= other;
}
}
impl Mul<Vec2> for f32 {
type Output = Vec2;
#[inline]
fn mul(self, other: Vec2) -> Vec2 {
Vec2(self * other.0, self * other.1)
}
}
impl Add for Vec2 {
type Output = Self;
#[inline]
fn add(self, other: Self) -> Self {
Self(self.0 + other.0, self.1 + other.1)
}
}
impl AddAssign for Vec2 {
#[inline]
fn add_assign(&mut self, other: Self) {
self.0 += other.0;
self.1 += other.1;
}
}
impl Sub for Vec2 {
type Output = Self;
#[inline]
fn sub(self, other: Vec2) -> Self {
Self(self.0 - other.0, self.1 - other.1)
}
}
impl SubAssign for Vec2 {
#[inline]
fn sub_assign(&mut self, other: Vec2) {
self.0 -= other.0;
self.1 -= other.1;
}
}
impl Neg for Vec2 {
type Output = Self;
#[inline]
fn neg(self) -> Self {
Self(-self.0, -self.1)
}
}
impl AsRef<[f32; 2]> for Vec2 {
#[inline]
fn as_ref(&self) -> &[f32; 2] {
unsafe { &*(self as *const Vec2 as *const [f32; 2]) }
}
}
impl AsMut<[f32; 2]> for Vec2 {
#[inline]
fn as_mut(&mut self) -> &mut [f32; 2] {
unsafe { &mut *(self as *mut Vec2 as *mut [f32; 2]) }
}
}
impl Index<usize> for Vec2 {
type Output = f32;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
&self.as_ref()[index]
}
}
impl IndexMut<usize> for Vec2 {
#[inline]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.as_mut()[index]
}
}
impl From<(f32, f32)> for Vec2 {
#[inline]
fn from(t: (f32, f32)) -> Self {
Self(t.0, t.1)
}
}
impl From<Vec2> for (f32, f32) {
#[inline]
fn from(v: Vec2) -> Self {
(v.0, v.1)
}
}
impl From<[f32; 2]> for Vec2 {
#[inline]
fn from(a: [f32; 2]) -> Self {
Self(a[0], a[1])
}
}
impl From<Vec2> for [f32; 2] {
#[inline]
fn from(v: Vec2) -> Self {
[v.0, v.1]
}
}