FastLED 3.10.8
Loading...
Searching...
No Matches

◆ compute()

vec2f fl::CatmullRomPath::compute ( float alpha)
overridevirtual

Implements fl::XYPathGenerator.

Definition at line 276 of file xypath_impls.cpp.hpp.

276 {
277 const auto &points = params().points;
278
279 // Need at least 2 points to define a path
280 if (points.size() < 2) {
281 // Return origin if not enough points
282 return vec2f(0.0f, 0.0f);
283 }
284
285 // If only 2 points, do linear interpolation
286 if (points.size() == 2) {
287 return vec2f(points[0].x + alpha * (points[1].x - points[0].x),
288 points[0].y + alpha * (points[1].y - points[0].y));
289 }
290
291 // For Catmull-Rom, we need 4 points to interpolate between the middle two
292 // Scale alpha to the number of segments
293 float scaledAlpha = alpha * (points.size() - 1);
294
295 // Determine which segment we're in
296 int segment = static_cast<int>(scaledAlpha);
297
298 // Clamp to valid range
299 if (segment >= static_cast<int>(points.size()) - 1) {
300 segment = points.size() - 2;
301 scaledAlpha = static_cast<float>(segment) + 1.0f;
302 }
303
304 // Get local alpha within this segment [0,1]
305 float t = scaledAlpha - static_cast<float>(segment);
306
307 // Get the four points needed for interpolation
308 vec2f p0, p1, p2, p3;
309
310 // Handle boundary cases
311 if (segment == 0) {
312 // For the first segment, duplicate the first point
313 p0 = points[0];
314 p1 = points[0];
315 p2 = points[1];
316 p3 = (points.size() > 2) ? points[2] : points[1];
317 } else if (segment == static_cast<int>(points.size()) - 2) {
318 // For the last segment, duplicate the last point
319 p0 = (segment > 0) ? points[segment - 1] : points[0];
320 p1 = points[segment];
321 p2 = points[segment + 1];
322 p3 = points[segment + 1];
323 } else {
324 // Normal case - we have points before and after
325 p0 = points[segment - 1];
326 p1 = points[segment];
327 p2 = points[segment + 1];
328 p3 = points[segment + 2];
329 }
330
331 // Perform Catmull-Rom interpolation
332 auto out = interpolate(p0, p1, p2, p3, t);
333 return out;
334}
fl::vector< vec2f > points
CatmullRomParams & params() FL_NO_EXCEPT
vec2f interpolate(const vec2f &p0, const vec2f &p1, const vec2f &p2, const vec2f &p3, float t) const FL_NO_EXCEPT
vec2< float > vec2f
Definition geometry.h:333

References fl::FL_NO_EXCEPT, interpolate(), params(), fl::t, fl::x, and fl::y.

+ Here is the call graph for this function: