Sign in to follow this  

trying to rotate a 2d square

Recommended Posts

Hello, I am trying to rotate a 2d square in java using four lines, and I am having some trouble, I am using this tut on matricies and it says to use the rotation matrix [ cos a sin a ] [-sin a cos a ] The square is rotating, but it is also flying in a large circle, compile this Java code and you will see what I mean
import javax.swing.*; import java.awt.*; import java.util.*; class rotation extends JPanel { double[] p1 = {225,225}; double[] p2 = {250,250}; double[] p3 = {225,250}; double[] p4 = {250,225}; public rotation() { setPreferredSize(new Dimension(640,480)); setVisible(true); } public void paintComponent(Graphics page) { page.setColor(; page.fillRect(0,0,getSize().width,getSize().height); page.setColor(; page.drawLine((int)p1[0],(int)p1[1],(int)p3[0],(int)p3[1]); page.setColor(Color.yellow); page.drawLine((int)p3[0],(int)p3[1],(int)p2[0],(int)p2[1]); page.setColor(; page.drawLine((int)p2[0],(int)p2[1],(int)p4[0],(int)p4[1]); page.setColor(; page.drawLine((int)p4[0],(int)p4[1],(int)p1[0],(int)p1[1]); page.drawString("p1[0]="+(int)p1[0]+" p1[1]="+(int)p1[1],30,100); page.drawString("p2[0]="+(int)p2[0]+" p2[1]="+(int)p1[1],30,150); page.drawString("p3[0]="+(int)p3[0]+" p3[1]="+(int)p1[1],30,200); page.drawString("p4[0]="+(int)p4[0]+" p4[1]="+(int)p1[1],30,250); } public double[] rotate(double x, double y,double angle) { System.out.println("rotate" ); double[] temp=new double[2]; angle=(angle/180)*(Math.PI); temp[0]=Math.cos(angle)*x; temp[0]+=(Math.sin(angle)*y); temp[1]=((-Math.sin(angle))*x); temp[1]+=Math.cos(angle)*y; System.out.println("oldx= "+x+" newx: "+temp[0] ); System.out.println("old y= "+y+" new y: "+temp[1]); return temp; } public void loop() { while (true) { p1=rotate(p1[0],p1[1],10.0); p3=rotate(p3[0],p3[1],10.0); p2=rotate(p2[0],p2[1],10.0); p4=rotate(p4[0],p4[1],10.0); repaint(); try { Thread.sleep(100); } catch(Exception e){} } } }
you will also need a frame, I use this one
import java.awt.*; import javax.swing.*; class Frame { public static void main (String[] args) { JFrame frame = new JFrame ("Rocket"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.setResizable( false ); rotation rot = new rotation(); frame.getContentPane().add(rot); frame.pack(); frame.setVisible(true); rot.loop(); } }

Share this post

Link to post
Share on other sites
That code rotates around the global origin. To rotate about a local origin, you should use a conjugate translation. I assume the syntax would go something like:

/*  Rotate by θ about (x0, y0)  */

// First translate (x0, y0) to the origin
p[0] = p[0] - x0;
p[1] = p[1] - y0;

// Rotate about the origin
p = rotate(p[0], p[1], θ);

// Then translate the origin back to (x0, y0)
p[0] = p[0] + x0;
p[1] = p[1] + y0;

Presumably you'd want to choose (x0, y0) to be the centre of the square.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this