1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
//! 2D & 3D point implementations.
use crate::number::{Number, Signed, SignedInteger, UnsignedInteger};
use std::fmt::Debug;
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
macro_rules! point_impl {
($(#[$m:meta])* $v:vis struct $s:ident{$($f:ident),+}) => {
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
$(#[$m])* $v struct $s<T: Number> {
$(pub $f: T,)+
}
impl<T: Number> $s<T> {
pub const ORIGIN: Self = Self{$($f: T::ZERO),+};
#[inline]
#[must_use]
pub const fn new($($f: T),+) -> Self {
Self{$($f),+}
}
/// Returns the manhattan distance from the origin.
#[inline]
#[must_use]
pub fn manhattan_distance(self) -> T {
T::ZERO $(+ self.$f.abs())+
}
/// Returns the manhattan distance from the origin.
#[inline]
#[must_use]
pub fn manhattan_distance_unsigned(self) -> T::Unsigned
where
T: SignedInteger
{
T::Unsigned::ZERO $(+ self.$f.unsigned_abs())+
}
/// Add the provided signed point, wrapping on overflow.
///
/// Useful for adding a signed direction onto an unsigned position.
#[inline]
#[must_use]
pub fn wrapping_add_signed(self, rhs: $s<T::Signed>) -> Self
where
T: UnsignedInteger,
{
Self{
$($f: self.$f.wrapping_add_signed(rhs.$f),)+
}
}
}
impl<T: Number> Add for $s<T> {
type Output = Self;
#[inline]
#[must_use]
fn add(self, rhs: Self) -> Self {
Self{
$($f: self.$f + rhs.$f,)+
}
}
}
impl<T: Number> AddAssign for $s<T> {
#[inline]
fn add_assign(&mut self, rhs: Self) {
$(self.$f += rhs.$f;)+
}
}
impl<T: Number> Mul<T> for $s<T> {
type Output = Self;
#[inline]
#[must_use]
fn mul(self, rhs: T) -> Self {
Self{
$($f: self.$f * rhs,)+
}
}
}
impl<T: Number> MulAssign<T> for $s<T> {
#[inline]
fn mul_assign(&mut self, rhs: T) {
$(self.$f *= rhs;)+
}
}
impl<T: Number> Sub for $s<T> {
type Output = Self;
#[inline]
#[must_use]
fn sub(self, rhs: Self) -> Self {
Self{
$($f: self.$f - rhs.$f,)+
}
}
}
impl<T: Number> SubAssign for $s<T> {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
$(self.$f -= rhs.$f;)+
}
}
};
}
point_impl! {
/// Struct representing a 2D point or vector.
pub struct Point2D{x, y}
}
impl<T: Signed> Point2D<T> {
pub const UP: Self = Self {
x: T::ZERO,
y: T::ONE,
};
pub const RIGHT: Self = Self {
x: T::ONE,
y: T::ZERO,
};
pub const DOWN: Self = Self {
x: T::ZERO,
y: T::MINUS_ONE,
};
pub const LEFT: Self = Self {
x: T::MINUS_ONE,
y: T::ZERO,
};
/// Rotate this vector 90 degrees clockwise.
#[inline]
#[must_use]
pub fn turn_right(self) -> Self {
Self {
x: self.y,
y: -self.x,
}
}
/// Rotate this vector 90 degrees counterclockwise.
#[inline]
#[must_use]
pub fn turn_left(self) -> Self {
Self {
x: -self.y,
y: self.x,
}
}
}
point_impl! {
/// Struct representing a 3D point or vector.
pub struct Point3D{x, y, z}
}