You probably meant to use &&, not ||. As it is, the loop will only end when both x2 and y2 become greater than 19 (x2 < 19 || y2 < 19 is equivalent to !(x2 >= 19 && y2 >= 19)). If angle == 0 then x2 will always equal start.first.
I have a problem with the negative value..
The idea with this code was to find an endpoint inside a matrix sixes 19x19.
from the center of this matrix (10,10) will a line with length 9 (hence x2 <19 or y2 < 19) try to find a endpoint at that given angle with the given length/displacement.
So sometimes i get negative values.. but at those cases it should output a proper value which fits the matrix, and is angled with the given angle from (10,10)
Let L be a straight line that forms an angle theta with the horizontal axis and passes through (10, 10), then its equation is y = cos(theta) * (x - 10) + 10 if theta in (-pi / 2; pi / 2). This line crosses these lines at the following points:
x = 0 at (0, cos(theta) * (-10) + 10) = P1
x = 19 at (19, cos(theta) * 9 + 10) = P2
y = 0 at (-10 / cos(theta) + 10, 0) = P3
y = 19 at (9 / cos(theta) + 10, 19) = P4
If P3.x > 19 || P3.x < 0 then the line is approximately horizontal, and will leave the box at P1 and P2.
If P3.x < 19 && P3.x > 0 then the line is approximately vertical, and will leave the box at P3 and P4.
If P3.x == 19 || P3.x == 0 the line is exactly diagonal and either pair of points may be used.
I'm not sure whether you want the maximum length which will fit within those boundaries. That would make a diagonal longer than a horizontal or vertical. Ot is it like the radius of a circle, a fixed length.
If you start from (10,10), 19 would be an upper limit. 1 would be the lower limit. You'd need to check both.
maybe?
Perhaps I misunderstood. I used a starting point of 10, 10
I think this way the value can never become negative.
I'm not sure about the change from while to do-while. it was just an idea I had to make sure that it started off from x2 = start.first and y2 = start.second.
The range [pi; 2*pi] is equivalent to the range [0; pi], only the order of the solutions is inverted. I.e. if f(theta) = sequence[a, b], then f(theta + pi) = sequence[b, a]. If order doesn't matter then f(theta) = f(theta + pi).
in between is computed using the equation.. and x = 9..
I don't understand this sentence.
what about -pi/4 and pi/4..
Those values are explicitly included in my method. Do you mean (+/-)pi/2? For (+/-)pi/2 the solutions are known: (10, 0) and (10, 19).
If you just want the matrix cell that is at a certain polar coordinate from the center, then that's simply
(round(r * cos(theta) + centerx), round(r * sin(theta) + centery)
After rounding, coordinates outside the matrix should be ignored.