use crate::Scalar;
use serde::{Deserialize, Serialize};
use std::ops::{Add, Div, Mul, Neg, Sub};
#[derive(Debug, Default, Copy, Clone, PartialEq, Serialize, Deserialize)]
pub struct Coord {
pub x: Scalar,
pub y: Scalar,
}
impl Coord {
#[inline]
pub fn new(x: Scalar, y: Scalar) -> Self {
Self { x, y }
}
#[inline]
pub fn sqr_magnitude(self) -> Scalar {
self.x * self.x + self.y * self.y
}
#[inline]
pub fn magnitude(self) -> Scalar {
self.sqr_magnitude().sqrt()
}
#[inline]
pub fn normalized(self) -> Self {
self / self.magnitude()
}
#[inline]
pub fn dot(self, other: Self) -> Scalar {
self.x * other.x + self.y * other.y
}
#[inline]
pub fn right(self) -> Self {
Self {
x: self.y,
y: -self.x,
}
}
#[inline]
pub fn is_left_wrt_line(self, from: Self, to: Self) -> i8 {
((to.x - from.x) * (self.y - from.y) - (self.x - from.x) * (to.y - from.y)) as i8
}
}
impl Add for Coord {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
impl Add<Scalar> for Coord {
type Output = Self;
fn add(self, other: Scalar) -> Self {
Self {
x: self.x + other,
y: self.y + other,
}
}
}
impl Sub for Coord {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self {
x: self.x - other.x,
y: self.y - other.y,
}
}
}
impl Sub<Scalar> for Coord {
type Output = Self;
fn sub(self, other: Scalar) -> Self {
Self {
x: self.x - other,
y: self.y - other,
}
}
}
impl Mul for Coord {
type Output = Self;
fn mul(self, other: Self) -> Self {
Self {
x: self.x * other.x,
y: self.y * other.y,
}
}
}
impl Mul<Scalar> for Coord {
type Output = Self;
fn mul(self, other: Scalar) -> Self {
Self {
x: self.x * other,
y: self.y * other,
}
}
}
impl Div for Coord {
type Output = Self;
fn div(self, other: Self) -> Self {
Self {
x: self.x / other.x,
y: self.y / other.y,
}
}
}
impl Div<Scalar> for Coord {
type Output = Self;
fn div(self, other: Scalar) -> Self {
Self {
x: self.x / other,
y: self.y / other,
}
}
}
impl Neg for Coord {
type Output = Self;
fn neg(self) -> Self {
Self {
x: -self.x,
y: -self.y,
}
}
}