cp_library_rs/geometry/
arg.rs

1//! 偏角ソート
2
3use std::cmp::Ordering;
4
5/// 2 次元ベクトルの偏角
6/// - Ord は x 軸正の向きを基準にした偏角順
7/// - 参考: <https://ngtkana.hatenablog.com/entry/2021/11/13/202103>
8#[derive(Debug, Clone, Copy)]
9pub struct Arg(pub i64, pub i64);
10
11impl Arg {
12    pub fn to_tuple(&self) -> (i64, i64) {
13        (self.0, self.1)
14    }
15}
16
17impl From<(i64, i64)> for Arg {
18    fn from((x, y): (i64, i64)) -> Self {
19        Self(x, y)
20    }
21}
22
23impl From<&(i64, i64)> for Arg {
24    fn from(&(x, y): &(i64, i64)) -> Self {
25        Self(x, y)
26    }
27}
28
29impl Ord for Arg {
30    fn cmp(&self, other: &Self) -> Ordering {
31        let &Arg(x0, y0) = self;
32        let &Arg(x1, y1) = other;
33
34        ((y0, x0) < (0, 0))
35            .cmp(&((y1, x1) < (0, 0)))
36            .then_with(|| (x1 * y0).cmp(&(x0 * y1)))
37    }
38}
39
40impl PartialOrd for Arg {
41    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
42        Some(self.cmp(other))
43    }
44}
45
46impl PartialEq for Arg {
47    fn eq(&self, other: &Self) -> bool {
48        self.cmp(other) == Ordering::Equal
49    }
50}
51
52impl Eq for Arg {}