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 378 of file geometry.h.

379 {
382 float dx = b.x - a.x;
383 float dy = b.y - a.y;
384 float len_sq = dx * dx + dy * dy;
385
388 const bool is_zero = (len_sq == 0.0f);
390
391 if (is_zero) {
392 // a == b, the segment is a point
393 out_proj = a;
394 dx = p.x - a.x;
395 dy = p.y - a.y;
396 return sqrt(dx * dx + dy * dy);
397 }
398
399 // Project point p onto the line segment, computing parameter t
400 float t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / len_sq;
401
402 // Clamp t to [0,1] to stay within the segment
403 if (t < 0.0f)
404 t = 0.0f;
405 else if (t > 1.0f)
406 t = 1.0f;
407
408 // Find the closest point
409 out_proj.x = a.x + t * dx;
410 out_proj.y = a.y + t * dy;
411
412 dx = p.x - out_proj.x;
413 dy = p.y - out_proj.y;
414 return sqrt(dx * dx + dy * dy);
415 }
#define FL_DISABLE_WARNING(warning)

References fl::equal(), FL_DISABLE_WARNING, FL_DISABLE_WARNING_POP, FL_DISABLE_WARNING_PUSH, 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: