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

◆ traverseGridSegmentFloat()

template<typename GridVisitor>
void fl::traverseGridSegmentFloat ( const vec2f & start,
const vec2f & end,
GridVisitor & visitor )
inline

Traverse a grid segment using floating point arithmetic.

Traverse a grid segment using fixed-point 8.8 arithmetic.

Useful for testing.

Template Parameters
GridVisitor
Parameters
startstart point
endend point
visitorcalled for each cell visited.

Fully tested.

Template Parameters
GridVisitor
Parameters
startstart point
endend point
visitorcalled for each cell visited.

Fully tested.

Definition at line 67 of file traverse_grid.h.

68 {
69 int x0 = static_cast<int>(fl::floor(start.x));
70 int y0 = static_cast<int>(fl::floor(start.y));
71 int x1 = static_cast<int>(fl::floor(end.x));
72 int y1 = static_cast<int>(fl::floor(end.y));
73
74 int stepX = (x1 > x0) ? 1 : (x1 < x0) ? -1 : 0;
75 int stepY = (y1 > y0) ? 1 : (y1 < y0) ? -1 : 0;
76
77 float dx = end.x - start.x;
78 float dy = end.y - start.y;
79
80 float tDeltaX = (dx != 0.0f) ? ABS(1.0f / dx) : FLT_MAX;
81 float tDeltaY = (dy != 0.0f) ? ABS(1.0f / dy) : FLT_MAX;
82
83 float nextX = (stepX > 0) ? (fl::floor(start.x) + 1) : fl::floor(start.x);
84 float nextY = (stepY > 0) ? (fl::floor(start.y) + 1) : fl::floor(start.y);
85
86 float tMaxX = (dx != 0.0f) ? ABS((nextX - start.x) / dx) : FLT_MAX;
87 float tMaxY = (dy != 0.0f) ? ABS((nextY - start.y) / dy) : FLT_MAX;
88
89 float maxT = 1.0f;
90
91 int currentX = x0;
92 int currentY = y0;
93
94 while (true) {
95 visitor.visit(currentX, currentY);
96 float t = MIN(tMaxX, tMaxY);
97 if (t > maxT)
98 break;
99
100 if (tMaxX < tMaxY) {
101 tMaxX += tDeltaX;
102 currentX += stepX;
103 } else {
104 tMaxY += tDeltaY;
105 currentY += stepY;
106 }
107 }
108
109 // Ensure the end cell (x1, y1) is visited at least once
110 if (currentX != x1 || currentY != y1) {
111 visitor.visit(x1, y1);
112 }
113}
#define MIN(a, b)
Definition math_macros.h:15
#define FLT_MAX
Definition math_macros.h:53
#define ABS(x)
Definition math_macros.h:19
T floor(T value)
Definition math.h:11

References ABS, floor(), FLT_MAX, MIN, fl::vec2< T >::x, and fl::vec2< T >::y.

+ Here is the call graph for this function: