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

◆ traverseGridSegment16()

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

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

126 {
127 const i16 FP_SHIFT = 8;
128 const i16 FP_ONE = 1 << FP_SHIFT;
129 // const i16 FP_MASK = FP_ONE - 1;
130
131 // Convert to fixed-point (Q8.8), signed
132 i16 startX_fp = static_cast<i16>(start.x * FP_ONE);
133 i16 startY_fp = static_cast<i16>(start.y * FP_ONE);
134 i16 endX_fp = static_cast<i16>(end.x * FP_ONE);
135 i16 endY_fp = static_cast<i16>(end.y * FP_ONE);
136
137 i16 x0 = startX_fp >> FP_SHIFT;
138 i16 y0 = startY_fp >> FP_SHIFT;
139 i16 x1 = endX_fp >> FP_SHIFT;
140 i16 y1 = endY_fp >> FP_SHIFT;
141
142 i16 stepX = (x1 > x0) ? 1 : (x1 < x0) ? -1 : 0;
143 i16 stepY = (y1 > y0) ? 1 : (y1 < y0) ? -1 : 0;
144
145 i16 deltaX_fp = endX_fp - startX_fp;
146 i16 deltaY_fp = endY_fp - startY_fp;
147
148 // Use (max)() to prevent macro expansion by Arduino.h's max macro
149 u16 absDeltaX_fp =
150 (deltaX_fp != 0) ? static_cast<u16>(
151 fl::abs((i32(FP_ONE) << FP_SHIFT) / deltaX_fp))
153 u16 absDeltaY_fp =
154 (deltaY_fp != 0) ? static_cast<u16>(
155 fl::abs((i32(FP_ONE) << FP_SHIFT) / deltaY_fp))
157
158 i16 nextX_fp = (stepX > 0) ? ((x0 + 1) << FP_SHIFT) : (x0 << FP_SHIFT);
159 i16 nextY_fp = (stepY > 0) ? ((y0 + 1) << FP_SHIFT) : (y0 << FP_SHIFT);
160
161 // Use (max)() to prevent macro expansion by Arduino.h's max macro
162 u16 tMaxX_fp =
163 (deltaX_fp != 0)
164 ? static_cast<u16>(
165 fl::abs(i32(nextX_fp - startX_fp)) * absDeltaX_fp >> FP_SHIFT)
167 u16 tMaxY_fp =
168 (deltaY_fp != 0)
169 ? static_cast<u16>(
170 fl::abs(i32(nextY_fp - startY_fp)) * absDeltaY_fp >> FP_SHIFT)
172
173 const u16 maxT_fp = FP_ONE;
174
175 i16 currentX = x0;
176 i16 currentY = y0;
177
178 while (true) {
179 visitor.visit(currentX, currentY);
180
181 u16 t_fp = (tMaxX_fp < tMaxY_fp) ? tMaxX_fp : tMaxY_fp;
182 if (t_fp > maxT_fp)
183 break;
184
185 if (tMaxX_fp < tMaxY_fp) {
186 tMaxX_fp += absDeltaX_fp;
187 currentX += stepX;
188 } else {
189 tMaxY_fp += absDeltaY_fp;
190 currentY += stepY;
191 }
192 }
193
194 // Ensure the end cell (x1, y1) is visited at least once
195 if (currentX != x1 || currentY != y1) {
196 visitor.visit(x1, y1);
197 }
198}
constexpr T * end(T(&array)[N]) FL_NOEXCEPT
static constexpr i32 FP_ONE
constexpr enable_if< is_fixed_point< T >::value, T >::type abs(T x) FL_NOEXCEPT
static constexpr T max() FL_NOEXCEPT
Definition limits.h:108
value_type y
Definition geometry.h:191
value_type x
Definition geometry.h:190

References abs(), end(), FP_ONE, fl::numeric_limits< T >::max(), fl::vec2< T >::x, and fl::vec2< T >::y.

Referenced by traverseGridSegment().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: