so how can I to get x,y coordinates of a point from a specific distance?
so
public static Location2D DistanceToXY(Location2D current, Directions dir, int steps) {
ushort x = current.X;
ushort y = current.Y;
for (int i = 0; i < steps; i++) {
switch (dir) {
case Directions.North:
y--;
break;
case Directions.South:
y++;
break;
case Directions.East:
x++;
break;
case Directions.West:
x--;
break;
case Directions.NorthWest:
x--;
y--;
break;
case Directions.SouthWest:
x--;
y++;
break;
case Directions.NorthEast:
x++;
y--;
break;
case Directions.SouthEast:
x++;
x++;
break;
}
}
return new Location2D(x, y);
}
is what am doing here is right?
I’m assuming you’re looking for a general solution. As others have pointed out you need a direction as a missing input. You will basically use the direction and magnitude (10 in this case) and convert these polar coordinates to Cartesian coordinates. Then you add resulting the coordinates together X + Xoffset = Xnew, and Y + Yoffset = Ynew.
The details of the conversion are here:
http://www.mathsisfun.com/polar-cartesian-coordinates.html
Edit: After you posted your code, the answer is no. The cases for NorthWest, SouthWest, NorthEast, SouthEast are not correct. In these cases you are moving 1.41 (aprox) pixels. You shouldn’t try to incrementally solve the puzzle. Use the polar coordinate math and sum the total offset and then round to the nearest integer.
Heres a simplified pseudocode mod to your solution: