FastLED 3.9.15
Loading...
Searching...
No Matches

◆ distance_to_line_with_point()

template<typename T>
static float fl::line_xy< T >::distance_to_line_with_point ( vec2< T > p,
vec2< T > a,
vec2< T > b,
vec2< T > * out_projected )
inlinestaticprivate

Definition at line 396 of file geometry.h.

397 {
400 float dx = b.x - a.x;
401 float dy = b.y - a.y;
402 float len_sq = dx * dx + dy * dy;
403
406 const bool is_zero = (len_sq == 0.0f);
408
409 if (is_zero) {
410 // a == b, the segment is a point
411 out_proj = a;
412 dx = p.x - a.x;
413 dy = p.y - a.y;
414 return fl::sqrt(dx * dx + dy * dy);
415 }
416
417 // Project point p onto the line segment, computing parameter t
418 float t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / len_sq;
419
420 // Clamp t to [0,1] to stay within the segment
421 if (t < 0.0f)
422 t = 0.0f;
423 else if (t > 1.0f)
424 t = 1.0f;
425
426 // Find the closest point
427 out_proj.x = a.x + t * dx;
428 out_proj.y = a.y + t * dy;
429
430 dx = p.x - out_proj.x;
431 dy = p.y - out_proj.y;
432 return fl::sqrt(dx * dx + dy * dy);
433 }
constexpr enable_if< is_fixed_point< T >::value, T >::type sqrt(T x) FL_NOEXCEPT
#define FL_DISABLE_WARNING(warning)

References fl::equal(), FL_DISABLE_WARNING, FL_DISABLE_WARNING_POP, FL_DISABLE_WARNING_PUSH, FL_NOEXCEPT, fl::sqrt(), fl::t, fl::vec2< T >::x, and fl::vec2< T >::y.

Referenced by distance_to().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: