use std::{fmt, iter::Sum};
use std::hash::Hash;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::ResultPoint;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Default)]
pub struct PointT<T> {
pub x: T,
pub y: T,
}
pub type PointF = PointT<f32>;
pub type PointI = PointT<i32>;
pub type PointU = PointT<u32>;
pub type Point = PointF;
impl From<Point> for PointI {
fn from(val: Point) -> Self {
PointI {
x: val.x.floor() as i32,
y: val.y.floor() as i32,
}
}
}
impl From<PointI> for Point {
fn from(val: PointI) -> Self {
Point {
x: val.x as f32,
y: val.y as f32,
}
}
}
impl From<Point> for PointU {
fn from(val: Point) -> Self {
PointU {
x: val.x.floor() as u32,
y: val.y.floor() as u32,
}
}
}
impl From<PointU> for Point {
fn from(val: PointU) -> Self {
Point {
x: val.x as f32,
y: val.y as f32,
}
}
}
pub const fn point_f(x: f32, y: f32) -> Point {
Point::new(x, y)
}
pub const fn point<T>(x: T, y: T) -> PointT<T>
where
T: Copy,
{
PointT::new(x, y)
}
pub fn point_i<T: Into<i64>>(x: T, y: T) -> Point {
Point::new(x.into() as f32, y.into() as f32)
}
impl Hash for Point {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.x.to_string().hash(state);
self.y.to_string().hash(state);
}
}
impl<T> PartialEq for PointT<T>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
}
}
impl<T> Eq for PointT<T> where T: PartialEq {}
impl<T> PointT<T>
where
T: Copy,
{
pub const fn new(x: T, y: T) -> PointT<T> {
PointT { x, y }
}
pub const fn with_single(x: T) -> Self {
Self { x, y: x }
}
}
impl<T> std::ops::AddAssign for PointT<T>
where
T: std::ops::Add<Output = T> + Copy,
{
fn add_assign(&mut self, rhs: Self) {
self.x = self.x + rhs.x;
self.y = self.y + rhs.y;
}
}
impl<T> std::ops::SubAssign for PointT<T>
where
T: std::ops::Sub<Output = T> + Copy,
{
fn sub_assign(&mut self, rhs: Self) {
self.x = self.x - rhs.x;
self.y = self.y - rhs.y;
}
}
impl<'a, T> Sum<&'a PointT<T>> for PointT<T>
where
T: std::ops::Add<Output = T> + 'a + Default,
PointT<T>: std::ops::Add<Output = PointT<T>> + Copy,
{
fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
iter.fold(Self::default(), |acc, &p| acc + p)
}
}
impl<T> ResultPoint for PointT<T>
where
T: Into<f32> + Copy,
{
fn getX(&self) -> f32 {
self.x.into()
}
fn getY(&self) -> f32 {
self.y.into()
}
fn to_rxing_result_point(&self) -> PointT<f32> {
PointT {
x: self.x.into(),
y: self.y.into(),
}
}
}
impl<T> fmt::Display for PointT<T>
where
T: std::fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({},{})", self.x, self.y)
}
}
impl<T> std::ops::Sub for PointT<T>
where
T: std::ops::Sub<Output = T> + Copy,
{
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self::new(self.x - rhs.x, self.y - rhs.y)
}
}
impl<T> std::ops::Neg for PointT<T>
where
T: std::ops::Neg<Output = T> + Copy,
{
type Output = Self;
fn neg(self) -> Self::Output {
Self::new(-self.x, -self.y)
}
}
impl<T> std::ops::Add for PointT<T>
where
T: std::ops::Add<Output = T> + Copy,
{
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::new(self.x + rhs.x, self.y + rhs.y)
}
}
impl<T> std::ops::Add<f32> for PointT<T>
where
T: Into<f32> + std::ops::Add<f32, Output = T> + Copy,
{
type Output = Self;
fn add(self, rhs: f32) -> Self::Output {
Self::new(self.x + rhs, self.y + rhs)
}
}
impl<T> std::ops::Add<PointT<T>> for f32
where
T: std::ops::Add<f32, Output = f32>,
{
type Output = Point;
fn add(self, rhs: PointT<T>) -> Self::Output {
Point::new(rhs.x + self, rhs.y + self)
}
}
impl<T> std::ops::Mul for PointT<T>
where
T: std::ops::Mul<Output = T> + Copy,
{
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self::new(self.x * rhs.x, self.y * rhs.y)
}
}
impl std::ops::Mul<f32> for Point {
type Output = Self;
fn mul(self, rhs: f32) -> Self::Output {
Self::new(self.x * rhs, self.y * rhs)
}
}
impl std::ops::Mul<i32> for Point {
type Output = Self;
fn mul(self, rhs: i32) -> Self::Output {
Self::new(self.x * rhs as f32, self.y * rhs as f32)
}
}
impl std::ops::Mul<u32> for Point {
type Output = Self;
fn mul(self, rhs: u32) -> Self::Output {
Self::new(self.x * rhs as f32, self.y * rhs as f32)
}
}
impl std::ops::Mul<Point> for i32 {
type Output = Point;
fn mul(self, rhs: Point) -> Self::Output {
Self::Output::new(rhs.x * self as f32, rhs.y * self as f32)
}
}
impl std::ops::Mul<Point> for f32 {
type Output = Point;
fn mul(self, rhs: Point) -> Self::Output {
Self::Output::new(rhs.x * self, rhs.y * self)
}
}
impl std::ops::Div<f32> for Point {
type Output = Point;
fn div(self, rhs: f32) -> Self::Output {
Self::Output::new(self.x / rhs, self.y / rhs)
}
}
impl std::ops::Mul<Point> for u32 {
type Output = Point;
fn mul(self, rhs: Point) -> Self::Output {
Self::Output::new(rhs.x * self as f32, rhs.y * self as f32)
}
}
impl<T> PointT<T>
where
T: std::ops::Mul<Output = T> + std::ops::Sub<Output = T> + num::traits::real::Real,
PointT<T>: std::ops::Div<T, Output = PointT<T>>,
{
pub fn dot(self, p: Self) -> T {
self.x * p.x + self.y * p.y
}
pub fn cross(self, p: Self) -> T {
self.x * p.y - p.x * self.y
}
pub fn sumAbsComponent(self) -> T {
self.x.abs() + self.y.abs()
}
pub fn length(self) -> T {
self.x.hypot(self.y)
}
pub fn maxAbsComponent(self) -> T {
self.x.abs().max(self.y.abs())
}
pub fn squaredDistance(self, p: Self) -> T {
let diff = self - p;
diff.x * diff.x + diff.y * diff.y
}
pub fn distance(self, p: Self) -> T {
(self - p).length()
}
pub fn abs(self) -> Self {
Self::new(self.x.abs(), self.y.abs())
}
pub fn fold<U, F: Fn(T, T) -> U>(self, f: F) -> U {
f(self.x, self.y)
}
pub fn middle(self, p: Self) -> Self
where
T: From<u8>,
{
(self + p) / 2.into()
}
pub fn normalized(self) -> Self {
self / Self::length(self)
}
pub fn bresenhamDirection(self) -> Self {
self / Self::maxAbsComponent(self)
}
pub fn mainDirection(self) -> Self
where
T: From<u8>,
{
if self.x.abs() > self.y.abs() {
Self::new(self.x, 0.into())
} else {
Self::new(0.into(), self.y)
}
}
pub fn round(self) -> Self {
Self {
x: self.x.round(),
y: self.y.round(),
}
}
#[inline(always)]
pub fn centered(self) -> PointT<f32>
where
T: Into<f32>,
{
PointT::new(self.x.floor().into() + 0.5, self.y.floor().into() + 0.5)
}
pub fn floor(self) -> Self {
Self {
x: self.x.floor(),
y: self.y.floor(),
}
}
pub fn crossProductZ(a: PointT<T>, b: PointT<T>, c: PointT<T>) -> T {
((c.x - b.x) * (a.y - b.y)) - ((c.y - b.y) * (a.x - b.x))
}
}
impl From<(i32, i32)> for Point {
fn from((x, y): (i32, i32)) -> Self {
Self::new(x as f32, y as f32)
}
}
impl From<(u32, u32)> for Point {
fn from((x, y): (u32, u32)) -> Self {
Self::new(x as f32, y as f32)
}
}
impl From<(f32, f32)> for PointI {
fn from((x, y): (f32, f32)) -> Self {
PointI {
x: x.floor() as i32,
y: y.floor() as i32,
}
}
}
impl<T> From<(T, T)> for PointT<T> {
fn from((x, y): (T, T)) -> PointT<T> {
PointT { x, y }
}
}
impl<T> From<&(T, T)> for PointT<T>
where
T: Copy,
{
fn from(&(x, y): &(T, T)) -> PointT<T> {
PointT { x, y }
}
}
impl<T> From<T> for PointT<T>
where
T: Copy,
{
fn from(value: T) -> Self {
Self::with_single(value)
}
}
#[cfg(test)]
mod tests {
use super::Point;
#[test]
fn testDistance() {
assert_eq!(
(8.0f32).sqrt(),
Point::new(1.0, 2.0).distance(Point::new(3.0, 4.0))
);
assert_eq!(0.0, Point::new(1.0, 2.0).distance(Point::new(1.0, 2.0)));
assert_eq!(
(8.0f32).sqrt(),
Point::new(1.0, 2.0).distance(Point::new(3.0, 4.0))
);
assert_eq!(0.0, Point::new(1.0, 2.0).distance(Point::new(1.0, 2.0)));
}
}