Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Geometry 如何从特定距离获得x,y坐标_Geometry - Fatal编程技术网

Geometry 如何从特定距离获得x,y坐标

Geometry 如何从特定距离获得x,y坐标,geometry,Geometry,那么,我怎样才能从一个特定的距离得到一个点的x,y坐标呢 所以 public static Location2D distance toxy(Location2D current,Directions,int steps){ ushort x=当前的.x; ushort y=当前的.y; 对于(int i=0;i

那么,我怎样才能从一个特定的距离得到一个点的x,y坐标呢

所以

public static Location2D distance toxy(Location2D current,Directions,int steps){
ushort x=当前的.x;
ushort y=当前的.y;
对于(int i=0;i

我在这里所做的是正确的吗?

我假设您正在寻找一个通用的解决方案。正如其他人指出的,您需要一个方向作为缺少的输入。您将基本上使用方向和幅值(本例中为10),并将这些极坐标转换为笛卡尔坐标。然后将得到的坐标加在一起X+Xoffset=Xnew和Y+Yoffset=Ynew

有关转换的详情如下:

编辑:发布代码后,答案是否定的。西北、西南、东北、东南的情况不正确。在这些情况下,移动的是1.41(大约1像素)。你不应该试图逐步解决这个难题。使用极坐标数学,将总偏移求和,然后四舍五入到最接近的整数

以下是您的解决方案的简化伪代码模型:

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

取决于你走的方向这不取决于你走的方向吗?你的意思是什么地方可以找到?如果是这样,你可以递归地做。我相信这篇文章会对你有所帮助@德米特里·萨维是的,有很多!
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);
    }