问题描述
我试图旋转2D点中的java围绕另一个具有指定程度的值,在这种情况下,简单地围绕点(0,0)在90度。
I am trying to rotate a 2D Point in java around another with a specified degree value, in this case simply around Point (0, 0) at 90 degrees.
方法:
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
Expected for (3, 0): X = 0, Y = -3
返回对(3,0):X = 1.8369701987210297E-16,Y = 1.8369701987210297E-16
Returned for (3, 0): X = 1.8369701987210297E-16, Y = 1.8369701987210297E-16
预期为(0,-10):X = -10,Y = 0
Expected for (0, -10): X = -10, Y = 0
返回为(0,-10):X = 10.0,Y = 10.0
Returned for (0, -10): X = 10.0, Y = 10.0
时的一些错误的方法本身?我移植从功能(旋转点在2D Lua中 - GPWiki)到Java。
Is something wrong with the method itself? I ported the function from (Rotating A Point In 2D In Lua - GPWiki) to Java.
编辑:
做了一些性能测试。我不这样想,但向量解赢了,所以我就用这一个。
Did some performance tests. I wouldn't have thought so, but the vector solution won, so I'll use this one.
推荐答案
如果你有机会获得 java.awt中的
,这只是
If you have access to java.awt
, this is just
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的:绕另一点由指定的角度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!