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

359 {
362 float dx = b.x - a.x;
363 float dy = b.y - a.y;
364 float len_sq = dx * dx + dy * dy;
365
366#pragma GCC diagnostic push
367#pragma GCC diagnostic ignored "-Wfloat-equal"
368 const bool is_zero = (len_sq == 0.0f);
369#pragma GCC diagnostic pop
370
371 if (is_zero) {
372 // a == b, the segment is a point
373 out_proj = a;
374 dx = p.x - a.x;
375 dy = p.y - a.y;
376 return sqrt(dx * dx + dy * dy);
377 }
378
379 // Project point p onto the line segment, computing parameter t
380 float t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / len_sq;
381
382 // Clamp t to [0,1] to stay within the segment
383 if (t < 0.0f)
384 t = 0.0f;
385 else if (t > 1.0f)
386 t = 1.0f;
387
388 // Find the closest point
389 out_proj.x = a.x + t * dx;
390 out_proj.y = a.y + t * dy;
391
392 dx = p.x - out_proj.x;
393 dy = p.y - out_proj.y;
394 return sqrt(dx * dx + dy * dy);
395 }

References p, fl::vec2< T >::x, and fl::vec2< T >::y.

Referenced by distance_to().

+ Here is the caller graph for this function: