density_mesh_core/
coord.rs1use crate::Scalar;
2use serde::{Deserialize, Serialize};
3use std::ops::{Add, Div, Mul, Neg, Sub};
4
5#[derive(Debug, Default, Copy, Clone, PartialEq, Serialize, Deserialize)]
20pub struct Coord {
21 pub x: Scalar,
23 pub y: Scalar,
25}
26
27impl Coord {
28 #[inline]
34 pub fn new(x: Scalar, y: Scalar) -> Self {
35 Self { x, y }
36 }
37
38 #[inline]
40 pub fn sqr_magnitude(self) -> Scalar {
41 self.x * self.x + self.y * self.y
42 }
43
44 #[inline]
46 pub fn magnitude(self) -> Scalar {
47 self.sqr_magnitude().sqrt()
48 }
49
50 #[inline]
52 pub fn normalized(self) -> Self {
53 self / self.magnitude()
54 }
55
56 #[inline]
71 pub fn dot(self, other: Self) -> Scalar {
72 self.x * other.x + self.y * other.y
73 }
74
75 #[inline]
83 pub fn right(self) -> Self {
84 Self {
85 x: self.y,
86 y: -self.x,
87 }
88 }
89
90 #[inline]
101 pub fn is_left_wrt_line(self, from: Self, to: Self) -> i8 {
102 ((to.x - from.x) * (self.y - from.y) - (self.x - from.x) * (to.y - from.y)) as i8
103 }
104}
105
106impl Add for Coord {
107 type Output = Self;
108
109 fn add(self, other: Self) -> Self {
110 Self {
111 x: self.x + other.x,
112 y: self.y + other.y,
113 }
114 }
115}
116
117impl Add<Scalar> for Coord {
118 type Output = Self;
119
120 fn add(self, other: Scalar) -> Self {
121 Self {
122 x: self.x + other,
123 y: self.y + other,
124 }
125 }
126}
127
128impl Sub for Coord {
129 type Output = Self;
130
131 fn sub(self, other: Self) -> Self {
132 Self {
133 x: self.x - other.x,
134 y: self.y - other.y,
135 }
136 }
137}
138
139impl Sub<Scalar> for Coord {
140 type Output = Self;
141
142 fn sub(self, other: Scalar) -> Self {
143 Self {
144 x: self.x - other,
145 y: self.y - other,
146 }
147 }
148}
149
150impl Mul for Coord {
151 type Output = Self;
152
153 fn mul(self, other: Self) -> Self {
154 Self {
155 x: self.x * other.x,
156 y: self.y * other.y,
157 }
158 }
159}
160
161impl Mul<Scalar> for Coord {
162 type Output = Self;
163
164 fn mul(self, other: Scalar) -> Self {
165 Self {
166 x: self.x * other,
167 y: self.y * other,
168 }
169 }
170}
171
172impl Div for Coord {
173 type Output = Self;
174
175 fn div(self, other: Self) -> Self {
176 Self {
177 x: self.x / other.x,
178 y: self.y / other.y,
179 }
180 }
181}
182
183impl Div<Scalar> for Coord {
184 type Output = Self;
185
186 fn div(self, other: Scalar) -> Self {
187 Self {
188 x: self.x / other,
189 y: self.y / other,
190 }
191 }
192}
193
194impl Neg for Coord {
195 type Output = Self;
196
197 fn neg(self) -> Self {
198 Self {
199 x: -self.x,
200 y: -self.y,
201 }
202 }
203}