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 67 of file traverse_grid.h.
68 {
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
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
110 if (currentX != x1 || currentY != y1) {
111 visitor.visit(x1, y1);
112 }
113}
References ABS, floor(), FLT_MAX, MIN, fl::vec2< T >::x, and fl::vec2< T >::y.