获取旋转矩形的四个角

6

我有一个矩形,它围绕中心旋转,我还有另一个矩形,我想连接到旋转矩形的右上角。问题是我不知道如何获取该角落,以便第二个矩形始终粘在那个角上。

这是我的示例代码。现在第二个矩形将始终处于相同的位置,这并不是我要的结果。

package Test;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

class Test{

    public static void main(String[] args){
        new Test();
    }

    public Test(){
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new Graphic());
                frame.setSize(1000,700);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

class Graphic extends JPanel{
    private int x, y, windowW, windowH;
    private double angle;
    private Rectangle rect1, rect2;
    private Path2D path;
    private Timer timer;
    private AffineTransform rotation;

    public Graphic(){
        windowW = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        windowH = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        path = new Path2D.Double();
        rotation = new AffineTransform();
        angle = 0;
        x = windowW / 2;
        y = windowH / 2;
        timer = new Timer(100, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                angle += .1;
                if(angle > 360) angle -= 360;
                repaint();
            }
        });
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        rotation.setToTranslation(500, 200);
        rotation.rotate(angle, 32, 32);
        rect1 = new Rectangle(0, 0, 64, 64);
        path = new Path2D.Double(rect1, rotation);
        rect2 = new Rectangle(path.getBounds().x, path.getBounds().y, 10, 50);
        g2d.fill(path);
        g2d.fill(rect2);
    }
}

你对 angle 设置的限制似乎表明你想用角度来表示它;然而,rotate 函数的第一个参数是弧度。条件 if(angle > 360) angle -= 360 实际上会在达到时导致奇怪的跳跃,矩形旋转的速度比你想要的快得多。 - Arend
2个回答

4

数学解决方案:)

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    rotation.setToTranslation(500, 200);
    rotation.rotate(angle, 32, 32);
    rect1 = new Rectangle(0, 0, 64, 64);
    path = new Path2D.Double(rect1, rotation);
    double r = 32.0 * Math.sqrt(2);
    // (532, 232) - coordinates of rectangle center        |
    // you can change position of second rectangle by this V substraction (all you need to know is that the full circle corresponds to 2Pi)
    int x2 = (int) Math.round(532 + r * Math.cos(angle - Math.PI / 4));
    int y2 = (int) Math.round(232 + r * Math.sin(angle - Math.PI / 4));
    rect2 = new Rectangle(x2, y2, 10, 50);
    g2d.fill(path);
    g2d.fill(rect2);
}

当然,一些常量应该是类字段,而不是方法变量。

2

我无法测试这段代码以确保其正确性,但我相信这就是你想要的正确工作代码。

int hw = -width / 2;
int hh = -height / 2;
int cos = Math.cos( theta );
int sin = Math.sin( theta );
int x = hw * cos - hh * sin;
int y = hw * sin + hh * cos;

根据正方形的旋转角度theta,这将为您获得左上角。要获取其他角,只需更改hw和hh值:

//top right corner
hw = width / 2
hh = -height / 2

//bottom right corner
hw = width / 2
hh = height / 2

//bottom left corer
hw = -width / 2
hh = height / 2

我希望你能够帮助解决这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接