Reduce operations in RayCast2d::circle_intersection_at using cross product (#19103)

# Objective

- Using the cross product to compute the perpendicular distance reduces
one multiplication and two additions.
This commit is contained in:
Kumikaya 2025-05-27 01:22:14 +08:00 committed by GitHub
parent 1c8d2ee3e1
commit ee3d9b2a88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -78,8 +78,8 @@ impl RayCast2d {
pub fn circle_intersection_at(&self, circle: &BoundingCircle) -> Option<f32> {
let offset = self.ray.origin - circle.center;
let projected = offset.dot(*self.ray.direction);
let closest_point = offset - projected * *self.ray.direction;
let distance_squared = circle.radius().squared() - closest_point.length_squared();
let cross = offset.perp_dot(*self.ray.direction);
let distance_squared = circle.radius().squared() - cross.squared();
if distance_squared < 0.
|| ops::copysign(projected.squared(), -projected) < -distance_squared
{