• Advertisement
Sign in to follow this  

trying to rotate a 2d square

This topic is 3943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 http://student.kuleuven.be/~m0216922/CG/matrices.html 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
Quote:
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(Color.black); page.fillRect(0,0,getSize().width,getSize().height); page.setColor(Color.red); 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(Color.green); page.drawLine((int)p2[0],(int)p2[1],(int)p4[0],(int)p4[1]); page.setColor(Color.blue); 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
Quote:
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
Advertisement
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.

Admiral

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement