use core::ops;
use serde::{Deserialize, Serialize};
use super::vector3d::Vector3D;
use super::vector4d::Vector4D;
use crate::base::{Point, PointF};
use crate::util::{fuzzy_compare_f32, fuzzy_is_zero};
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Vector2D {
v: [f32; 2],
}
impl Default for Vector2D {
fn default() -> Self {
Self::new()
}
}
impl Vector2D {
#[must_use]
pub const fn new() -> Self {
Self::from(0.0, 0.0)
}
#[must_use]
pub const fn from(x: f32, y: f32) -> Self {
Self { v: [x, y] }
}
#[must_use]
pub const fn from_4d(vector: &Vector4D) -> Self {
Self::from(vector.x(), vector.y())
}
#[must_use]
pub const fn from_3d(vector: &Vector3D) -> Self {
Self::from(vector.x(), vector.y())
}
#[must_use]
pub const fn from_point_f(point: &PointF) -> Self {
#[allow(clippy::cast_possible_truncation)]
Self::from(point.x() as f32, point.y() as f32)
}
#[must_use]
pub const fn from_point(point: &Point) -> Self {
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_precision_loss)]
Self::from(point.x() as f32, point.y() as f32)
}
#[must_use]
pub fn distance_to_line(&self, point: &Self, direction: &Self) -> f32 {
if direction.is_null() {
return (self - point).length();
}
let p = (self - point).dot_product(direction) * direction + point;
(self - &p).length()
}
#[must_use]
pub fn distance_to_point(&self, point: &Self) -> f32 {
(*self - point).length()
}
#[must_use]
pub fn dot_product(&self, vector: &Self) -> f32 {
self.v[0].mul_add(vector.v[0], self.v[1] * vector.v[1])
}
#[must_use]
pub fn is_null(&self) -> bool {
self.x() == 0.0 && self.y() == 0.0
}
#[must_use]
#[allow(clippy::cast_possible_truncation)]
pub fn length(self) -> f32 {
let hypot = self.length_squared_precise();
hypot.sqrt() as f32
}
fn length_squared_precise(self) -> f64 {
let x = f64::from(self.v[0]);
let y = f64::from(self.v[1]);
x.mul_add(x, y * y)
}
#[must_use]
pub fn length_squared(&self) -> f32 {
self.v[0].mul_add(self.v[0], self.v[1] * self.v[1])
}
#[allow(clippy::cast_possible_truncation)]
pub fn normalize(&mut self) {
let hypot = self.length_squared_precise();
if fuzzy_is_zero(hypot - 1.0) || fuzzy_is_zero(hypot) {
return;
}
let sqrt = hypot.sqrt();
self.v[0] = (f64::from(self.v[0]) / sqrt) as f32;
self.v[1] = (f64::from(self.v[1]) / sqrt) as f32;
}
#[must_use]
pub fn normalized(&self) -> Self {
let hypot = self.length_squared_precise();
if fuzzy_is_zero(hypot - 1.0) {
*self
} else if fuzzy_is_zero(hypot) {
Self::new()
} else {
let sqrt = hypot.sqrt();
#[allow(clippy::cast_possible_truncation)]
Self::from(
(f64::from(self.v[0]) / sqrt) as f32,
(f64::from(self.v[1]) / sqrt) as f32,
)
}
}
pub fn set_x(&mut self, x: f32) {
self.v[0] = x;
}
pub fn set_y(&mut self, y: f32) {
self.v[1] = y;
}
#[must_use]
pub fn to_point(&self) -> Point {
#[allow(clippy::cast_possible_truncation)]
Point::from(self.x().round() as i32, self.y().round() as i32)
}
#[must_use]
pub fn to_point_f(&self) -> PointF {
PointF::from(f64::from(self.x()), f64::from(self.y()))
}
#[must_use]
pub const fn to_vector3d(&self) -> Vector3D {
Vector3D::from(self.v[0], self.v[1], 0.0)
}
#[must_use]
pub const fn to_vector4d(&self) -> Vector4D {
Vector4D::from(self.v[0], self.v[1], 0.0, 0.0)
}
#[must_use]
pub const fn x(&self) -> f32 {
self.v[0]
}
#[must_use]
pub const fn y(&self) -> f32 {
self.v[1]
}
#[must_use]
pub fn fuzzy_compare(&self, other: &Self) -> bool {
fuzzy_compare_f32(self.v[0], other.v[0]) && fuzzy_compare_f32(self.v[1], other.v[1])
}
}
impl ops::AddAssign<&Self> for Vector2D {
fn add_assign(&mut self, vector: &Self) {
self.v[0] += vector.v[0];
self.v[1] += vector.v[1];
}
}
impl ops::Add<&Self> for Vector2D {
type Output = Self;
fn add(self, vector: &Self) -> Self::Output {
Self::from(self.v[0] + vector.v[0], self.v[1] + vector.v[1])
}
}
impl ops::Add<&Vector2D> for &Vector2D {
type Output = Vector2D;
fn add(self, vector: &Vector2D) -> Self::Output {
Vector2D::from(self.v[0] + vector.v[0], self.v[1] + vector.v[1])
}
}
impl ops::SubAssign<&Self> for Vector2D {
fn sub_assign(&mut self, vector: &Self) {
self.v[0] -= vector.v[0];
self.v[1] -= vector.v[1];
}
}
impl ops::Sub<&Self> for Vector2D {
type Output = Self;
fn sub(self, vector: &Self) -> Self::Output {
Self::from(self.v[0] - vector.v[0], self.v[1] - vector.v[1])
}
}
impl ops::Sub<&Vector2D> for &Vector2D {
type Output = Vector2D;
fn sub(self, vector: &Vector2D) -> Self::Output {
Vector2D::from(self.v[0] - vector.v[0], self.v[1] - vector.v[1])
}
}
impl ops::Neg for &Vector2D {
type Output = Vector2D;
fn neg(self) -> Self::Output {
Vector2D::from(-self.v[0], -self.v[1])
}
}
impl ops::MulAssign<f32> for Vector2D {
fn mul_assign(&mut self, factor: f32) {
self.v[0] *= factor;
self.v[1] *= factor;
}
}
impl ops::MulAssign<&Self> for Vector2D {
fn mul_assign(&mut self, vector: &Self) {
self.v[0] *= vector.v[0];
self.v[1] *= vector.v[1];
}
}
impl ops::Mul<f32> for Vector2D {
type Output = Self;
fn mul(self, factor: f32) -> Self::Output {
Self::from(self.v[0] * factor, self.v[1] * factor)
}
}
impl ops::Mul<&Vector2D> for f32 {
type Output = Vector2D;
fn mul(self, vector: &Vector2D) -> Self::Output {
Vector2D::from(self * vector.v[0], self * vector.v[1])
}
}
impl ops::Mul<&Self> for Vector2D {
type Output = Self;
fn mul(self, vector: &Self) -> Self::Output {
Self::from(self.v[0] * vector.v[0], self.v[1] * vector.v[1])
}
}
impl ops::DivAssign<f32> for Vector2D {
fn div_assign(&mut self, divisor: f32) {
assert!(divisor != 0.0);
self.v[0] /= divisor;
self.v[1] /= divisor;
}
}
impl ops::DivAssign<&Self> for Vector2D {
fn div_assign(&mut self, vector: &Self) {
assert!(vector.v[0] != 0.0);
assert!(vector.v[1] != 0.0);
self.v[0] /= vector.v[0];
self.v[1] /= vector.v[1];
}
}
impl ops::Div<f32> for Vector2D {
type Output = Self;
fn div(self, divisor: f32) -> Self::Output {
assert!(divisor != 0.0);
Self::from(self.v[0] / divisor, self.v[1] / divisor)
}
}
impl ops::Div<&Self> for Vector2D {
type Output = Self;
fn div(self, vector: &Self) -> Self::Output {
assert!(vector.v[0] != 0.0);
assert!(vector.v[1] != 0.0);
Self::from(self.v[0] / vector.v[0], self.v[1] / vector.v[1])
}
}
impl ops::Index<usize> for Vector2D {
type Output = f32;
fn index(&self, index: usize) -> &Self::Output {
assert!(index <= 1);
&self.v[index]
}
}
impl ops::IndexMut<usize> for Vector2D {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
assert!(index <= 1);
&mut self.v[index]
}
}