数学帮助 - 无法旋转某些东西(了解 Java 会更好)
好吧,我正在尝试根据角度 0.x 的原始位置旋转 Java 多边形,并且 y 最终在我使用它们的末尾转换为 int,所以我可以理解没有看到任何变化,但是当角度差异很大,例如 0 到 180,我想我应该看到某些东西。
我已经在这个问题上呆了一段时间了,想不出它是什么。 这是方法。 (抱歉,如果代码标签弄乱了,我的火狐浏览器会把它们弄乱。)
public void rotate(double x, double y, obj o1)
{
double dist = Math.sqrt(Math.pow(x - (o1.x + (o1.w/2)), 2) + Math.pow(y - (o1.y + (o1.h/2)),2));
x += (Math.sin(Math.toRadians(o1.a)) * dist);
y -= (Math.cos(Math.toRadians(o1.a)) * dist);
}
Okay I'm trying to rotate a Java Polygon based on it's original position of angle 0. x, and y end up being converted to an int at the end of me using them, so I could understand not seeing some change, but when the difference in angles is big like 0 to 180 I think I should see something.
I've been at this for a little while and can't think of what it is. Here's the method. (Sorry if it messes up in the code tags, my firefox messes them up.)
public void rotate(double x, double y, obj o1)
{
double dist = Math.sqrt(Math.pow(x - (o1.x + (o1.w/2)), 2) + Math.pow(y - (o1.y + (o1.h/2)),2));
x += (Math.sin(Math.toRadians(o1.a)) * dist);
y -= (Math.cos(Math.toRadians(o1.a)) * dist);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
rotate
方法中操作的x
和y
值不会在调用它的方法中看到,因为 Java 按值传递方法参数。因此,在
rotate
方法中更改的x
和y
值是本地副本,因此一旦超出范围(即从rotate
方法返回到其调用方法),x
和y
的值将消失。所以目前发生的情况是:
从 Java 中的方法获取多个值的唯一方法是传递一个对象,并操作传入的对象。(实际上,传入的是该对象的引用的副本当进行方法调用时。)
例如,重新定义
rotate
以返回Point
:也就是说,如 Pierre 建议,使用 AffineTransform 在我看来会容易得多。
例如,创建一个
矩形
对象并使用AffineTransform
旋转它可以通过以下方式执行:AffineTransform
可以应用于实现Shape
接口。 可以在Shape
接口的链接 Java API 规范中找到实现Shape
的类列表。有关如何使用
AffineTransform
和 Java 2D 的更多信息:The values of
x
andy
that are being manipulated in therotate
method will not be seen in the method that is calling it because Java passes method arguments by value.Therefore, the
x
andy
values that are being changed in therotate
method is a local copy, so once it goes out of scope (i.e. returning from therotate
method to its calling method), the values ofx
andy
will disappear.So currently, what is happening is:
The only way to get multiple values back from a method in Java is to pass an object, and manipulate the object that is passed in. (Actually, a copy of the reference to the object is passed in when an method call is made.)
For example, redefining
rotate
to return aPoint
:That said, as Pierre suggests, using the AffineTransform would be much easier in my opinion.
For example, creating a
Rectangle
object and rotating it usingAffineTransform
can be performed by the following:AffineTransform
can be applied to classes which implement theShape
interface. A list of classes implementingShape
can be found in the linked Java API specifications for theShape
interface.For more information on how to use
AffineTransform
and Java 2D:仅供参考:旋转形状和点已在 java.awt.geom.AffineTransform
FYI: Rotating shapes and points has been implemented in java.awt.geom.AffineTransform
您正在执行2D 旋转变换。
它应该看起来像这样:
当然,旋转角度 t 必须以弧度为单位。 它在 x 轴处为零,并沿逆时针方向增加。
旧点和新点都需要相对于您旋转的原点来表示。
You're performing a 2D rotational transformation.
It ought to look something like this:
The rotation angle t must be in radians, of course. It's zero at the x-axis, and increased in the anti-clockwise direction.
Both the old and new points need to be expressed relative to the origin you're rotating about.