1
votes

I wrote a program that supports 3 actions : rotate ,translate and scaling .

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 . 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 :

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 .

Any idea for the problem would be greatly appreciated !

Regards

1

1 Answers

1
votes

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:

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.

You should probably combine these 3 operations into one matrix, then apply that to currentLine, so you'd have a method like

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