Java 中 Vector2d 类中的旋转 [英] Rotation in a Vector2d class in Java
问题描述
我已经为此工作了一个小时,只是无法理解.
I've been working on this for one hour, just can't get it.
我有一个 Vector2d 类:
I have a Vector2d class:
public class Vector2d
{
public double x = 0.0;
public double y = 0.0;
....
}
这个向量类有一个 rotate() 方法,这给我带来了麻烦.
This vector class has a rotate() method which is causing me trouble.
第一个片段似乎使 x 和 y 值越来越小.第二个工作得很好!我在这里遗漏了一些简单的东西吗?
The first snippet seems to make the x and y values smaller and smaller. The second one works just fine! Am I missing something simple here?
public void rotate(double n)
{
this.x = (this.x * Math.cos(n)) - (this.y * Math.sin(n));
this.y = (this.x * Math.sin(n)) + (this.y * Math.cos(n));
}
这有效:
public void rotate(double n)
{
double rx = (this.x * Math.cos(n)) - (this.y * Math.sin(n));
double ry = (this.x * Math.sin(n)) + (this.y * Math.cos(n));
x = rx;
y = ry;
}
我就是看不出有什么不同
I just can't spot any difference there
推荐答案
第一行设置 this.x
的值,当你真正想要的是原始值时,第二行使用this.x
的值.第二个版本工作正常,因为您没有更改 this.x
.
The first line sets the value of this.x
which is then used in the second line when what you really want is the original value of this.x
. The second version works fine because you don't alter this.x
.
这篇关于Java 中 Vector2d 类中的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!