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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::{f64,f32};
use std::ops::{
Neg,
Add,Div,Mul,Rem,Sub,
AddAssign,DivAssign,MulAssign,RemAssign,SubAssign,
};
pub trait Angle: Sized {
type Type : Into<Self>;
const TURN: Self::Type;
fn from_value<V: Into<Self::Type>>(v: V) -> Self;
fn into_value<V>(self) -> V
where Self::Type: Into<V>;
#[inline]
fn sin(self) -> Self::Type;
#[inline]
fn cos(self) -> Self::Type;
#[inline]
fn tan(self) -> Self::Type;
#[inline]
fn sin_cos(self) -> (Self::Type, Self::Type);
#[inline]
fn asin(s: Self::Type) -> Self;
#[inline]
fn acos(s: Self::Type) -> Self;
#[inline]
fn atan(s: Self::Type) -> Self;
#[inline]
fn atan2(y: Self::Type, x: Self::Type) -> Self;
}
macro_rules! impl_angle_binop {
($float:ty, $trait_name:ident::$method_name:ident for $id:ty = $output:ty ) => {
impl_angle_binop!{$float, $trait_name::$method_name<$id> for $id = $output}
};
($float:ty, $trait_name:ident::$method_name:ident<$rhs:ty> for $id:ty = $output:ty) => {
impl $trait_name<$rhs> for $id {
type Output = $output;
fn $method_name(self, rhs: $rhs) -> $output {
let lhs: $float = self.into();
let rhs: $float = rhs.into();
$trait_name::$method_name(lhs, rhs).into()
}
}
}
}
macro_rules! impl_angle_binop_assign {
($trait_name:ident::$method_name:ident for $id:ident) => {
impl_angle_binop_assign!{$trait_name::$method_name<$id> for $id}
};
($trait_name:ident::$method_name:ident<$rhs:ty> for $id:ident) => {
impl $trait_name<$rhs> for $id {
fn $method_name(&mut self, rhs: $rhs) {
let rhs: <$id as Angle>::Type = rhs.into();
$trait_name::$method_name(&mut self.0, rhs)
}
}
};
}
macro_rules! impl_angle {
($id:ident: $float:ty, $size:tt, $rad:ident, $pi:expr) => {
impl From<$float> for $id { fn from(v: $float) -> Self { $id(v) } }
impl From<$id> for $float { fn from(v: $id) -> Self { v.0 } }
impl Angle for $id {
type Type = $float;
const TURN: $float = $pi;
#[inline]
fn from_value<V: Into<Self::Type>>(v: V) -> Self {
$id(v.into())
}
#[inline]
fn into_value<V>(self) -> V
where Self::Type: Into<V> {
self.0.into()
}
#[inline]
fn sin(self) -> Self::Type { $rad::from(self).0.sin() }
#[inline]
fn cos(self) -> Self::Type { $rad::from(self).0.cos() }
#[inline]
fn tan(self) -> Self::Type { $rad::from(self).0.tan() }
#[inline]
fn sin_cos(self) -> (Self::Type, Self::Type) { $rad::from(self).0.sin_cos() }
#[inline]
fn asin(s: Self::Type) -> Self { $rad(s.asin()).into() }
#[inline]
fn acos(s: Self::Type) -> Self { $rad(s.acos()).into() }
#[inline]
fn atan(s: Self::Type) -> Self { $rad(s.atan()).into() }
#[inline]
fn atan2(y: Self::Type, x: Self::Type) -> Self { $rad(y.atan2(x)).into() }
}
impl Neg for $id {
type Output = $id;
fn neg(self) -> Self::Output { $id(Neg::neg(self.0)) }
}
impl_angle_binop!{$float, Add::add for $id = $id}
impl_angle_binop!{$float, Div::div for $id = $float}
impl_angle_binop!{$float, Div::div<$float> for $id = $id}
impl_angle_binop!{$float, Mul::mul<$float> for $id = $id}
impl_angle_binop!{$float, Mul::mul<$id> for $float = $id}
impl_angle_binop!{$float, Rem::rem for $id = $id}
impl_angle_binop!{$float, Sub::sub for $id = $id}
impl_angle_binop_assign!{ AddAssign::add_assign for $id }
impl_angle_binop_assign!{ MulAssign::mul_assign<$float> for $id }
impl_angle_binop_assign!{ DivAssign::div_assign<$float> for $id }
impl_angle_binop_assign!{ RemAssign::rem_assign for $id }
impl_angle_binop_assign!{ SubAssign::sub_assign for $id }
};
}
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Rad32(f32);
impl_angle!{Rad32: f32, 32, Rad32, f32::consts::PI}
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Rad64(f64);
impl_angle!{Rad64: f64, 64, Rad64, f64::consts::PI}
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Deg32(f32);
impl_angle!{Deg32: f32, 32, Rad32, 360f32}
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Deg64(f64);
impl_angle!{Deg64: f64, 64, Rad64, 360f64}
impl From<Deg32> for Rad32 { fn from(v: Deg32) -> Self { Rad32(v.0.to_radians()) } }
impl From<Rad32> for Rad64 { fn from(v: Rad32) -> Self { Rad64(v.0.into()) } }
impl From<Deg32> for Rad64 { fn from(v: Deg32) -> Self { Rad64((v.0 as f64).to_radians()) } }
impl From<Deg64> for Rad64 { fn from(v: Deg64) -> Self { Rad64(v.0.to_radians()) } }
impl From<Rad32> for Deg32 { fn from(v: Rad32) -> Self { Deg32(v.0.to_degrees()) } }
impl From<Deg32> for Deg64 { fn from(v: Deg32) -> Self { Deg64(v.0.into()) } }
impl From<Rad32> for Deg64 { fn from(v: Rad32) -> Self { Deg64((v.0 as f64).to_degrees()) } }
impl From<Rad64> for Deg64 { fn from(v: Rad64) -> Self { Deg64(v.0.to_degrees()) } }