我试图用Java编写代码以使用两个角度移动船只:一个在xy平面上,另一个在xz平面上。我想要的是将执行以下操作的代码:
以下是我所拥有的:
xyAngle = Math.toRadians(xyCourse);
xzAngle = Math.toRadians(xzCourse);
double deltaX = Math.cos(xyAngle) * Math.sin(xzAngle);
double deltaY = Math.sin(xyAngle) * Math.cos(xzAngle);
double deltaZ = Math.cos(xzAngle);
System.out.println(String.format("xyAngle = %.4f, xzAngle = %.4f", xyAngle, xzAngle));
System.out.println("xyAngle info: sin = " + Math.sin(xyAngle) + ", cos = " + Math.cos(xyAngle));
System.out.println("xzAngle info: sin = " + Math.sin(xzAngle) + ", cos = " + Math.cos(xzAngle));
System.out.println(String.format("deltaX = %.2f, deltaY = %.2f, deltaZ = %.2f", deltaX, deltaY, deltaZ));
for (int i = 0; i <= distance; i++) {
System.out.println("Coordinates: " + String.format("(%.2f, %.2f, %.2f)", xCoordinate, yCoordinate, zCoordinate));
xCoordinate += deltaX;
yCoordinate += deltaY;
zCoordinate += deltaZ;
}
我似乎无法让它发挥作用。有人有任何提示吗?
答案 0 :(得分:1)
使用Math.cos
和Math.sin
,我认为你想要的很容易做到?
<强>代码:强>
public class QuickTester {
public static void main(String[] args) {
double [] xyAngles = {0.0, 90.0, 0.0, 45.0};
double [] xzAngles = {0.0, 0.0, 90.0, 45.0};
for(int i = 0; i < xyAngles.length; i++)
{
double xyAngleRad = Math.toRadians(xyAngles[i]);
double xzAngleRad = Math.toRadians(xzAngles[i]);
// To fulfill the 3 conditions mentioned
double deltaX = Math.cos(xyAngleRad) * Math.cos(xzAngleRad);
double deltaY = Math.sin(xyAngleRad) * Math.cos(xzAngleRad);
double deltaZ = Math.cos(xyAngleRad) * Math.sin(xzAngleRad);
System.out.printf("XY Angle: %4.2f, XZ Angle: %4.2f%n",
xyAngles[i], xzAngles[i]);
System.out.printf("dX: %4.2f, dY: %4.2f, dZ: %4.2f%n%n",
deltaX, deltaY, deltaZ);
}
}
}
使用您提到的值进行了一些测试,以及XY和XZ角度均为45度的其他情况。 的输出:强>
XY Angle: 0.00, XZ Angle: 0.00
dX: 1.00, dY: 0.00, dZ: 0.00
XY Angle: 90.00, XZ Angle: 0.00
dX: 0.00, dY: 1.00, dZ: 0.00
XY Angle: 0.00, XZ Angle: 90.00
dX: 0.00, dY: 0.00, dZ: 1.00
XY Angle: 45.00, XZ Angle: 45.00
dX: 0.50, dY: 0.50, dZ: 0.50
如输出所示,