Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:按指定的度值围绕另一个点旋转_Java_Geometry_Rotation_2d_Point - Fatal编程技术网

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

Java:按指定的度值围绕另一个点旋转,java,geometry,rotation,2d,point,Java,Geometry,Rotation,2d,Point,我试图在java中用一个指定的度值围绕另一个2D点旋转一个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

我试图在java中用一个指定的度值围绕另一个2D点旋转一个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));
}
预期为(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

方法本身有问题吗?我将函数从移植到Java

编辑:


做了一些性能测试。我本不这么认为,但矢量解赢了,所以我将使用这个。

在对Y值执行计算之前,您正在对
中心的X值进行变异。改为使用临时点


此外,该函数接受三个参数。为什么您只需要两个呢?

如果您可以访问
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];

在Lua代码中,第一个参数是应移动的点。那里的函数似乎没有绑定到类,就像在PHP中一样。在我的java代码中,第一个参数位于方法调用point.rotateAround(center,angle)之前。添加临时坐标后,该方法仍然返回意外值=/那你能发布你的新代码吗?因为@Peter已经正确地解释了为什么你的代码没有做正确的事情。听起来你好像没有正确理解他让你做的事情。@Peter@David
公共类点{double x,y;公共点(double x,double y){this.x=x;this.y=y;}public void rotateAround(Point center,double angle){double tempx=center.x+(Math.cos(Math.toRadians(angle))*(x-center.x)-Math.sin(Math.toRadians(angle))*(y-center.y));double tempy=center.y+(Math.sin(Math.toRadians(angle))*(x-center.x)+Math.cos(Math.toRadians(angle))*(y-center.y));x=tempx;y=tempy;}公共字符串到字符串(){return Point{x=“+String.valueOf(x)+”,y=“+String.valueOf(y)+”;}
好的,这不是6.123…,这是6.123 x 10^-16。这是一个很小的数字,这只是处理浮点数时通常会出现的不准确度。也就是说,它实际上与零没有区别。y之所以显示为10,而不是-10,是因为公式是逆时针旋转的;所以从(10,0)开始旋转90度给你(0,10)。速度够快吗?我需要在游戏中使用超快的方法。本机例程似乎没有性能优化。AWT是为图形而构建的,这通常意味着游戏——因此,我不担心。