use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
pub const RVO_EPSILON: f32 = 0.00001;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
pub struct Vector2 {
pub x: f32,
pub y: f32,
}
impl Vector2 {
pub fn default() -> Self {
Self {
x: Default::default(),
y: Default::default(),
}
}
pub fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
pub fn x(&self) -> f32 {
self.x
}
pub fn y(&self) -> f32 {
self.y
}
pub fn add(&self, other: &Vector2) -> Vector2 {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
pub fn sub(&self, other: &Vector2) -> Vector2 {
Self {
x: self.x - other.x,
y: self.y - other.y,
}
}
pub fn mul(&self, other: &Vector2) -> f32 {
self.x * other.x + self.y * other.y
}
pub fn mul_number(&self, other: f32) -> Vector2 {
Self {
x: self.x * other,
y: self.y * other,
}
}
pub fn div(&self, other: f32) -> Vector2 {
Self {
x: self.x / other,
y: self.y / other,
}
}
pub fn neg(&self) -> Vector2 {
Self {
x: -self.x,
y: -self.y,
}
}
pub fn abs(v: &Vector2) -> f32 {
return ((*v) * (*v)).sqrt();
}
pub fn abs_sq(v: &Vector2) -> f32 {
return (*v) * (*v);
}
pub fn det(v1: &Vector2, v2: &Vector2) -> f32 {
return v1.x() * v2.y() - v1.y() * v2.x();
}
pub fn normalize(vector: &Vector2) -> Vector2 {
return (*vector) / Self::abs(vector);
}
pub fn dist_sq_point_line_segment(a: &Vector2, b: &Vector2, c: &Vector2) -> f32 {
let r = (((*c) - *(a)) * ((*b) - *(a))) / Self::abs_sq(&((*b) - *(a)));
if r < 0.0 {
return Self::abs_sq(&((*c) - *a));
} else if r > 1.0 {
return Self::abs_sq(&((*c) - *b));
} else {
return Self::abs_sq(&(*c - ((*b - *a) * r + a)));
}
}
pub fn left_of(a: &Vector2, b: &Vector2, c: &Vector2) -> f32 {
return Self::det(&(*a - *c), &(*b - *a));
}
pub fn sqr(a: f32) -> f32 {
return a * a;
}
}
impl Neg for Vector2 {
type Output = Self;
fn neg(self) -> Self::Output {
Self {
x: -self.x,
y: -self.y,
}
}
}
impl Mul for Vector2 {
type Output = f32;
fn mul(self, rhs: Self) -> f32 {
return self.x * rhs.x + self.y * rhs.y;
}
}
impl Mul<&Self> for Vector2 {
type Output = f32;
fn mul(self, rhs: &Self) -> f32 {
return self.x * rhs.x + self.y * rhs.y;
}
}
impl Mul<f32> for Vector2 {
type Output = Self;
fn mul(self, rhs: f32) -> Self {
return Vector2::new(self.x * rhs, self.y * rhs);
}
}
impl Div<f32> for Vector2 {
type Output = Self;
fn div(self, rhs: f32) -> Self::Output {
let inv_s = 1.0 / rhs;
return Vector2::new(self.x * inv_s, self.y * inv_s);
}
}
impl Add<&Self> for Vector2 {
type Output = Self;
fn add(self, other: &Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
impl Add for Vector2 {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
impl Sub for Vector2 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
return Vector2::new(self.x - rhs.x, self.y - rhs.y);
}
}
impl Sub<&Self> for Vector2 {
type Output = Self;
fn sub(self, rhs: &Self) -> Self::Output {
return Vector2::new(self.x - rhs.x, self.y - rhs.y);
}
}
impl PartialEq for Vector2 {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
}
}
impl MulAssign<f32> for Vector2 {
fn mul_assign(&mut self, rhs: f32) {
self.x *= rhs;
self.y *= rhs;
}
}
impl DivAssign<f32> for Vector2 {
fn div_assign(&mut self, rhs: f32) {
self.x /= rhs;
self.y /= rhs;
}
}
impl AddAssign<f32> for Vector2 {
fn add_assign(&mut self, rhs: f32) {
self.x += rhs;
self.y += rhs;
}
}
impl SubAssign<f32> for Vector2 {
fn sub_assign(&mut self, rhs: f32) {
self.x -= rhs;
self.y -= rhs;
}
}
impl Add<f32> for Vector2 {
type Output = Self;
fn add(self, other: f32) -> Self {
Self {
x: self.x + other,
y: self.y + other,
}
}
}