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 68 of file traverse_grid.h.

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

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

+ Here is the call graph for this function: