MCPcopy Create free account
hub / github.com/ArduPilot/ardupilot / distance_to_segment

Method distance_to_segment

libraries/AP_Math/vector3.cpp:468–490  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

466// distance from the tip of this vector to a line segment specified by two vectors
467template <typename T>
468T Vector3<T>::distance_to_segment(const Vector3<T> &seg_start, const Vector3<T> &seg_end) const
469{
470 // triangle side lengths
471 const T a = (*this-seg_start).length();
472 const T b = (seg_start-seg_end).length();
473 const T c = (seg_end-*this).length();
474
475 // protect against divide by zero later
476 if (::is_zero(b)) {
477 return 0.0f;
478 }
479
480 // semiperimeter of triangle
481 const T s = (a+b+c) * 0.5f;
482
483 T area_squared = s*(s-a)*(s-b)*(s-c);
484 // area must be constrained above 0 because a triangle could have 3 points could be on a line and float rounding could push this under 0
485 if (area_squared < 0.0f) {
486 area_squared = 0.0f;
487 }
488 const T area = safe_sqrt(area_squared);
489 return 2.0f*area/b;
490}
491
492// Shortest distance between point(p) to a point contained in the line segment defined by w1,w2
493template <typename T>

Callers 1

Calls 3

safe_sqrtFunction · 0.85
is_zeroFunction · 0.70
lengthMethod · 0.45

Tested by

no test coverage detected