1use mathverse_core::constants::{PI, TAU};
4
5#[derive(Debug, Clone, Copy, PartialEq)]
7pub struct Point2 {
8 pub x: f64,
9 pub y: f64,
10}
11
12impl Point2 {
13 pub fn new(x: f64, y: f64) -> Self {
14 Point2 { x, y }
15 }
16 pub fn distance_to(self, other: Point2) -> f64 {
18 let dx = self.x - other.x;
19 let dy = self.y - other.y;
20 (dx * dx + dy * dy).sqrt()
21 }
22 pub fn translate(self, dx: f64, dy: f64) -> Point2 {
24 Point2::new(self.x + dx, self.y + dy)
25 }
26 pub fn scale(self, s: f64) -> Point2 {
28 Point2::new(self.x * s, self.y * s)
29 }
30 pub fn rotate(self, angle: f64) -> Point2 {
32 let (s, c) = angle.sin_cos();
33 Point2::new(c * self.x - s * self.y, s * self.x + c * self.y)
34 }
35 pub fn rotate_around(self, angle: f64, center: Point2) -> Point2 {
37 self.translate(-center.x, -center.y).rotate(angle).translate(center.x, center.y)
38 }
39}
40
41#[derive(Debug, Clone, Copy, PartialEq)]
43pub struct Circle {
44 pub center: Point2,
45 pub radius: f64,
46}
47
48impl Circle {
49 pub fn new(center: Point2, radius: f64) -> Circle {
51 assert!(radius >= 0.0, "circle radius must be non-negative");
52 Circle { center, radius }
53 }
54 pub fn area(&self) -> f64 {
55 PI * self.radius * self.radius
56 }
57 pub fn perimeter(&self) -> f64 {
58 TAU * self.radius
59 }
60 pub fn centroid(&self) -> Point2 {
61 self.center
62 }
63 pub fn contains(&self, p: Point2) -> bool {
64 self.center.distance_to(p) <= self.radius
65 }
66 pub fn intersects(&self, other: &Circle) -> bool {
68 self.center.distance_to(other.center) <= self.radius + other.radius
69 }
70 pub fn intersection_points(&self, other: &Circle) -> Vec<Point2> {
73 let d = self.center.distance_to(other.center);
74 let (r1, r2) = (self.radius, other.radius);
75 if d > r1 + r2 || d < (r1 - r2).abs() || d == 0.0 {
76 return vec![];
77 }
78 let a = (r1 * r1 - r2 * r2 + d * d) / (2.0 * d);
79 let h2 = r1 * r1 - a * a;
80 let p = self.center.translate(a * (other.center.x - self.center.x) / d, a * (other.center.y - self.center.y) / d);
81 if h2 <= 1e-14 {
82 return vec![p];
83 }
84 let h = h2.sqrt();
85 let dx = h * (other.center.y - self.center.y) / d;
86 let dy = h * (other.center.x - self.center.x) / d;
87 vec![Point2::new(p.x + dx, p.y - dy), Point2::new(p.x - dx, p.y + dy)]
88 }
89}
90
91#[derive(Debug, Clone, Copy, PartialEq)]
93pub struct Triangle {
94 pub a: Point2,
95 pub b: Point2,
96 pub c: Point2,
97}
98
99impl Triangle {
100 pub fn new(a: Point2, b: Point2, c: Point2) -> Triangle {
101 Triangle { a, b, c }
102 }
103 pub fn area(&self) -> f64 {
105 ((self.b.x - self.a.x) * (self.c.y - self.a.y) - (self.c.x - self.a.x) * (self.b.y - self.a.y)).abs() / 2.0
106 }
107 pub fn perimeter(&self) -> f64 {
108 self.a.distance_to(self.b) + self.b.distance_to(self.c) + self.c.distance_to(self.a)
109 }
110 pub fn centroid(&self) -> Point2 {
111 Point2::new((self.a.x + self.b.x + self.c.x) / 3.0, (self.a.y + self.b.y + self.c.y) / 3.0)
112 }
113}
114
115#[derive(Debug, Clone, Copy, PartialEq)]
117pub struct Rectangle {
118 pub x: f64,
119 pub y: f64,
120 pub width: f64,
121 pub height: f64,
122}
123
124impl Rectangle {
125 pub fn new(x: f64, y: f64, width: f64, height: f64) -> Rectangle {
127 assert!(width >= 0.0 && height >= 0.0, "rectangle dimensions must be non-negative");
128 Rectangle { x, y, width, height }
129 }
130 pub fn area(&self) -> f64 {
131 self.width * self.height
132 }
133 pub fn perimeter(&self) -> f64 {
134 2.0 * (self.width + self.height)
135 }
136 pub fn centroid(&self) -> Point2 {
137 Point2::new(self.x + self.width / 2.0, self.y + self.height / 2.0)
138 }
139 pub fn contains(&self, p: Point2) -> bool {
140 p.x >= self.x && p.x <= self.x + self.width && p.y >= self.y && p.y <= self.y + self.height
141 }
142 pub fn intersects(&self, other: &Rectangle) -> bool {
144 self.x < other.x + other.width
145 && other.x < self.x + self.width
146 && self.y < other.y + other.height
147 && other.y < self.y + self.height
148 }
149 pub fn rotated_polygon(&self, angle: f64) -> Polygon {
151 let c = self.centroid();
152 let corners = [
153 Point2::new(self.x, self.y),
154 Point2::new(self.x + self.width, self.y),
155 Point2::new(self.x + self.width, self.y + self.height),
156 Point2::new(self.x, self.y + self.height),
157 ];
158 Polygon::new(corners.iter().map(|p| p.rotate_around(angle, c)).collect())
159 }
160}
161
162#[derive(Debug, Clone, PartialEq)]
164pub struct Polygon {
165 pub points: Vec<Point2>,
166}
167
168impl Polygon {
169 pub fn new(points: Vec<Point2>) -> Polygon {
171 assert!(points.len() >= 3, "polygon needs at least 3 points");
172 Polygon { points }
173 }
174 pub fn area(&self) -> f64 {
176 let mut s = 0.0;
177 let n = self.points.len();
178 for i in 0..n {
179 let (a, b) = (self.points[i], self.points[(i + 1) % n]);
180 s += a.x * b.y - b.x * a.y;
181 }
182 s.abs() / 2.0
183 }
184 pub fn perimeter(&self) -> f64 {
185 let n = self.points.len();
186 (0..n).map(|i| self.points[i].distance_to(self.points[(i + 1) % n])).sum()
187 }
188 pub fn centroid(&self) -> Point2 {
190 let n = self.points.len();
191 let mut cx = 0.0;
192 let mut cy = 0.0;
193 let mut a2 = 0.0;
194 for i in 0..n {
195 let (p, q) = (self.points[i], self.points[(i + 1) % n]);
196 let cross = p.x * q.y - q.x * p.y;
197 a2 += cross;
198 cx += (p.x + q.x) * cross;
199 cy += (p.y + q.y) * cross;
200 }
201 if a2 == 0.0 {
202 return Point2::new(0.0, 0.0);
203 }
204 Point2::new(cx / (3.0 * a2), cy / (3.0 * a2))
205 }
206}
207
208#[derive(Debug, Clone, Copy, PartialEq)]
210pub struct Ellipse {
211 pub center: Point2,
212 pub rx: f64,
213 pub ry: f64,
214}
215
216impl Ellipse {
217 pub fn new(center: Point2, rx: f64, ry: f64) -> Ellipse {
219 assert!(rx >= 0.0 && ry >= 0.0, "ellipse semi-axes must be non-negative");
220 Ellipse { center, rx, ry }
221 }
222 pub fn area(&self) -> f64 {
223 PI * self.rx * self.ry
224 }
225 pub fn perimeter(&self) -> f64 {
227 let s = self.rx + self.ry;
228 PI * (3.0 * s - ((3.0 * self.rx + self.ry) * (self.rx + 3.0 * self.ry)).sqrt())
229 }
230 pub fn centroid(&self) -> Point2 {
231 self.center
232 }
233 pub fn contains(&self, p: Point2) -> bool {
234 let dx = (p.x - self.center.x) / self.rx;
235 let dy = (p.y - self.center.y) / self.ry;
236 dx * dx + dy * dy <= 1.0
237 }
238}
239
240#[cfg(test)]
241mod tests {
242 use super::*;
243
244 #[test]
245 fn point_ops() {
246 let p = Point2::new(1.0, 2.0);
247 assert_eq!(p.distance_to(Point2::new(4.0, 6.0)), 5.0);
248 assert_eq!(p.translate(2.0, -1.0), Point2::new(3.0, 1.0));
249 assert_eq!(p.scale(2.0), Point2::new(2.0, 4.0));
250 let r = Point2::new(1.0, 0.0).rotate(PI / 2.0);
251 assert!((r.x - 0.0).abs() < 1e-12 && (r.y - 1.0).abs() < 1e-12);
252 assert_eq!(p.rotate_around(PI, Point2::new(1.0, 2.0)), Point2::new(1.0, 2.0));
253 }
254
255 #[test]
256 fn circle() {
257 let c = Circle::new(Point2::new(0.0, 0.0), 2.0);
258 assert!((c.area() - 4.0 * PI).abs() < 1e-12);
259 assert!((c.perimeter() - 4.0 * PI).abs() < 1e-12);
260 assert!(c.contains(Point2::new(1.0, 1.0)));
261 assert!(!c.contains(Point2::new(2.0, 2.0)));
262 let a = Circle::new(Point2::new(0.0, 0.0), 1.0);
264 let b = Circle::new(Point2::new(1.0, 0.0), 1.0);
265 assert!(a.intersects(&b));
266 let pts = a.intersection_points(&b);
267 assert_eq!(pts.len(), 2);
268 for p in &pts {
269 assert!((p.x - 0.5).abs() < 1e-12);
270 assert!((p.y.abs() - 3.0f64.sqrt() / 2.0).abs() < 1e-12);
271 }
272 let c = Circle::new(Point2::new(2.0, 0.0), 1.0);
274 assert_eq!(a.intersection_points(&c).len(), 1);
275 let d = Circle::new(Point2::new(5.0, 0.0), 1.0);
277 assert!(!a.intersects(&d));
278 assert!(a.intersection_points(&d).is_empty());
279 }
280
281 #[test]
282 fn triangle() {
283 let t = Triangle::new(Point2::new(0.0, 0.0), Point2::new(4.0, 0.0), Point2::new(0.0, 3.0));
284 assert_eq!(t.area(), 6.0);
285 assert_eq!(t.perimeter(), 12.0);
286 let c = t.centroid();
287 assert!((c.x - 4.0 / 3.0).abs() < 1e-12 && (c.y - 1.0).abs() < 1e-12);
288 }
289
290 #[test]
291 fn rectangle() {
292 let r = Rectangle::new(0.0, 0.0, 3.0, 4.0);
293 assert_eq!(r.area(), 12.0);
294 assert_eq!(r.perimeter(), 14.0);
295 assert_eq!(r.centroid(), Point2::new(1.5, 2.0));
296 assert!(r.contains(Point2::new(1.0, 2.0)));
297 assert!(!r.contains(Point2::new(-1.0, 0.0)));
298 assert!(r.intersects(&Rectangle::new(2.0, 2.0, 2.0, 2.0)));
299 assert!(!r.intersects(&Rectangle::new(4.0, 4.0, 1.0, 1.0)));
300 }
301
302 #[test]
303 fn polygon() {
304 let sq = Polygon::new(vec![
305 Point2::new(0.0, 0.0),
306 Point2::new(2.0, 0.0),
307 Point2::new(2.0, 2.0),
308 Point2::new(0.0, 2.0),
309 ]);
310 assert_eq!(sq.area(), 4.0);
311 assert_eq!(sq.perimeter(), 8.0);
312 assert_eq!(sq.centroid(), Point2::new(1.0, 1.0));
313 }
314
315 #[test]
316 fn ellipse() {
317 let e = Ellipse::new(Point2::new(0.0, 0.0), 2.0, 1.0);
318 assert!((e.area() - 2.0 * PI).abs() < 1e-12);
319 assert!(e.contains(Point2::new(1.9, 0.0)));
320 assert!(!e.contains(Point2::new(2.1, 0.0)));
321 assert!(!e.contains(Point2::new(0.0, 1.1)));
322 }
323}