use std::{fmt, ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}};
use num_traits::{Float, NumCast, Zero};
use crate::base::Orientation;
#[derive(Copy, Clone, Default, PartialEq, Eq, Hash)]
pub struct Vec2<T> {
pub x: T,
pub y: T,
}
impl<T: Zero> Vec2<T> {
pub fn zero() -> Self {
Self {
x: T::zero(),
y: T::zero(),
}
}
pub fn is_zero(&self) -> bool {
self.x.is_zero() && self.y.is_zero()
}
}
#[macro_export]
macro_rules! vec2 {
($x:expr, $y:expr) => {
Vec2::new($x, $y)
};
}
impl<T: Copy> From<Vec2<T>> for (T, T) {
#[inline]
fn from(v: Vec2<T>) -> Self {
(v.x, v.y)
}
}
impl<T: Copy> From<&Vec2<T>> for (T, T) {
#[inline]
fn from(v: &Vec2<T>) -> Self {
(v.x, v.y)
}
}
impl<T> Vec2<T> {
pub fn new(x: T, y: T) -> Self {
Vec2 { x, y }
}
}
impl<T: Copy + Zero + PartialOrd + Sub<Output = T>> Vec2<T> {
pub fn norm1(&self) -> T {
let xabs = if self.x < T::zero() {
T::zero() - self.x
} else {
self.x
};
let yabs = if self.y < T::zero() {
T::zero() - self.y
} else {
self.y
};
xabs + yabs
}
}
impl<T: Copy + Zero + PartialOrd + Mul<Output = T> + Sub<Output = T>> Vec2<T> {
pub fn orientation_of(&self, other: Self) -> Orientation {
let p = self.cross_prod(other);
if p < T::zero() {
Orientation::ClockWise
} else if p > T::zero() {
Orientation::CounterClockWise
} else {
Orientation::Straight
}
}
}
impl<T: Copy + Mul<Output = T> + Sub<Output = T>> Vec2<T> {
#[inline]
pub fn cross_prod(&self, other: Self) -> T {
self.x * other.y - other.x * self.y
}
}
impl<T: Copy + Mul<Output = T> + Add<Output = T>> Vec2<T> {
#[inline]
pub fn norm2_squared(&self) -> T {
self.x * self.x + self.y * self.y
}
pub fn dot(&self, other: Self) -> T {
self.x * other.x + self.y * other.y
}
}
impl<T> Vec2<T>
where
T: Copy + Float + Mul<Output = T> + Add<Output = T>,
{
#[inline]
pub fn norm2(&self) -> T {
(self.norm2_squared()).sqrt()
}
#[inline]
pub fn normalized(&self) -> Self {
*self / self.norm2()
}
}
impl<T: Copy + NumCast> Vec2<T> {
#[inline]
pub fn length<F: Float>(&self) -> F {
let x = F::from(self.x).unwrap();
let y = F::from(self.y).unwrap();
x.hypot(y)
}
}
impl<T> Vec2<T>
where
T: Copy + NumCast,
{
#[inline]
pub fn to_float<F>(&self) -> Vec2<F>
where
F: Float + NumCast,
{
Vec2 {
x: F::from(self.x).unwrap(),
y: F::from(self.y).unwrap(),
}
}
}
impl<T> Add for Vec2<T>
where
T: Add<Output = T>,
{
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self {
Vec2 {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl<T> AddAssign for Vec2<T>
where
T: AddAssign,
{
#[inline]
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
}
}
impl<T> Sub for Vec2<T>
where
T: Sub<Output = T>,
{
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self {
Vec2 {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl<T> SubAssign for Vec2<T>
where
T: SubAssign,
{
#[inline]
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
}
}
impl<T> Neg for Vec2<T>
where
T: Neg<Output = T>,
{
type Output = Self;
#[inline]
fn neg(self) -> Self {
Vec2 {
x: -self.x,
y: -self.y,
}
}
}
impl<T, M> Mul<M> for Vec2<T>
where
T: Mul<M, Output = T>,
M: Copy,
{
type Output = Self;
fn mul(self, rhs: M) -> Self {
Vec2 {
x: self.x * rhs,
y: self.y * rhs,
}
}
}
impl<T, M> MulAssign<M> for Vec2<T>
where
T: Copy + MulAssign<M>,
M: Copy,
{
#[inline]
fn mul_assign(&mut self, rhs: M) {
self.x *= rhs;
self.y *= rhs;
}
}
impl<T, D> Div<D> for Vec2<T>
where
T: Copy + Div<D, Output = T>,
D: Copy,
{
type Output = Self;
#[inline]
fn div(self, rhs: D) -> Self {
Vec2 {
x: self.x / rhs,
y: self.y / rhs,
}
}
}
impl<T, D> DivAssign<D> for Vec2<T>
where
T: Copy + DivAssign<D>,
D: Copy,
{
#[inline]
fn div_assign(&mut self, rhs: D) {
self.x /= rhs;
self.y /= rhs;
}
}
impl<T> std::iter::Sum for Vec2<T>
where
T: Zero + Add<Output = T>,
{
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Self::zero(), |acc, v| acc + v)
}
}
impl<T: Copy> From<(T, T)> for Vec2<T> {
#[inline]
fn from(coords: (T, T)) -> Self {
Vec2::new(coords.0, coords.1)
}
}
impl<'a, T: Copy> From<&'a (T, T)> for Vec2<T> {
#[inline]
fn from(coords: &'a (T, T)) -> Self {
Vec2::new(coords.0, coords.1)
}
}
impl<'a, T: Copy> From<&'a Vec2<T>> for Vec2<T> {
#[inline]
fn from(v: &'a Vec2<T>) -> Self {
Vec2::new(v.x, v.y)
}
}
impl<T: Copy> From<[T; 2]> for Vec2<T> {
#[inline]
fn from(coords: [T; 2]) -> Self {
Vec2::new(coords[0], coords[1])
}
}
impl<T> fmt::Debug for Vec2<T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({:?}, {:?})", &self.x, &self.y)
}
}
impl<T> fmt::Display for Vec2<T>
where
T: fmt::Display + Copy,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}