数学帮助 - 无法旋转某些东西(了解 Java 会更好)

发布于 2024-07-13 23:25:26 字数 490 浏览 6 评论 0原文

好吧,我正在尝试根据角度 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘过的浮云 2024-07-20 23:25:26

rotate 方法中操作的 xy 值不会在调用它的方法中看到,因为 Java 按值传递方法参数

因此,在 rotate 方法中更改的 xy 值是本地副本,因此一旦超出范围(即从rotate方法返回到其调用方法),xy的值将消失。

所以目前发生的情况是:

x = 10;
y = 10;
o1 = new obj();
o1.a = 100;
rotate(x, y, obj);
System.out.println(x);  // Still prints 10
System.out.println(y);  // Still prints 10

从 Java 中的方法获取多个值的唯一方法是传递一个对象,并操作传入的对象。(实际上,传入的是该对象的引用的副本当进行方法调用时。)

例如,重新定义 rotate 以返回 Point

public Point rotate(int x, int y, double angle)
{
    // Do rotation.
    return new Point(newX, newY);
}

public void callingMethod()
{
    int x = 10;
    int y = 10;
    p = rotate(x, y, 45);
    System.out.println(x);  // Should print something other than 10.
    System.out.println(y);  // Should print something other than 10.
}

也就是说,如 Pierre 建议,使用 AffineTransform 在我看来会容易得多。

例如,创建一个 矩形 对象并使用 AffineTransform 旋转它可以通过以下方式执行:

Rectangle rect = new Rectangle(0, 0, 10, 10);

AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(45));

Shape rotatedRect = at.createTransformedShape(rect);

AffineTransform 可以应用于实现 Shape 接口。 可以在 Shape 接口的链接 Java API 规范中找到实现 Shape 的类列表。

有关如何使用 AffineTransform 和 Java 2D 的更多信息:

The values of x and y that are being manipulated in the rotate method will not be seen in the method that is calling it because Java passes method arguments by value.

Therefore, the x and y values that are being changed in the rotate method is a local copy, so once it goes out of scope (i.e. returning from the rotate method to its calling method), the values of x and y will disappear.

So currently, what is happening is:

x = 10;
y = 10;
o1 = new obj();
o1.a = 100;
rotate(x, y, obj);
System.out.println(x);  // Still prints 10
System.out.println(y);  // Still prints 10

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 a Point:

public Point rotate(int x, int y, double angle)
{
    // Do rotation.
    return new Point(newX, newY);
}

public void callingMethod()
{
    int x = 10;
    int y = 10;
    p = rotate(x, y, 45);
    System.out.println(x);  // Should print something other than 10.
    System.out.println(y);  // Should print something other than 10.
}

That said, as Pierre suggests, using the AffineTransform would be much easier in my opinion.

For example, creating a Rectangle object and rotating it using AffineTransform can be performed by the following:

Rectangle rect = new Rectangle(0, 0, 10, 10);

AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(45));

Shape rotatedRect = at.createTransformedShape(rect);

AffineTransform can be applied to classes which implement the Shape interface. A list of classes implementing Shape can be found in the linked Java API specifications for the Shape interface.

For more information on how to use AffineTransform and Java 2D:

苦行僧 2024-07-20 23:25:26

仅供参考:旋转形状和点已在 java.awt.geom.AffineTransform

FYI: Rotating shapes and points has been implemented in java.awt.geom.AffineTransform

戏舞 2024-07-20 23:25:26

您正在执行2D 旋转变换

它应该看起来像这样:

xnew = xold*cos(t) - yold*sin(t)
ynew = xold*sin(t) + yold*cos(t)

当然,旋转角度 t 必须以弧度为单位。 它在 x 轴处为零,并沿逆时针方向增加。

旧点和新点都需要相对于您旋转的原点来表示。

You're performing a 2D rotational transformation.

It ought to look something like this:

xnew = xold*cos(t) - yold*sin(t)
ynew = xold*sin(t) + yold*cos(t)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文