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

187 {
190 float dx = b.x - a.x;
191 float dy = b.y - a.y;
192 float len_sq = dx * dx + dy * dy;
193
194#pragma GCC diagnostic push
195#pragma GCC diagnostic ignored "-Wfloat-equal"
196 const bool is_zero = (len_sq == 0.0f);
197#pragma GCC diagnostic pop
198
199 if (is_zero) {
200 // a == b, the segment is a point
201 out_proj = a;
202 dx = p.x - a.x;
203 dy = p.y - a.y;
204 return sqrt(dx * dx + dy * dy);
205 }
206
207 // Project point p onto the line segment, computing parameter t
208 float t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / len_sq;
209
210 // Clamp t to [0,1] to stay within the segment
211 if (t < 0.0f)
212 t = 0.0f;
213 else if (t > 1.0f)
214 t = 1.0f;
215
216 // Find the closest point
217 out_proj.x = a.x + t * dx;
218 out_proj.y = a.y + t * dy;
219
220 dx = p.x - out_proj.x;
221 dy = p.y - out_proj.y;
222 return sqrt(dx * dx + dy * dy);
223 }

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

Referenced by distance_to().

+ Here is the caller graph for this function: