绕 x 轴和 y 轴旋转多边形

发布于 2024-10-31 11:47:32 字数 9637 浏览 6 评论 0原文

当我更改多边形的值时,我遇到旋转后坐标不完全正确的问题,因为我的许多计算使用双精度和 Polygon 构造函数仅接受 int[]。有没有更好的方法来解决这个问题并达到相同的结果?

请注意,顶点向“z”方向(框架外部)的旋转是通过缩放实现的;并且每个字母的坐标轴相对于框架的坐标轴具有相对位置。

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();
    }
}

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北陌 2024-11-07 11:47:32

除了重构和更改幻数之外,还可以考虑使用 Path2D.Double 对象而不是 Polygon。

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

没有心的人 2024-11-07 11:47:32

使用 AffineTransform< /a> 以与所需轴正交的方向缩放,如问题的答案中所述。另请注意,Polygon 有一个 translate() 方法,可以显着简化您的代码。

附录:这里还有两种可以增强幻觉的技术:

  1. Shape在其行程极限处改变方向时,交替显示和隐藏所选的轴。这可以通过更改每个轴的绘制顺序来实现。

  2. 通过取消正交方向上的比例,在改变方向时反转形状。例如,当 Shape 位于 x 轴“后面”时,请使用 scale(1, -scaleFactor),这会影响垂直尺寸。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文