Java:按指定的度数值旋转另一个

Aic*_*ich 4 java geometry 2d point rotation

我试图在java中围绕另一个具有指定度数值的2D点旋转,在这种情况下简单地围绕点(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));
}
Run Code Online (Sandbox Code Playgroud)

预期为(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

这个方法本身有问题吗?我将函数移植到(在Lua-GPWiki中将2D点转换为Java).

编辑:

做了一些性能测试.我不会这么想,但矢量解决方案赢了,所以我会用这个.

Lou*_*man 10

如果您有权访问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];
Run Code Online (Sandbox Code Playgroud)