import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/*
  <applet code="affine" width=350 height=400>
  </applet>
*/


public class kadai4b extends Applet
implements ActionListener, TextListener {
	TextField t1,t2,t3,t4,t5,t6,t7,t8,t9;
	Label l1,l2,l3,l4,l5,l6,l7,l8,l9;
	Button b;
	Dimension d;
	int xc;
	int yc;
	int count = 18;
	int bunkatu = 4;
	int kazu = 10;
	double xkan = 0;
	double ykan = 0;
	double ans = 0;
	int p[][][][][] = new int[10][10][kazu][count][3];
	int p_new[][][][][] = new int[10][10][kazu][count][3];
	int p_new2[][][][][] = new int[10][10][kazu][count][3];
	double a[][] = new double[count][3];
	double kaiten[][][] = new double[kazu][count][3];

	public void init( ) {
		t1 = new TextField("1",4); t2 = new TextField("0",4); t3 = new TextField("0",4);
		t4 = new TextField("0",4); t5 = new TextField("1",4); t6 = new TextField("0", 4);
		t7 = new TextField("4",4); t8 = new TextField("100",4); t9 = new TextField("100",4);

		l1=new Label("x拡大"); l2=new Label("x歪み"); l3=new Label("x平行");
		l4=new Label("y歪み"); l5=new Label("y拡大"); l6=new Label("y平行");
		l7=new Label("分割数(1〜10)");
		l8=new Label("x間隔"); l9=new Label("y間隔");
		add(l1); add(t1); add(l2); add(t2); add(l3); add(t3);
		add(l4); add(t4); add(l5); add(t5); add(l6); add(t6);
		add(l7); add(t7); add(l8); add(t8); add(l9); add(t9);

		b = new Button("calc");
		b.addActionListener(this);
		add(b);

		for(int x=0; x<10; x++){
			for(int y=0; y<10; y++){
				for(int z=0; z<kazu; z++){
					p[x][y][z][0][0] = 50; p[x][y][z][0][1] = 0;
					p[x][y][z][1][0] = 50; p[x][y][z][1][1] = 150;
					p[x][y][z][2][0] = 0; p[x][y][z][2][1] = 200;
					p[x][y][z][3][0] = -50; p[x][y][z][3][1] = 150;
					p[x][y][z][4][0] = -50; p[x][y][z][4][1] = 0;
					p[x][y][z][5][0] = 0; p[x][y][z][5][1] = 50;
					p[x][y][z][6][0] = 50; p[x][y][z][6][1] = 0;
					p[x][y][z][7][0] = 0; p[x][y][z][7][1] = 200;
					p[x][y][z][8][0] = -50; p[x][y][z][8][1] = 0;
					p[x][y][z][9][0] = -50; p[x][y][z][9][1] = 100;
					p[x][y][z][10][0] = 0; p[x][y][z][10][1] = 200;
					p[x][y][z][11][0] = 50; p[x][y][z][11][1] = 100;
					p[x][y][z][12][0] = 50; p[x][y][z][12][1] = 30;
					p[x][y][z][13][0] = 0; p[x][y][z][13][1] = 80;
					p[x][y][z][14][0] = -50; p[x][y][z][14][1] = 30;
					p[x][y][z][15][0] = -50; p[x][y][z][15][1] = 0;
					p[x][y][z][16][0] = 0; p[x][y][z][16][1] = 50;
					p[x][y][z][17][0] = 50; p[x][y][z][17][1] = 0;
				}
			}
		}

		for(int x=0; x<10; x++){
			for(int y=0; y<10; y++){
				for(int z=0; z<kazu; z++){
					for(int i=0; i<count; i++){
						p[x][y][z][i][2] = 1;
					}
				}
			}
		}
    }

	public void actionPerformed(ActionEvent ae)
    {
		a[0][0] = Double.valueOf(t1.getText()).doubleValue();
		a[0][1] = Double.valueOf(t2.getText()).doubleValue();
		a[0][2] = Double.valueOf(t3.getText()).doubleValue();
		a[1][0] = Double.valueOf(t4.getText()).doubleValue();
		a[1][1] = Double.valueOf(t5.getText()).doubleValue();
		a[1][2] = Double.valueOf(t6.getText()).doubleValue();

		bunkatu = Integer.valueOf(t7.getText()).intValue();
		ans = 2*Math.PI/bunkatu;
		for(int z=0; z<kazu; z++){
			kaiten[z][0][0] = Math.cos((z+1)*ans);
			kaiten[z][0][1] = -1 * Math.sin((z+1)*ans);
			kaiten[z][0][2] = 0;
			kaiten[z][1][0] = Math.sin((z+1)*ans);
			kaiten[z][1][1] = Math.cos((z+1)*ans);
			kaiten[z][1][2] = 0;
			kaiten[z][2][0] = 0;
			kaiten[z][2][1] = 0;
			kaiten[z][2][2] = 1;
		}
		xkan = Double.valueOf(t8.getText()).doubleValue();
		ykan = Double.valueOf(t9.getText()).doubleValue();

		repaint( );
    }

	public void textValueChanged(TextEvent te) {
	}


	public void paint(Graphics g) {
		d = getSize( );
		xc = d.width /2;
		yc = d.height / 2;

		write_base(g);
		calc( );
		write_new(g);
	}

	public void write_base(Graphics g) {
		g.setColor(Color.white);
		g.fillRect(0, 0, d.width, d.height);
		g.setColor(Color.red);
		g.drawLine(xc, 0, xc, d.height);
		g.drawLine(0, yc, d.width, yc);
		g.setColor(Color.black);
		/*for(int z=0; z<kazu; z++){
			for(int i=0; i<count; i++){
				g.drawLine(p[z][i][0] + xc, yc - p[z][i][1], p[z][(i+1)%count][0] + xc, yc - p[z][(i+1)%count][1]);
			}
		}*/
	}

	public void calc( ) {
		for(int x=0; x<10; x++){
			for(int y=0; y<10; y++){
				for(int z=0; z<kazu; z++){
					for(int i=0; i<count; i++) {
						for(int j=0; j<3; j++) {
							p_new[x][y][z][i][j] = 0;
							p_new2[x][y][z][i][j] = 0;
						}
					}
				}
			}
		}

		for(int x=0; x<10; x++){
			for(int y=0; y<10; y++){
				for(int z=0; z<kazu; z++){
					for(int i=0; i<count; i++) {
						for(int j=0; j<3; j++) {
							for(int k=0; k<3; k++) {
								p_new[x][y][z][i][j] += (int ) (a[j][k]*p[x][y][z][i][k]);
							}
						}
					}
				}
			}
		}
		for(int x=0; x<10; x++){
			for(int y=0; y<10; y++){
				for(int z=0; z<kazu; z++){
					for(int i=0; i<count; i++) {
						for(int j=0; j<3; j++) {
							for(int k=0; k<3; k++) {
								p_new2[x][y][z][i][j] += (int ) (kaiten[z][j][k]*p_new[x][y][z][i][k]);
							}
						}
					}
				}
			}
		}
		for(int x=0; x<10; x++){
			for(int y=0; y<10; y++){
				for(int z=0; z<kazu; z++){
					for(int i=0; i<count; i++) {
							p_new2[x][y][z][i][0] += -500 + ((x+1)*xkan) ;
							p_new2[x][y][z][i][1] += 500 - ((y+1)*ykan) ;
					}
				}
			}
		}
	}

	public void write_new(Graphics g) {
		g.setColor(Color.blue);
		for(int x=0; x<10; x++){
			for(int y=0; y<10; y++){
				for(int z=0; z<kazu; z++){
					for(int i=0; i<count; i++){
						g.drawLine(p_new2[x][y][z][i][0] + xc, yc - p_new2[x][y][z][i][1],
							p_new2[x][y][z][(i+1)%count][0] + xc, yc - p_new2[x][y][z][(i+1)%count][1]);
					}
				}
			}
		}
	}

}






