Traverse a grid segment using floating point arithmetic.
Traverse a grid segment using fixed-point 8.8 arithmetic.
Useful for testing.
- Template Parameters
-
- Parameters
-
start | start point |
end | end point |
visitor | called for each cell visited. |
Fully tested.
- Template Parameters
-
- Parameters
-
start | start point |
end | end point |
visitor | called for each cell visited. |
Fully tested.
Definition at line 68 of file traverse_grid.h.
69 {
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
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);
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
111 if (currentX != x1 || currentY != y1) {
112 visitor.visit(x1, y1);
113 }
114}
constexpr T * end(T(&array)[N]) noexcept
References ABS, end(), floor(), FLT_MAX, MIN, t, fl::vec2< T >::x, and fl::vec2< T >::y.