use super::unsignedcoord::UnsignedCoord;
type CoordPart = i32;
#[cfg(not(feature = "nalgebra_support"))]
mod internal_coord {
use super::*;
use core::ops::{Add, AddAssign, Index, Neg, Sub, SubAssign};
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Coord(pub CoordPart, pub CoordPart);
impl Coord {
pub fn new(x: CoordPart, y: CoordPart) -> Self {
Coord(x, y)
}
pub fn clamp_positive(&self) -> Self {
Coord::new(self.0.max(0), self.1.max(0))
}
pub fn abs(&self) -> Self {
Coord::new(self.0.abs(), self.1.abs())
}
}
impl Add for Coord {
type Output = Coord;
fn add(self, other: Coord) -> Coord {
Coord::new(self.0 + other.0, self.1 + other.1)
}
}
impl AddAssign for Coord {
fn add_assign(&mut self, other: Coord) {
self.0 += other.0;
self.1 += other.1;
}
}
impl Sub for Coord {
type Output = Coord;
fn sub(self, other: Coord) -> Coord {
Coord::new(self.0 - other.0, self.1 - other.1)
}
}
impl SubAssign for Coord {
fn sub_assign(&mut self, other: Coord) {
self.0 -= other.0;
self.1 -= other.1;
}
}
impl Index<usize> for Coord {
type Output = CoordPart;
fn index(&self, idx: usize) -> &CoordPart {
match idx {
0 => &self.0,
1 => &self.1,
_ => panic!("Unreachable index {}", idx),
}
}
}
impl Neg for Coord {
type Output = Coord;
fn neg(self) -> Self::Output {
Coord::new(-self.0, -self.1)
}
}
impl From<(u32, u32)> for Coord {
fn from(other: (u32, u32)) -> Self {
Self(other.0 as i32, other.1 as i32)
}
}
impl From<[u32; 2]> for Coord {
fn from(other: [u32; 2]) -> Self {
Self(other[0] as i32, other[1] as i32)
}
}
impl From<&[u32; 2]> for Coord {
fn from(other: &[u32; 2]) -> Self {
Self(other[0] as i32, other[1] as i32)
}
}
impl From<(i32, i32)> for Coord {
fn from(other: (i32, i32)) -> Self {
Self(other.0, other.1)
}
}
impl From<[i32; 2]> for Coord {
fn from(other: [i32; 2]) -> Self {
Self(other[0], other[1])
}
}
impl From<&[i32; 2]> for Coord {
fn from(other: &[i32; 2]) -> Self {
Self(other[0], other[1])
}
}
impl From<Coord> for (i32, i32) {
fn from(other: Coord) -> (i32, i32) {
(other.0, other.1)
}
}
impl From<&Coord> for (i32, i32) {
fn from(other: &Coord) -> (i32, i32) {
(other.0, other.1)
}
}
}
#[cfg(not(feature = "nalgebra_support"))]
pub use self::internal_coord::Coord;
#[cfg(feature = "nalgebra_support")]
use nalgebra;
#[cfg(feature = "nalgebra_support")]
pub type Coord = nalgebra::Vector2<CoordPart>;
pub trait ToUnsigned {
fn to_unsigned(self) -> UnsignedCoord;
}
impl ToUnsigned for Coord {
fn to_unsigned(self) -> UnsignedCoord {
UnsignedCoord::new(self[0].max(0) as u32, self[1].max(0) as u32)
}
}
#[macro_export]
macro_rules! icoord {
($x:expr, $y:expr) => {
$crate::coord::Coord::new($x, $y)
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn coords_can_be_added() {
let left = Coord::new(10, 20);
let right = Coord::new(30, 40);
assert_eq!(left + right, Coord::new(40, 60));
}
#[test]
fn coords_can_be_subtracted() {
let left = Coord::new(30, 40);
let right = Coord::new(10, 20);
assert_eq!(left - right, Coord::new(20, 20));
}
#[test]
fn coords_can_be_negative_subtracted() {
let left = Coord::new(10, 20);
let right = Coord::new(30, 40);
assert_eq!(left - right, Coord::new(-20, -20));
}
#[test]
#[cfg(not(feature = "nalgebra_support"))]
fn from_tuple() {
assert_eq!(Coord::from((20u32, 30u32)), Coord::new(20, 30));
assert_eq!(Coord::from((20i32, 30i32)), Coord::new(20, 30));
}
#[test]
#[cfg(not(feature = "nalgebra_support"))]
fn from_array() {
assert_eq!(Coord::from([20u32, 30u32]), Coord::new(20, 30));
assert_eq!(Coord::from([20i32, 30i32]), Coord::new(20, 30));
}
#[test]
#[cfg(feature = "nalgebra_support")]
fn from_array() {
assert_eq!(Coord::from([20i32, 30i32]), Coord::new(20, 30));
}
#[test]
#[cfg(not(feature = "nalgebra_support"))]
fn from_array_ref() {
assert_eq!(Coord::from(&[20u32, 30u32]), Coord::new(20, 30));
assert_eq!(Coord::from(&[20i32, 30i32]), Coord::new(20, 30));
}
#[test]
#[cfg(feature = "nalgebra_support")]
fn nalgebra_support() {
let left = nalgebra::Vector2::new(30, 40);
let right = nalgebra::Vector2::new(10, 20);
assert_eq!(left - right, Coord::new(20, 20));
}
#[test]
fn neg() {
assert_eq!(-Coord::new(10, 20), Coord::new(-10, -20));
assert_eq!(-Coord::new(-40, -50), Coord::new(40, 50));
}
}