2

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 Polygon constructor only takes in int[]. Is there a better way to go about this and achieve the same results?

Note that rotations of vertices into the “z-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.

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<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>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<=100) && (steps>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<=80) && (steps>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<=60) && (steps>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<=40) && (steps>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<=20) && (steps>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<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();
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Colin Gray
  • 79
  • 2
  • 11
  • 3
    What are all those magic numbers being added and subtracted? – luqui Apr 09 '11 at 15:48
  • 1
    The second thing I'd do (after getting rid of the magic numbers) would be to refactor that code as there seems to be an awful lot of needless repetition making debugging even harder. – Hovercraft Full Of Eels Apr 09 '11 at 16:04
  • possible duplicate of [Rotating a shape vertically around the x-axis](http://stackoverflow.com/questions/5593066/rotating-a-shape-vertically-around-the-x-axis) – trashgod Apr 09 '11 at 16:41
  • I just did the math on paper and plugged them in because I could not figure out AffineTransform and didnt have any more time to waste on playing around with it. WHat would be the best way to refactor the code so the it alternates between the 2 directions? – Colin Gray Apr 10 '11 at 06:05

2 Answers2

3

In addition to refactoring and changing magic numbers, consider using a Path2D.Double object instead of a Polygon.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    +1 for re-factoring. I'd argue that any [`Shape`](http://download.oracle.com/javase/6/docs/api/java/awt/Shape.html) would be a reasonable alternative. – trashgod Apr 09 '11 at 16:50
  • @trashgod: I agree, but was surprised to learn that Polygon **is** a Shape in that it implements the interface, but it has a legacy smell to it. – Hovercraft Full Of Eels Apr 09 '11 at 17:22
  • Aha, I see what you mean: it _is_ a legacy from 1.0. Most of the rest seem to have come in with `Graphics2D`. Thanks! – trashgod Apr 09 '11 at 17:58
2

Use AffineTransform to scale in the direction orthogonal to the desired axis, as mentioned in this answer to your question. Also, note that Polygon has a translate() method that would significantly simplify your code.

Addendum: Here's two more techniques that may enhance the illusion:

  1. Alternately show and hide the chosen axis as the Shape changes direction at the limits of its travel. This can be accomplished by changing the order in which each is drawn.

  2. Invert the Shape as it changes direction by negating the scale in the orthogonal direction. For example, when the Shape is "behind" the x axis, use scale(1, -scaleFactor), which affects the vertical size.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I looked up AffineTransform, but I can't figure out how to scale it in the orthogonal direction. – Colin Gray Apr 10 '11 at 06:02
  • [Orthogonal](http://en.wikipedia.org/wiki/Orthogonality) means perpendicular. If you're simulating rotation around the the _x_ axis, then you'll want to scale as you translate in the _y_ direction. – trashgod Apr 10 '11 at 06:29