I have a 2 dimensional space and a ray in that space.
I need to get the coordinates of the red rectangles in the order of the numbers.
My attempt:
I have the starting of the ray which is a double.
I have a value yaw which is the direction in radians.
The I did this to get the coordinate of the next red square.
double tanyaw = Math.tan(yaw);
if ((int) (x + 1/tanyaw) == (int) x) {
x += 1/tanyaw;
z += Math.signum(tanyaw);
} else {
x += Math.signum(tanyaw);
z += tanyaw;
}
However:
That was with yaw = 3*pi/8
note that the line was made in a drawing program and is only approximatly correct.
Line equation is:
x = x0 + t*xd
y = y0 + t*yd
For simplicity define that begin of shot leaves at t=0 and the last position to test is at t=1. Then you can simple define step sizes with (assuming that you can simple calculate the end position x1,y1):
xstep = 1/(x1 - x0)
ystep = 1/(y1 - y0)
xpos = x0
ypos = y0
And then simple loop until you hit the intersection or end using:
while(!hitsomething()) {
if (xpos + xstep - x0 < ypos + ystep - y0) {
xpos += xstep;
visit(xpos, ypos);
} else {
ypos += ystep;
visit(xpos, ypos);
}
}