|
1 | 1 | //! Contains [`Bounded2d`] implementations for [geometric primitives](crate::primitives).
|
2 | 2 |
|
3 |
| -use std::f32::consts::PI; |
| 3 | +use std::f32::consts::{FRAC_PI_2, PI, TAU}; |
4 | 4 |
|
5 |
| -use glam::{Mat2, Vec2}; |
6 | 5 | use smallvec::SmallVec;
|
7 | 6 |
|
8 | 7 | use crate::{
|
9 | 8 | primitives::{
|
10 |
| - BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, CircularSector, CircularSegment, |
11 |
| - Ellipse, Line2d, Plane2d, Polygon, |
12 |
| - Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d, |
| 9 | + Arc2d, BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, CircularSector, CircularSegment, |
| 10 | + Ellipse, Line2d, Plane2d, Polygon, Polyline2d, Rectangle, RegularPolygon, Segment2d, |
| 11 | + Triangle2d, |
13 | 12 | },
|
14 | 13 | Dir2, Mat2, Vec2,
|
15 | 14 | };
|
@@ -40,11 +39,11 @@ fn arc_bounding_points(arc: Arc2d, rotation: f32) -> SmallVec<[Vec2; 7]> {
|
40 | 39 | // The half-angles are measured from a starting point of π/2, being the angle of Vec2::Y.
|
41 | 40 | // Compute the normalized angles of the endpoints with the rotation taken into account, and then
|
42 | 41 | // check if we are looking for an angle that is between or outside them.
|
43 |
| - let left_angle = (PI / 2.0 + arc.half_angle + rotation).rem_euclid(2.0 * PI); |
44 |
| - let right_angle = (PI / 2.0 - arc.half_angle + rotation).rem_euclid(2.0 * PI); |
| 42 | + let left_angle = (FRAC_PI_2 + arc.half_angle + rotation).rem_euclid(TAU); |
| 43 | + let right_angle = (FRAC_PI_2 - arc.half_angle + rotation).rem_euclid(TAU); |
45 | 44 | let inverted = left_angle < right_angle;
|
46 | 45 | for extremum in [Vec2::X, Vec2::Y, Vec2::NEG_X, Vec2::NEG_Y] {
|
47 |
| - let angle = extremum.to_angle().rem_euclid(2.0 * PI); |
| 46 | + let angle = extremum.to_angle().rem_euclid(TAU); |
48 | 47 | // If inverted = true, then right_angle > left_angle, so we are looking for an angle that is not between them.
|
49 | 48 | // There's a chance that this condition fails due to rounding error, if the endpoint angle is juuuust shy of the axis.
|
50 | 49 | // But in that case, the endpoint itself is within rounding error of the axis and will define the bounds just fine.
|
@@ -366,16 +365,16 @@ impl Bounded2d for Capsule2d {
|
366 | 365 |
|
367 | 366 | #[cfg(test)]
|
368 | 367 | mod tests {
|
369 |
| - use std::f32::consts::PI; |
| 368 | + use std::f32::consts::{PI, TAU}; |
370 | 369 |
|
371 | 370 | use approx::assert_abs_diff_eq;
|
372 | 371 | use glam::Vec2;
|
373 | 372 |
|
374 | 373 | use crate::{
|
375 | 374 | bounding::Bounded2d,
|
376 | 375 | primitives::{
|
377 |
| - Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, Direction2d, Ellipse, |
378 |
| - Line2d, Plane2d, Polygon, Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d, |
| 376 | + Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, Ellipse, Line2d, Plane2d, |
| 377 | + Polygon, Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d, |
379 | 378 | },
|
380 | 379 | Dir2,
|
381 | 380 | };
|
@@ -561,7 +560,7 @@ mod tests {
|
561 | 560 | // Test case: An sector whose arc is minor, but whose bounding circle is not the circumcircle of the endpoints and center
|
562 | 561 | TestCase {
|
563 | 562 | name: "1/3rd circle",
|
564 |
| - arc: Arc2d::from_radians(1.0, 2.0 * PI / 3.0), |
| 563 | + arc: Arc2d::from_radians(1.0, TAU / 3.0), |
565 | 564 | translation: Vec2::ZERO,
|
566 | 565 | rotation: 0.0,
|
567 | 566 | aabb_min: Vec2::new(-apothem, 0.0),
|
@@ -713,7 +712,6 @@ mod tests {
|
713 | 712 | assert_eq!(aabb3.max, Vec2::new(f32::MAX / 2.0, f32::MAX / 2.0));
|
714 | 713 |
|
715 | 714 | let bounding_circle = Plane2d::new(Vec2::Y).bounding_circle(translation, 0.0);
|
716 |
| - dbg!(bounding_circle); |
717 | 715 | assert_eq!(bounding_circle.center, translation);
|
718 | 716 | assert_eq!(bounding_circle.radius(), f32::MAX / 2.0);
|
719 | 717 | }
|
|
0 commit comments