简体   繁体   English

如何围绕自身旋转2D对象?

[英]How to rotate a 2D object around itself?

I wrote a program that supports 3 actions : rotate ,translate and scaling . 我编写了一个程序,该程序支持3个动作:旋转,平移和缩放。

The translation & scaling are working great , but I'm having some problems with the rotation. 平移和缩放效果很好,但是旋转遇到了一些问题。

At the beginning of the code , I parse the origin point from a file , and then draw the initial object in the 2D plane . 在代码的开头,我从文件中解析出起点,然后在2D平面中绘制初始对象。 Then ,instead of rotating around itself , the object is rotating around the origin . 然后,对象绕原点旋转,而不是绕自身旋转。

I check all the matrices and mathematical equations but couldn't find the exact bug , any idea where did I go wrong here ? 我检查了所有矩阵和数学方程式,但找不到确切的错误,不知道我在哪里出错了?

I attached A partial code of the rotation , without the scaling and the transform. 我附加了旋转的部分代码,没有缩放和变换。

Here is a SSCCE of the code : 这是代码的SSCCE:

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;
import java.io.IOException;

import javax.swing.JFrame;

public class SSCCE {

    public static void main (String[] args) throws IOException 
    {
        ClippingView1 CC = new ClippingView1(); 
        CC.start();
    }


}
  • This SSCCE supports only rotation , since this is my main problem . 该SSCCE仅支持旋转,因为这是我的主要问题。

Any idea for the problem would be greatly appreciated ! 任何问题的想法将不胜感激!

Regards 问候

To rotate around another point than origin, you first need to apply translation matrix to move the point you want to rotate around to origin, then rotation matrix around origin, then reverse translation matrix to move everything back to original location. 要绕原点以外的其他点旋转,首先需要应用平移矩阵将要绕原点旋转的点移动到原点,然后绕原点旋转矩阵,然后反转平移矩阵将所有内容移回原始位置。

So, the line where you no do m_transforms.rotate , you should have something like: 因此,在不执行m_transforms.rotate的行中,您应该有类似以下内容的代码:

currentLine = m_transforms.translate(-xPoint, -yPoint,
                  m_transforms.rotate(m_direction,
                       m_transforms.translate(xPoint, yPoint, currentLine);

Where xPoint and yPoint are whatever point you want to rotate around. 其中xPointyPoint是您要旋转的任意点。

You should probably combine these 3 operations into one matrix, then apply that to currentLine , so you'd have a method like 您可能应该将这三个操作组合到一个矩阵中,然后将其应用于currentLine ,因此您将拥有一个类似

Line2D rotate(double xPoint, double yPoint, double angle, Line2D line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM