Java 围绕其中心旋转矩形多边形。(爪哇)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20936429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:25:47  来源:igfitidea点击:

Rotating a rectangle-shaped Polygon around it's center. (Java)

javaswingmatrixrotationpolygon

提问by user3150201

I have this code to draw a rectangle (Polygon object), and then draw another rectangle which is the original one, rotated 90 degrees, using the rotation matrix.

我有这个代码来绘制一个矩形(Polygon 对象),然后使用旋转矩阵绘制另一个旋转 90 度的原始矩形。

public class DrawingPanel extends JPanel{

    public void paintComponent(Graphics g){

        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        Point p1,p2,p3,p4;
        p1 = new Point(50,50);
        p2 = new Point(200,50);
        p3 = new Point(200,100);
        p4 = new Point(50,100);

        int[] x = {(int) p1.getX(), (int) p2.getX(), (int)p3.getX(), (int) p4.getX()};
        int[] y = {(int) p1.getY(), (int) p2.getY(), (int)p3.getY(), (int) p4.getY()};

        Polygon poly = new Polygon(x, y, x.length);
        g2d.draw(poly);

        p1.setLocation(p1.getX() * Math.cos(Math.toRadians(90)) - p1.getY() * Math.sin(Math.toRadians(90)),
                p1.getX() * Math.sin(Math.toRadians(90)) + p1.getY() * Math.cos(Math.toRadians(90)));
        p2.setLocation(p2.getX() * Math.cos(Math.toRadians(90)) - p2.getY() * Math.sin(Math.toRadians(90)),
                p2.getX() * Math.sin(Math.toRadians(90)) + p2.getY() * Math.cos(Math.toRadians(90)));
        p3.setLocation(p3.getX() * Math.cos(Math.toRadians(90)) - p3.getY() * Math.sin(Math.toRadians(90)),
                p3.getX() * Math.sin(Math.toRadians(90)) + p3.getY() * Math.cos(Math.toRadians(90)));
        p4.setLocation(p4.getX() * Math.cos(Math.toRadians(90)) - p4.getY() * Math.sin(Math.toRadians(90)),
                p4.getX() * Math.sin(Math.toRadians(90)) + p4.getY() * Math.cos(Math.toRadians(90)));

        int[] x2 = {(int) p1.getX(), (int) p2.getX(), (int)p3.getX(), (int) p4.getX()};
        int[] y2 = {(int) p1.getY(), (int) p2.getY(), (int)p3.getY(), (int) p4.getY()};

        Polygon poly2 = new Polygon(x2, y2, x2.length);
        g2d.draw(poly2);

    }

}

Currently, the second rectangle doesn't show up. When I asked about this in a different question, someone answered that that's because it draws outside the screen.

目前,第二个矩形不显示。当我在另一个问题中询问这个问题时,有人回答说那是因为它在屏幕之外绘制。

I asked how to rotate the rectangle around it's center, so the new drawing will appear in the screen, and he answered but I didn't really understand how to implement what he said in the code (tried different things that didn't work).

我问如何围绕它的中心旋转矩形,这样新的绘图就会出现在屏幕上,他回答了,但我真的不明白如何实现他在代码中所说的(尝试了不同的东西,但不起作用) .

Could you show me exactly in the code, how to rotate the rectangle around it's center?

你能在代码中准确地告诉我,如何围绕它的中心旋转矩形吗?

(Of course this isn't actual rotating of an object. It's making a rotated-duplicate of an object).

(当然,这不是对象的实际旋转。它是对对象进行旋转复制)。

Help would be appreciated. Thanks

帮助将不胜感激。谢谢

采纳答案by Christian

Since you are trying to rotate around the rectangle's center, first you need to translate the rectangle to the origin, then apply the rotation and finally translate back. See the following code (adapted to your case) from this answerin another post.

由于您试图围绕矩形的中心旋转,首先您需要将矩形平移到原点,然后应用旋转,最后平移回来。请参阅另一篇文章中此答案中的以下代码(适合您的情况)。

//TRANSLATE TO ORIGIN
double x1 = p1.getX() - center.x;
double y1 = p1.getY() - center.y;

//APPLY ROTATION
double temp_x1 = x1 * Math.cos(angle) - y1 * Math.sin(angle));
double temp_y1 = x1 * Math.sin(angle) + y1 * Math.cos(angle));

//TRANSLATE BACK
p1.setLocation(temp_x1 + center.x, temp_y1 + center.y);

You have to do this for each point. Also you will need to find the center of the rectangle center.xand center.y.

你必须对每个点都这样做。您还需要找到矩形的中心center.xcenter.y

ExplanationWhen you directly apply the rotation, you are rotating it around the origin (0,0)(you can easily see this when you change the angle rotation in your code). If you want to rotate around the center of the rectangle, you have to follow the steps described above.

说明当您直接应用旋转时,您是在围绕原点旋转它(0,0)(当您在代码中更改角度旋转时,您可以很容易地看到这一点)。如果要绕矩形的中心旋转,则必须按照上述步骤操作。

  • Translate the shape (rectangle) to origin, this can be accomplished by substracting the center components to the point's components.
  • Apply rotation to the translated pointsaround the origin (0,0).
  • Translate back each point to the original position, by adding the center components (the ones you substracted in the first step).
  • 将形状(矩形)转换为 origin,这可以通过将中心分量减去点的分量来完成。
  • 将旋转应用于围绕原点的平移点(0,0)
  • 通过添加中心组件(您在第一步中减去的那些)将每个点平移回原始位置

Edit 1:

编辑1:

Let's say we have a Rectangle (Square) which vertex are:

假设我们有一个矩形(方形),其顶点是:

p1: (2, 2)
p2: (3, 2)
p3: (3, 3)
p4: (2, 3)

For point p2:

对于点p2

In first step:

第一步:

center: (2.5, 2.5)
x1 = 3 - 2.5 = 0.5
y1 = 2 - 2.5 = -0.5

Doing this for each point:

为每个点执行此操作:

new_p1: (-0.5, -0.5)
new_p2: (0.5, -0.5)
new_p3: (0.5, 0.5)
new_p4: (-0.5, 0.5)

Then you apply the rotation...

然后你应用旋转......

Edit 2:I hope it gets clearer with this image, sorry if I'm bad with Paint.

编辑 2:我希望这张图片能看得更清楚,如果我对 Paint 不满意,我很抱歉。

Explanation

解释

回答by Tim B

Rotations always happen around 0,0.

旋转总是发生在 0,0 附近。

In order to rotate around the center of the object you need to move the points so the center of the object is at 0,0; then rotate them; then afterwards move them back again:

为了围绕对象的中心旋转,您需要移动点,使对象的中心位于 0,0;然后旋转它们;然后再将它们移回:

So if cxand cyare your center:

因此,如果cx并且cy是您的中心:

    p1.setLocation((p1.getX()-cx) * Math.cos(Math.toRadians(90)) - (p1.getY()-cy) * Math.sin(Math.toRadians(90))+cx,
            (p1.getX()-cx) * Math.sin(Math.toRadians(90)) + (p1.getY()-cy) * Math.cos(Math.toRadians(90))+cy);

And do the same for the other points too.

对其他点也做同样的事情。