| 466 | // distance from the tip of this vector to a line segment specified by two vectors |
| 467 | template <typename T> |
| 468 | T 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 |
| 493 | template <typename T> |
no test coverage detected