绕 x 轴和 y 轴旋转多边形
当我更改多边形的值时,我遇到旋转后坐标不完全正确的问题,因为我的许多计算使用双精度和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了重构和更改幻数之外,还可以考虑使用 Path2D.Double 对象而不是 Polygon。
In addition to refactoring and changing magic numbers, consider using a Path2D.Double object instead of a Polygon.
使用
AffineTransform
< /a> 以与所需轴正交的方向缩放,如问题的答案中所述。另请注意,Polygon
有一个translate()
方法,可以显着简化您的代码。附录:这里还有两种可以增强幻觉的技术:
当
Shape
在其行程极限处改变方向时,交替显示和隐藏所选的轴。这可以通过更改每个轴的绘制顺序来实现。通过取消正交方向上的比例,在改变方向时反转形状。例如,当
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 thatPolygon
has atranslate()
method that would significantly simplify your code.Addendum: Here's two more techniques that may enhance the illusion:
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.Invert the
Shape
as it changes direction by negating the scale in the orthogonal direction. For example, when theShape
is "behind" the x axis, usescale(1, -scaleFactor)
, which affects the vertical size.