gpt4 book ai didi

Java:按指定的度值围绕另一个旋转点

转载 作者:搜寻专家 更新时间:2023-11-01 01:18:07 24 4
gpt4 key购买 nike

我试图在 java 中围绕另一个具有指定度数的二维点旋转,在本例中只是围绕点 (0, 0) 旋转 90 度。

方法:

public void rotateAround(Point center, double angle) {
x = center.x + (Math.cos(Math.toRadians(angle)) * (x - center.x) - Math.sin(Math.toRadians(angle)) * (y - center.y));
y = center.y + (Math.sin(Math.toRadians(angle)) * (x - center.x) + Math.cos(Math.toRadians(angle)) * (y - center.y));
}

预期 (3, 0):X = 0,Y = -3

为 (3, 0) 返回:X = 1.8369701987210297E-16,Y = 1.8369701987210297E-16

预期 (0, -10):X = -10,Y = 0

返回 (0, -10):X = 10.0,Y = 10.0

是不是方法本身有问题?我从 (Rotating A Point In 2D In Lua - GPWiki) 移植了函数到 Java。

编辑:

做了一些性能测试。我不会这么想,但 vector 解决方案赢了,所以我将使用这个解决方案。

最佳答案

如果您有权访问 java.awt,这就是

double[] pt = {x, y};
AffineTransform.getRotateInstance(Math.toRadians(angle), center.x, center.y)
.transform(pt, 0, pt, 0, 1); // specifying to use this double[] to hold coords
double newX = pt[0];
double newY = pt[1];

关于Java:按指定的度值围绕另一个旋转点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9985473/

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