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

◆ traverseGridSegment16()

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

Traverse a grid segment using fixed-point 8.8 arithmetic.

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

UNTESTED!!!!

Definition at line 122 of file traverse_grid.h.

123 {
124 const int16_t FP_SHIFT = 8;
125 const int16_t FP_ONE = 1 << FP_SHIFT;
126 // const int16_t FP_MASK = FP_ONE - 1;
127
128 // Convert to fixed-point (Q8.8), signed
129 int16_t startX_fp = static_cast<int16_t>(start.x * FP_ONE);
130 int16_t startY_fp = static_cast<int16_t>(start.y * FP_ONE);
131 int16_t endX_fp = static_cast<int16_t>(end.x * FP_ONE);
132 int16_t endY_fp = static_cast<int16_t>(end.y * FP_ONE);
133
134 int16_t x0 = startX_fp >> FP_SHIFT;
135 int16_t y0 = startY_fp >> FP_SHIFT;
136 int16_t x1 = endX_fp >> FP_SHIFT;
137 int16_t y1 = endY_fp >> FP_SHIFT;
138
139 int16_t stepX = (x1 > x0) ? 1 : (x1 < x0) ? -1 : 0;
140 int16_t stepY = (y1 > y0) ? 1 : (y1 < y0) ? -1 : 0;
141
142 int16_t deltaX_fp = endX_fp - startX_fp;
143 int16_t deltaY_fp = endY_fp - startY_fp;
144
145 uint16_t absDeltaX_fp =
146 (deltaX_fp != 0) ? static_cast<uint16_t>(
147 ABS((int32_t(FP_ONE) << FP_SHIFT) / deltaX_fp))
148 : UINT16_MAX;
149 uint16_t absDeltaY_fp =
150 (deltaY_fp != 0) ? static_cast<uint16_t>(
151 ABS((int32_t(FP_ONE) << FP_SHIFT) / deltaY_fp))
152 : UINT16_MAX;
153
154 int16_t nextX_fp = (stepX > 0) ? ((x0 + 1) << FP_SHIFT) : (x0 << FP_SHIFT);
155 int16_t nextY_fp = (stepY > 0) ? ((y0 + 1) << FP_SHIFT) : (y0 << FP_SHIFT);
156
157 uint16_t tMaxX_fp =
158 (deltaX_fp != 0)
159 ? static_cast<uint16_t>(
160 ABS(int32_t(nextX_fp - startX_fp)) * absDeltaX_fp >> FP_SHIFT)
161 : UINT16_MAX;
162 uint16_t tMaxY_fp =
163 (deltaY_fp != 0)
164 ? static_cast<uint16_t>(
165 ABS(int32_t(nextY_fp - startY_fp)) * absDeltaY_fp >> FP_SHIFT)
166 : UINT16_MAX;
167
168 const uint16_t maxT_fp = FP_ONE;
169
170 int16_t currentX = x0;
171 int16_t currentY = y0;
172
173 while (true) {
174 visitor.visit(currentX, currentY);
175
176 uint16_t t_fp = (tMaxX_fp < tMaxY_fp) ? tMaxX_fp : tMaxY_fp;
177 if (t_fp > maxT_fp)
178 break;
179
180 if (tMaxX_fp < tMaxY_fp) {
181 tMaxX_fp += absDeltaX_fp;
182 currentX += stepX;
183 } else {
184 tMaxY_fp += absDeltaY_fp;
185 currentY += stepY;
186 }
187 }
188
189 // Ensure the end cell (x1, y1) is visited at least once
190 if (currentX != x1 || currentY != y1) {
191 visitor.visit(x1, y1);
192 }
193}
#define ABS(x)
Definition math_macros.h:19

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

Referenced by traverseGridSegment().

+ Here is the caller graph for this function: