gpt4 book ai didi

java - 我的大脑卡住了,帮我使这个方法有效 :>

转载 作者:行者123 更新时间:2023-11-29 09:57:28 26 4
gpt4 key购买 nike

我有以下代码,我可以看出值之间存在联系。但是,我无法集中精力并弄清楚如何使用一些 add subtract modelo haxx 删除 switch 语句。真的很想得到一些帮助,让这个方法更顺利。

标题变量是 0、1、2 或 3。

public static int getRotation(Point currentPoint, Point nextPoint, int heading) {
int rotation = -1;
if(currentPoint.getX() < nextPoint.getX()) /* DRIVE WEST */ {
switch(heading) {
case 0: rotation = 1; break;
case 1: rotation = 0; break;
case 2: rotation = 3; break;
case 3: rotation = 2; break;
}
} else if(currentPoint.getX() > nextPoint.getX()) /* DRIVE EAST */ {
switch(heading) {
case 0: rotation = 3; break;
case 1: rotation = 2; break;
case 2: rotation = 1; break;
case 3: rotation = 0; break;
}
} else if(currentPoint.getY() < nextPoint.getY()) /* DRIVE NORTH */ {
switch(heading) {
case 0: rotation = 0; break;
case 1: rotation = 3; break;
case 2: rotation = 2; break;
case 3: rotation = 1; break;
}
} else if(currentPoint.getY() > nextPoint.getY()) /* DRIVE SOUTH */ {
switch(heading) {
case 0: rotation = 2; break;
case 1: rotation = 1; break;
case 2: rotation = 0; break;
case 3: rotation = 3; break;
}
}

return rotation;
}

编辑:我确实忘记提到 nextPoint 与 currentPoint 相比只能是 +-x 或 y。如果 currentPoint 是 (0,0),newPoint 必须是 (-1,0)、(1,0)、(0,-1) 或 (0,1)。

最佳答案

你可以试试。

int eastWest = Double.compare(currentPoint.getX(), nextPoint.getX()) 
if (eastWest == 0) {
int northSouth = Double.compare(currentPoint.getX() , nextPoint.getX())
/* DRIVE NORTH (-1), SOUTH (+1) */
if (northSouth == 0)
rotation = -1;
else
rotation = (5 + northSouth - heading) % 4;
} else {
/* DRIVE WEST (-1), EAST (+1) */
rotation = heading ^ (2 + eastWest);
}

注意:我希望看到这些函数的一些对称性,因为每个方向似乎都有不同的组合并且听起来不对。

关于java - 我的大脑卡住了,帮我使这个方法有效 :>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5540336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com