Java Vector2d 类中的旋转
我已经为此工作了一个小时,但无法理解。
我有一个 Vector2d 类:
public class Vector2d
{
public double x = 0.0;
public double y = 0.0;
....
}
这个向量类有一个rotate() 方法,这给我带来了麻烦。
第一个片段似乎使 x 和 y 值越来越小。第二个效果很好!我在这里缺少一些简单的东西吗?
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've been working on this for one hour, just can't get it.
I have a Vector2d class:
public class Vector2d
{
public double x = 0.0;
public double y = 0.0;
....
}
This vector class has a rotate() method which is causing me trouble.
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));
}
This works:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一行设置
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 ofthis.x
. The second version works fine because you don't alterthis.x
.