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
//! 幾何ライブラリ

use std::{
    hash::Hash,
    ops::{Add, Div, Mul, Neg, Sub},
};

#[allow(unused_imports)]
use crate::{
    geometry::basic,
    utils::num_traits::{Num, PrimInt},
};

/// 2次元ベクトル
#[derive(Debug, Clone, Copy)]
pub struct Vec2<T>(pub T, pub T);

impl<T: PrimInt> PartialEq for Vec2<T> {
    fn eq(&self, other: &Self) -> bool {
        let Self(ax, ay) = self;
        let Self(bx, by) = &other;
        ax == bx && ay == by
    }
}

impl<T: PrimInt> Eq for Vec2<T> {}

impl<T: Hash> Hash for Vec2<T> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state);
        self.1.hash(state);
    }
}

impl<T> Add for Vec2<T>
where
    T: Add<Output = T>,
{
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0 + rhs.0, self.1 + rhs.1)
    }
}

impl<T> Sub for Vec2<T>
where
    T: Sub<Output = T>,
{
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Self(self.0 - rhs.0, self.1 - rhs.1)
    }
}

impl<T> Neg for Vec2<T>
where
    T: Neg<Output = T>,
{
    type Output = Self;
    fn neg(self) -> Self::Output {
        Self(-self.0, -self.1)
    }
}

impl<T> Mul<T> for Vec2<T>
where
    T: Mul<Output = T> + Clone,
{
    type Output = Self;
    fn mul(self, rhs: T) -> Self::Output {
        Self(self.0 * rhs.clone(), self.1 * rhs)
    }
}

impl<T> Div<T> for Vec2<T>
where
    T: Div<Output = T> + Clone,
{
    type Output = Self;
    fn div(self, rhs: T) -> Self::Output {
        Self(self.0 / rhs.clone(), self.1 / rhs)
    }
}

impl<T: Num + Copy> Vec2<T> {
    /// 2つのベクトルのドット積を求める
    ///
    /// ```math
    /// \boldsymbol{a}\cdot\boldsymbol{b} = a_x b_x + a_y b_y
    /// ```
    pub fn dot(&self, other: Self) -> T {
        let Self(ax, ay) = *self;
        let Self(bx, by) = other;
        ax * bx + ay * by
    }

    /// ノルムの2乗を求める
    ///
    /// ```math
    /// \|\boldsymbol{a}\|^2 = a_x^2 + a_y^2
    /// ```
    pub fn norm2(&self) -> T {
        self.dot(*self)
    }

    /// ベクトル同士の距離の2乗を求める
    ///
    /// ```math
    /// \|\boldsymbol{a} - \boldsymbol{b}\|^2 = (a_x - b_x)^2 + (a_y - b_y)^2
    /// ```
    pub fn dist2(&self, other: Self) -> T {
        let diff = *self - other;
        diff.norm2()
    }

    /// クロス積を求める
    ///
    /// ```math
    /// \boldsymbol{a} \times \boldsymbol{b} = a_x b_y - a_y b_x
    /// ```
    pub fn cross(&self, other: Self) -> T {
        let Self(ax, ay) = *self;
        let Self(bx, by) = other;
        ax * by - ay * bx
    }
}