2012-02-18 92 views
0

那么我如何才能从特定距离获得某个点的x,y坐标?如何从特定距离获得x,y坐标

所以

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); 
     } 

就是我在这里做什么是正确的?

+6

取决于在你走什么方向 – 2012-02-18 06:11:12

+0

难道这取决于你在行进的方向? – Oleksi 2012-02-18 06:11:31

+0

你的意思是哪些地点可以使用?如果是这样,你可以递归地做。 – Ovilia 2012-02-18 06:19:20

回答

0

我假设你正在寻找一个通用的解决方案。正如其他人指出的那样,你需要一个方向作为缺失的输入。您将基本上使用方向和大小(在这种情况下为10),并将这些极坐标转换为笛卡尔坐标。然后将所得的坐标加在一起X + Xoffset = Xnew,Y + Yoffset = Ynew。

转换的细节在这里: http://www.mathsisfun.com/polar-cartesian-coordinates.html

编辑:在您贴出你的代码,答案是否定的。 NorthWest,SouthWest,NorthEast,SouthEast的情况并不正确。在这些情况下,您将移动1.41(aprox)像素。你不应该试图逐步解决这个难题。使用极坐标数学计算总和偏移量,然后四舍五入到最接近的整数。

继承人简化伪国防部为您的解决方案:

public static Location2D DistanceToXY(Location2D current, Directions dir, int steps) { 
     ushort x = current.X; 
     ushort y = current.Y; 

     switch (dir) { 
      case Directions.North: 
       y=y+steps; 
       break; 
      case Directions.South: 
       y=y-steps; 
       break; 
      case Directions.East: 
       x=x+steps; 
       break; 
      case Directions.West: 
       x=x-steps; 
       break; 
      case Directions.NorthWest: 
       float sqrt2 = 2^0.5 
       x=x+int((sqrt2 * steps) + 0.5); 
       y=y-int((sqrt2 * steps) + 0.5); 
       break; 
      case Directions.SouthWest: 
       x=x-int((sqrt2 * steps) + 0.5); 
       y=y-int((sqrt2 * steps) + 0.5); 
       break; 
      case Directions.NorthEast: 
       x=x+int((sqrt2 * steps) + 0.5); 
       y=y+int((sqrt2 * steps) + 0.5); 
       break; 
      case Directions.SouthEast: 
       x=x-int((sqrt2 * steps) + 0.5); 
       y=y+int((sqrt2 * steps) + 0.5); 
       break; 
     } 
     return new Location2D(x, y); 
    } 
+0

thnx求助^ _ ^ – Abanoub 2012-02-18 07:02:41