diff --git a/libraries/AP_Math/vector2.cpp b/libraries/AP_Math/vector2.cpp index 2518d678c2..b4cd504ff5 100644 --- a/libraries/AP_Math/vector2.cpp +++ b/libraries/AP_Math/vector2.cpp @@ -329,6 +329,27 @@ Vector2 Vector2::closest_point(const Vector2 &p, const Vector2 &v, c } } +// w defines a line segment from the origin +// p is a point +// returns the square of the closest distance between the radial and the point +template +float Vector2::closest_distance_between_radial_and_point_squared(const Vector2 &w, + const Vector2 &p) +{ + const Vector2 closest = closest_point(p, w); + return (closest - p).length_squared(); +} + +// w defines a line segment from the origin +// p is a point +// returns the closest distance between the radial and the point +template +float Vector2::closest_distance_between_radial_and_point(const Vector2 &w, + const Vector2 &p) +{ + return sqrtf(closest_distance_between_radial_and_point_squared(w,p)); +} + // only define for float template float Vector2::length_squared(void) const; template float Vector2::length(void) const; @@ -354,6 +375,8 @@ template float Vector2::angle(void) const; template bool Vector2::segment_intersection(const Vector2& seg1_start, const Vector2& seg1_end, const Vector2& seg2_start, const Vector2& seg2_end, Vector2& intersection); template bool Vector2::circle_segment_intersection(const Vector2& seg_start, const Vector2& seg_end, const Vector2& circle_center, float radius, Vector2& intersection); template Vector2 Vector2::closest_point(const Vector2 &p, const Vector2 &v, const Vector2 &w); +template float Vector2::closest_distance_between_radial_and_point_squared(const Vector2 &w, const Vector2 &p); +template float Vector2::closest_distance_between_radial_and_point(const Vector2 &w, const Vector2 &p); template bool Vector2::operator ==(const Vector2 &v) const; diff --git a/libraries/AP_Math/vector2.h b/libraries/AP_Math/vector2.h index 7b9906cac7..8e4fcf7b3a 100644 --- a/libraries/AP_Math/vector2.h +++ b/libraries/AP_Math/vector2.h @@ -163,16 +163,17 @@ struct Vector2 */ static Vector2 closest_point(const Vector2 &p, const Vector2 &v, const Vector2 &w); + // w defines a line segment from the origin + // p is a point + // returns the square of the closest distance between the radial and the point + static float closest_distance_between_radial_and_point_squared(const Vector2 &w, + const Vector2 &p); + // w defines a line segment from the origin // p is a point // returns the closest distance between the radial and the point static float closest_distance_between_radial_and_point(const Vector2 &w, - const Vector2 &p) - { - const Vector2 closest = closest_point(p, Vector2(0,0), w); - const Vector2 delta = closest - p; - return delta.length(); - } + const Vector2 &p); // find the intersection between two line segments // returns true if they intersect, false if they do not