1use mathverse_core::constants::TAU;
4
5use super::shapes2d::Point2;
6
7#[derive(Debug, Clone, Copy, PartialEq)]
13pub struct LineSegment2 {
14 pub a: Point2,
15 pub b: Point2,
16}
17
18impl LineSegment2 {
19 pub fn new(a: Point2, b: Point2) -> Self {
20 Self { a, b }
21 }
22 pub fn length(self) -> f64 {
23 self.a.distance_to(self.b)
24 }
25 pub fn midpoint(self) -> Point2 {
26 Point2::new((self.a.x + self.b.x) / 2.0, (self.a.y + self.b.y) / 2.0)
27 }
28 pub fn angle(self) -> f64 {
30 (self.b.y - self.a.y).atan2(self.b.x - self.a.x)
31 }
32 pub fn closest_point(self, p: Point2) -> Point2 {
34 let dx = self.b.x - self.a.x;
35 let dy = self.b.y - self.a.y;
36 let len_sq = dx * dx + dy * dy;
37 if len_sq < 1e-30 {
38 return self.a;
39 }
40 let t = ((p.x - self.a.x) * dx + (p.y - self.a.y) * dy) / len_sq;
41 let t = t.clamp(0.0, 1.0);
42 Point2::new(self.a.x + t * dx, self.a.y + t * dy)
43 }
44 pub fn distance_to_point(self, p: Point2) -> f64 {
46 p.distance_to(self.closest_point(p))
47 }
48}
49
50#[derive(Debug, Clone, Copy, PartialEq)]
56pub struct Ray2 {
57 pub origin: Point2,
58 pub dir: Point2,
59}
60
61impl Ray2 {
62 pub fn new(origin: Point2, dir: Point2) -> Self {
64 assert!(
65 (dir.x * dir.x + dir.y * dir.y) > 1e-30,
66 "ray direction must be non-zero"
67 );
68 Self { origin, dir }
69 }
70 pub fn point_at(self, t: f64) -> Point2 {
71 Point2::new(self.origin.x + t * self.dir.x, self.origin.y + t * self.dir.y)
72 }
73 pub fn distance_to_point(self, p: Point2) -> f64 {
75 let vx = p.x - self.origin.x;
76 let vy = p.y - self.origin.y;
77 let t = (vx * self.dir.x + vy * self.dir.y) / (self.dir.x * self.dir.x + self.dir.y * self.dir.y);
78 if t <= 0.0 {
79 return p.distance_to(self.origin);
80 }
81 p.distance_to(self.point_at(t))
82 }
83}
84
85#[derive(Debug, Clone, Copy, PartialEq)]
91pub struct Arc {
92 pub center: Point2,
93 pub radius: f64,
94 pub start: f64,
95 pub sweep: f64,
96}
97
98impl Arc {
99 pub fn new(center: Point2, radius: f64, start: f64, sweep: f64) -> Self {
101 assert!(radius >= 0.0, "arc radius must be non-negative");
102 Self {
103 center,
104 radius,
105 start,
106 sweep,
107 }
108 }
109 pub fn start_point(self) -> Point2 {
110 Point2::new(
111 self.center.x + self.radius * self.start.cos(),
112 self.center.y + self.radius * self.start.sin(),
113 )
114 }
115 pub fn end_point(self) -> Point2 {
116 let a = self.start + self.sweep;
117 Point2::new(
118 self.center.x + self.radius * a.cos(),
119 self.center.y + self.radius * a.sin(),
120 )
121 }
122 pub fn length(self) -> f64 {
124 self.radius * self.sweep.abs()
125 }
126 pub fn area(self) -> f64 {
128 0.5 * self.radius * self.radius * self.sweep.abs()
129 }
130 pub fn contains(self, p: Point2) -> bool {
132 let dx = p.x - self.center.x;
133 let dy = p.y - self.center.y;
134 let dist = (dx * dx + dy * dy).sqrt();
135 if (dist - self.radius).abs() > 1e-10 {
136 return false;
137 }
138 let mut angle = dy.atan2(dx);
139 let mut start = self.start;
140 let mut end = self.start + self.sweep;
141 while angle < 0.0 {
143 angle += TAU;
144 }
145 while angle >= TAU {
146 angle -= TAU;
147 }
148 while start < 0.0 {
149 start += TAU;
150 }
151 while start >= TAU {
152 start -= TAU;
153 }
154 while end < 0.0 {
155 end += TAU;
156 }
157 while end >= TAU {
158 end -= TAU;
159 }
160 if self.sweep >= 0.0 {
161 if start <= end {
162 angle >= start && angle <= end
163 } else {
164 angle >= start || angle <= end
165 }
166 } else {
167 if end <= start {
168 angle >= end && angle <= start
169 } else {
170 angle >= end || angle <= start
171 }
172 }
173 }
174}
175
176#[derive(Debug, Clone, Copy, PartialEq)]
182pub struct Sector {
183 pub center: Point2,
184 pub radius: f64,
185 pub start: f64,
186 pub sweep: f64,
187}
188
189impl Sector {
190 pub fn new(center: Point2, radius: f64, start: f64, sweep: f64) -> Self {
191 assert!(radius >= 0.0, "sector radius must be non-negative");
192 Self {
193 center,
194 radius,
195 start,
196 sweep,
197 }
198 }
199 pub fn area(self) -> f64 {
200 0.5 * self.radius * self.radius * self.sweep.abs()
201 }
202 pub fn perimeter(self) -> f64 {
203 2.0 * self.radius + self.radius * self.sweep.abs()
204 }
205 pub fn contains(self, p: Point2) -> bool {
206 let dx = p.x - self.center.x;
207 let dy = p.y - self.center.y;
208 let dist = (dx * dx + dy * dy).sqrt();
209 if dist > self.radius + 1e-10 {
210 return false;
211 }
212 let angle = dy.atan2(dx);
213 let start = self.start;
214 let sweep = self.sweep;
215 if sweep >= 0.0 {
216 let mut a = angle - start;
217 while a < 0.0 {
218 a += TAU;
219 }
220 while a >= TAU {
221 a -= TAU;
222 }
223 a <= sweep
224 } else {
225 let mut a = angle - start;
226 while a < 0.0 {
227 a += TAU;
228 }
229 while a >= TAU {
230 a -= TAU;
231 }
232 a >= sweep + TAU || a == 0.0
233 }
234 }
235 pub fn arc(self) -> Arc {
237 Arc::new(self.center, self.radius, self.start, self.sweep)
238 }
239}
240
241#[derive(Debug, Clone, Copy, PartialEq)]
247pub struct CircularSegment {
248 pub center: Point2,
249 pub radius: f64,
250 pub start: f64,
251 pub sweep: f64,
252}
253
254impl CircularSegment {
255 pub fn new(center: Point2, radius: f64, start: f64, sweep: f64) -> Self {
256 assert!(radius >= 0.0, "segment radius must be non-negative");
257 Self {
258 center,
259 radius,
260 start,
261 sweep,
262 }
263 }
264 pub fn area(self) -> f64 {
266 let sector = 0.5 * self.radius * self.radius * self.sweep.abs();
267 let triangle =
268 0.5 * self.radius * self.radius * self.sweep.abs().sin();
269 sector - triangle
270 }
271 pub fn chord_length(self) -> f64 {
273 2.0 * self.radius * (self.sweep.abs() / 2.0).sin()
274 }
275 pub fn arc_length(self) -> f64 {
277 self.radius * self.sweep.abs()
278 }
279 pub fn perimeter(self) -> f64 {
281 self.chord_length() + self.arc_length()
282 }
283}
284
285#[derive(Debug, Clone, PartialEq)]
291pub struct Polyline {
292 pub points: Vec<Point2>,
293 pub closed: bool,
294}
295
296impl Polyline {
297 pub fn open(points: Vec<Point2>) -> Self {
298 Self {
299 points,
300 closed: false,
301 }
302 }
303 pub fn closed(points: Vec<Point2>) -> Self {
304 Self {
305 points,
306 closed: true,
307 }
308 }
309 pub fn total_length(&self) -> f64 {
310 self.points
311 .windows(2)
312 .map(|w| w[0].distance_to(w[1]))
313 .sum::<f64>()
314 + if self.closed && self.points.len() > 1 {
315 self.points.last().unwrap().distance_to(self.points[0])
316 } else {
317 0.0
318 }
319 }
320 pub fn point_at(&self, t: f64) -> Point2 {
322 let total = self.total_length();
323 let target = t * total;
324 let mut acc = 0.0;
325 for w in self.points.windows(2) {
326 let seg = w[0].distance_to(w[1]);
327 if acc + seg >= target {
328 let frac = if seg > 1e-30 {
329 (target - acc) / seg
330 } else {
331 0.0
332 };
333 return Point2::new(
334 w[0].x + frac * (w[1].x - w[0].x),
335 w[0].y + frac * (w[1].y - w[0].y),
336 );
337 }
338 acc += seg;
339 }
340 if self.closed && self.points.len() > 1 {
341 *self.points.last().unwrap()
342 } else {
343 *self.points.last().unwrap()
344 }
345 }
346}
347
348#[derive(Debug, Clone, Copy, PartialEq)]
354pub struct BezierCurve {
355 pub p0: Point2,
356 pub p1: Point2,
357 pub p2: Point2,
358 pub p3: Point2,
359}
360
361impl BezierCurve {
362 pub fn new(p0: Point2, p1: Point2, p2: Point2, p3: Point2) -> Self {
363 Self { p0, p1, p2, p3 }
364 }
365 pub fn point_at(self, t: f64) -> Point2 {
367 let u = 1.0 - t;
368 let u2 = u * u;
369 let u3 = u2 * u;
370 let t2 = t * t;
371 let t3 = t2 * t;
372 Point2::new(
373 u3 * self.p0.x + 3.0 * u2 * t * self.p1.x + 3.0 * u * t2 * self.p2.x + t3 * self.p3.x,
374 u3 * self.p0.y + 3.0 * u2 * t * self.p1.y + 3.0 * u * t2 * self.p2.y + t3 * self.p3.y,
375 )
376 }
377 pub fn length(self) -> f64 {
379 fn approx_len(p0: Point2, p1: Point2, p2: Point2, p3: Point2, depth: u32) -> f64 {
380 let d = p0.distance_to(p3);
381 let chord = d;
382 let poly = p0.distance_to(p1) + p1.distance_to(p2) + p2.distance_to(p3);
383 if (poly - chord).abs() < 1e-6 || depth > 10 {
384 return (chord + poly) / 2.0;
385 }
386 let m01 = Point2::new(
387 (p0.x + p1.x) / 2.0,
388 (p0.y + p1.y) / 2.0,
389 );
390 let m12 = Point2::new(
391 (p1.x + p2.x) / 2.0,
392 (p1.y + p2.y) / 2.0,
393 );
394 let m23 = Point2::new(
395 (p2.x + p3.x) / 2.0,
396 (p2.y + p3.y) / 2.0,
397 );
398 let m012 = Point2::new(
399 (m01.x + m12.x) / 2.0,
400 (m01.y + m12.y) / 2.0,
401 );
402 let m123 = Point2::new(
403 (m12.x + m23.x) / 2.0,
404 (m12.y + m23.y) / 2.0,
405 );
406 let mid = Point2::new(
407 (m012.x + m123.x) / 2.0,
408 (m012.y + m123.y) / 2.0,
409 );
410 approx_len(p0, m01, m012, mid, depth + 1)
411 + approx_len(mid, m123, m23, p3, depth + 1)
412 }
413 approx_len(self.p0, self.p1, self.p2, self.p3, 0)
414 }
415}
416
417#[cfg(test)]
418mod tests {
419 use super::*;
420 use mathverse_core::constants::{PI, TAU};
421 use std::f64::consts::FRAC_PI_2;
422
423 fn pt(x: f64, y: f64) -> Point2 {
424 Point2::new(x, y)
425 }
426
427 #[test]
428 fn segment_basics() {
429 let s = LineSegment2::new(pt(0.0, 0.0), pt(3.0, 4.0));
430 assert!((s.length() - 5.0).abs() < 1e-12);
431 assert_eq!(s.midpoint(), pt(1.5, 2.0));
432 }
433
434 #[test]
435 fn segment_closest_point() {
436 let s = LineSegment2::new(pt(0.0, 0.0), pt(1.0, 0.0));
437 assert_eq!(s.closest_point(pt(0.5, 3.0)), pt(0.5, 0.0));
438 assert_eq!(s.closest_point(pt(-1.0, 0.0)), pt(0.0, 0.0));
439 assert_eq!(s.closest_point(pt(2.0, 0.0)), pt(1.0, 0.0));
440 }
441
442 #[test]
443 fn ray_distance() {
444 let r = Ray2::new(pt(0.0, 0.0), pt(1.0, 0.0));
445 assert!((r.distance_to_point(pt(0.0, 1.0)) - 1.0).abs() < 1e-12);
446 assert!((r.distance_to_point(pt(-1.0, 0.0)) - 1.0).abs() < 1e-12);
447 }
448
449 #[test]
450 fn arc_area() {
451 let a = Arc::new(pt(0.0, 0.0), 2.0, 0.0, core::f64::consts::FRAC_PI_2);
452 assert!((a.area() - PI).abs() < 1e-12);
453 }
454
455 #[test]
456 fn sector_contains() {
457 let s = Sector::new(pt(0.0, 0.0), 1.0, 0.0, FRAC_PI_2);
458 assert!(s.contains(pt(0.5, 0.5)));
459 assert!(!s.contains(pt(2.0, 2.0)));
460 }
461
462 #[test]
463 fn segment_area() {
464 let s = CircularSegment::new(pt(0.0, 0.0), 1.0, 0.0, PI);
465 let expected = PI / 2.0 - 0.0; assert!((s.area() - expected).abs() < 1e-12);
468 }
469
470 #[test]
471 fn polyline_length() {
472 let pl = Polyline::open(vec![pt(0.0, 0.0), pt(1.0, 0.0), pt(1.0, 1.0)]);
473 assert!((pl.total_length() - 2.0).abs() < 1e-12);
474 }
475
476 #[test]
477 fn bezier_at_zero_one() {
478 let b = BezierCurve::new(pt(0.0, 0.0), pt(1.0, 2.0), pt(3.0, 2.0), pt(4.0, 0.0));
479 assert_eq!(b.point_at(0.0), pt(0.0, 0.0));
480 assert_eq!(b.point_at(1.0), pt(4.0, 0.0));
481 }
482}