Note that there are some explanatory texts on larger screens.

plurals
  1. PORotating a polygon around x and y axis
    primarykey
    data
    text
    <p>I am having problem with the coordinates not being exactly correct after the rotation as I change the values of the polygons, since many of my calculation use doubles and the <a href="http://download.oracle.com/javase/6/docs/api/java/awt/Polygon.html" rel="nofollow"><code>Polygon</code></a> constructor only takes in <code>int[]</code>. Is there a better way to go about this and achieve the same results?</p> <p>Note that rotations of vertices into the “<em>z</em>-direction” (outside of the frame) are achieved by scaling; and that the coordinate axes for each letter has a relative position with respect to the coordinate axes of the frame.</p> <pre><code>import java.awt.*; import java.awt.event.*; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import javax.swing.*; import java.lang.reflect.Array; public class test extends JPanel implements ActionListener{ double[] p1x = {200,200,260,260,220,220,260,260,200}; //int[] p1y = {300,360,360,340,340,320,320,300,300}; double[] p1y = {240,300,300,280,280,260,260,240,240}; double[] p2x = {600,600,620,620,640,640,660,660,600}; double[] p2y = {500,520,520,560,560,520,520,500,500}; double[] p3x = {500,500,560,560,540,540,520,520,500}; double[] p3y = {400,460,460,400,400,440,440,400,400}; int delay = 500; int dx=0; int dy=5; int steps = 120; Polygon t; Timer tim = new Timer(delay,this); public void actionPerformed(ActionEvent event){ for (int i=0; i&lt;Array.getLength(p2x);i++){ //p2x[i] = (int) (p2x[i]*Math.cos(Math.toRadians(1))- p2y[i]*Math.sin(Math.toRadians(1))); //p2y[i] = (int) (p2x[i]*Math.sin(Math.toRadians(1))+ p2y[i]*Math.cos(Math.toRadians(1)));; //p2x[i] -=10; //p3x[i]-= 10; if(steps&gt;100){ p2y[i] -=10; p1y[i] +=10; p3x[i] -=10; if(i==0){ p1y[i] += 6; } if(i==1){ p2y[i] -= 1.5; System.out.println("steps: " + steps +" "+ p2y[i]); } if(i==2){ p2y[i] -= 1.5; p3x[i] -= 6; } if(i==3){ p2y[i] -= 6; p1y[i] += 1.5; p3x[i] -= 6; } if(i==4){ p2y[i] -= 6; p1y[i] += 1.5; p3x[i] -= 4.5; } if(i==5){ p2y[i] -= 1.5; p1y[i] += 4.5; p3x[i] -= 4.5; } if(i==6){ p2y[i] -= 1.5; p1y[i] += 4.5; p3x[i] -= 1.5; } if(i==7){ p1y[i] += 6; p3x[i] -= 1.5; } if(i==8){ p1y[i] += 6; } } else if((steps&lt;=100) &amp;&amp; (steps&gt;80)){ p2y[i] +=10; p1y[i] -=10; p3x[i] +=10; if(i==0){ p1y[i] -= 6; } if(i==1){ p2y[i] += 1.5; System.out.println("steps: " + steps +" "+ p2y[i]); } if(i==2){ p2y[i] += 1.5; p3x[i] += 6; } if(i==3){ p2y[i] += 6; p1y[i] -= 1.5; p3x[i] += 6; } if(i==4){ p3x[i] += 4.5; p2y[i] += 6; p1y[i] -= 1.5; //p3x[i] += 4.5; } if(i==5){ p2y[i] += 1.5; p1y[i] -= 4.5; p3x[i] += 4.5; } if(i==6){ p2y[i] += 1.5; p1y[i] -= 4.5; p3x[i] += 1.5; } if(i==7){ p1y[i] -= 6; p3x[i] += 1.5; } if(i==8){ p1y[i] -= 6; } } else if((steps&lt;=80) &amp;&amp; (steps&gt;60)){ p2y[i] -=10; p1y[i] +=10; p3x[i] -=10; if(i==0){ p1y[i] += 6; } if(i==1){ p2y[i] -= 1.5; System.out.println("steps: " + steps +" "+ p2y[i]); } if(i==2){ p2y[i] -= 1.5; p3x[i] -= 6; } if(i==3){ p2y[i] -= 6; p1y[i] += 1.5; p3x[i] -= 6; } if(i==4){ p2y[i] -= 6; p1y[i] += 1.5; p3x[i] -= 4.5; } if(i==5){ p2y[i] -= 1.5; p1y[i] += 4.5; p3x[i] -= 4.5; } if(i==6){ p2y[i] -= 1.5; p1y[i] += 4.5; p3x[i] -= 1.5; } if(i==7){ p1y[i] += 6; p3x[i] -= 1.5; } if(i==8){ p1y[i] += 6; } } else if((steps&lt;=60) &amp;&amp; (steps&gt;40)){ p2y[i] +=10; p1y[i] -=10; p3x[i] +=10; if(i==0){ p1y[i] -= 6; } if(i==1){ p2y[i] += 1.5; System.out.println("steps: " + steps +" "+ p2y[i]); } if(i==2){ p2y[i] += 1.5; p3x[i] += 6; } if(i==3){ p2y[i] += 6; p1y[i] -= 1.5; p3x[i] += 6; } if(i==4){ p3x[i] += 4.5; p2y[i] += 6; p1y[i] -= 1.5; //p3x[i] += 4.5; } if(i==5){ p2y[i] += 1.5; p1y[i] -= 4.5; p3x[i] += 4.5; } if(i==6){ p2y[i] += 1.5; p1y[i] -= 4.5; p3x[i] += 1.5; } if(i==7){ p1y[i] -= 6; p3x[i] += 1.5; } if(i==8){ p1y[i] -= 6; } } else if((steps&lt;=40) &amp;&amp; (steps&gt;20)){ p2y[i] -=10; p1y[i] +=10; p3x[i] -=10; if(i==0){ p1y[i] += 6; } if(i==1){ p2y[i] -= 1.5; System.out.println("steps: " + steps +" "+ p2y[i]); } if(i==2){ p2y[i] -= 1.5; p3x[i] -= 6; } if(i==3){ p2y[i] -= 6; p1y[i] += 1.5; p3x[i] -= 6; } if(i==4){ p2y[i] -= 6; p1y[i] += 1.5; p3x[i] -= 4.5; } if(i==5){ p2y[i] -= 1.5; p1y[i] += 4.5; p3x[i] -= 4.5; } if(i==6){ p2y[i] -= 1.5; p1y[i] += 4.5; p3x[i] -= 1.5; } if(i==7){ p1y[i] += 6; p3x[i] -= 1.5; } if(i==8){ p1y[i] += 6; } } else if((steps&lt;=20) &amp;&amp; (steps&gt;0)){ p2y[i] +=10; p1y[i] -=10; p3x[i] +=10; if(i==0){ p1y[i] -= 6; } if(i==1){ p2y[i] += 1.5; System.out.println("steps: " + steps +" "+ p2y[i]); } if(i==2){ p2y[i] += 1.5; p3x[i] += 6; } if(i==3){ p2y[i] += 6; p1y[i] -= 1.5; p3x[i] += 6; } if(i==4){ p3x[i] += 4.5; p2y[i] += 6; p1y[i] -= 1.5; //p3x[i] += 4.5; } if(i==5){ p2y[i] += 1.5; p1y[i] -= 4.5; p3x[i] += 4.5; } if(i==6){ p2y[i] += 1.5; p1y[i] -= 4.5; p3x[i] += 1.5; } if(i==7){ p1y[i] -= 6; p3x[i] += 1.5; } if(i==8){ p1y[i] -= 6; } } } repaint(); if (--steps ==0) tim.stop(); } public void paintComponent(Graphics g ) { super.paintComponent(g); this.setBackground(Color.white); g.drawLine(400, 0,400, 800); g.drawLine(0, 400, 800, 400); int[] p2xintarray =new int[9]; int[] p2yintarray =new int[9]; int[] p1xintarray =new int[9]; int[] p1yintarray =new int[9]; int[] p3xintarray =new int[9]; int[] p3yintarray =new int[9]; for (int i=0; i&lt;Array.getLength(p2x);i++){ int p2xint= (int)p2x[i]; p2xintarray[i]=p2xint; int p2yint= (int)p2y[i]; p2yintarray[i]=p2yint; int p1xint=(int)p1x[i]; p1xintarray[i]=p1xint; int p1yint= (int)p1y[i]; p1yintarray[i]=p1yint; int p3xint=(int)p3x[i]; p3xintarray[i]=p3xint; int p3yint= (int)p3y[i]; p3yintarray[i]=p3yint; } Polygon t = new Polygon(p2xintarray, p2yintarray, 9); g.drawPolygon(t); //Polygon ti = new Polygon(p2xi, p2yi, 9); //g.drawPolygon(ti); Polygon u = new Polygon(p3xintarray, p3yintarray, 9); g.drawPolygon(u); Polygon l = new Polygon(p1xintarray, p1yintarray, 9); g.drawPolygon(l); } public static void main(String[] args) { JFrame frame = new JFrame("Drawing line and a moving polygon"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test sl = new test(); frame.getContentPane().add(sl); frame.setSize(700,700); frame.setVisible(true); sl.tim.start(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload