/* cappello@cs.ucsb.edu Peter Cappello Perm: 123456 Project 4 */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class hypercube extends Applet implements ActionListener { double xyR = 0, xzR = 0, xwR = 0, yzR = 0, ywR = 0, zwR = 0; // rotations in radians Label xyL = new Label("XY"), xzL = new Label("XZ"), xwL = new Label("XW"), yzL = new Label("YZ"), ywL = new Label("YW"), zwL = new Label("ZW"); TextField xyT = new TextField("0", 3), xzT = new TextField("0", 3), xwT = new TextField("0", 3), yzT = new TextField("0", 3), ywT = new TextField("0", 3), zwT = new TextField("0", 3); // vertices of cube 2 x 2 x 2 x 2 X 4 double v[][][][][] = { {{{{-1, -1, -1, -1}, { 1, -1, -1, -1}}, {{-1, 1, -1, -1}, { 1, 1, -1, -1}}}, {{{-1, -1, 1, -1}, { 1, -1, 1, -1}}, {{-1, 1, 1, -1}, { 1, 1, 1, -1}}}}, {{{{-1, -1, -1, 1}, { 1, -1, -1, 1}}, {{-1, 1, -1, 1}, { 1, 1, -1, 1}}}, {{{-1, -1, 1, 1}, { 1, -1, 1, 1}}, {{-1, 1, 1, 1}, { 1, 1, 1, 1}}}} }; public void init() { add(xyL); add(xyT); add(xzL); add(xzT); add(xwL); add(xwT); add(yzL); add(yzT); add(ywL); add(ywT); add(zwL); add(zwT); // "this" applet handles ActionEvents for all TextFields xyT.addActionListener( this ); xzT.addActionListener( this ); xwT.addActionListener( this ); yzT.addActionListener( this ); ywT.addActionListener( this ); zwT.addActionListener( this ); } public void actionPerformed( ActionEvent e ) { xyR = Integer.parseInt( xyT.getText() )*2*Math.PI/360; xzR = Integer.parseInt( xzT.getText() )*2*Math.PI/360; xwR = Integer.parseInt( xwT.getText() )*2*Math.PI/360; yzR = Integer.parseInt( yzT.getText() )*2*Math.PI/360; ywR = Integer.parseInt( ywT.getText() )*2*Math.PI/360; zwR = Integer.parseInt( zwT.getText() )*2*Math.PI/360; repaint(); } public void paint(Graphics g) { // rotate vertices for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++) for (int z = 0; z < 2; z++) for (int w = 0; w < 2; w++) { xyRotate(v[x][y][z][w], Math.sin(xyR), Math.cos(xyR)); xzRotate(v[x][y][z][w], Math.sin(xzR), Math.cos(xzR)); xwRotate(v[x][y][z][w], Math.sin(xwR), Math.cos(xwR)); yzRotate(v[x][y][z][w], Math.sin(yzR), Math.cos(yzR)); ywRotate(v[x][y][z][w], Math.sin(ywR), Math.cos(ywR)); zwRotate(v[x][y][z][w], Math.sin(zwR), Math.cos(zwR)); } // draw edges for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++) for (int z = 0; z < 2; z++) { g.setColor(Color.red); drawEdge(v[x][y][z][0][0], v[x][y][z][0][1], v[x][y][z][1][0], v[x][y][z][1][1], g); g.setColor(Color.blue); drawEdge(v[x][y][0][z][0], v[x][y][0][z][1], v[x][y][1][z][0], v[x][y][1][z][1], g); g.setColor(Color.green); drawEdge(v[x][0][y][z][0], v[x][0][y][z][1], v[x][1][y][z][0], v[x][1][y][z][1], g); g.setColor(Color.black); drawEdge(v[0][x][y][z][0], v[0][x][y][z][1], v[1][x][y][z][0], v[1][x][y][z][1], g); } // draw vertices for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++) for (int z = 0; z < 2; z++) for (int w = 0; w < 2; w++) drawVertex(v[x][y][z][w][0], v[x][y][z][w][1], g); } // apply plane rotation to 3D point, // only 2 coordinates are affected final void xyRotate(double p[], double sin, double cos) { double temp; temp = cos*p[0] + sin*p[1]; p[1] = -sin*p[0] + cos*p[1]; p[0] = temp; } final void xzRotate(double p[], double sin, double cos) { double temp; temp = cos*p[0] + sin*p[2]; p[2] = -sin*p[0] + cos*p[2]; p[0] = temp; } final void xwRotate(double p[], double sin, double cos) { double temp; temp = cos*p[0] + sin*p[3]; p[3] = -sin*p[0] + cos*p[3]; p[0] = temp; } final void yzRotate(double p[], double sin, double cos) { double temp; temp = cos*p[1] + sin*p[2]; p[2] = -sin*p[1] + cos*p[2]; p[1] = temp; } final void ywRotate(double p[], double sin, double cos) { double temp; temp = cos*p[1] + sin*p[3]; p[3] = -sin*p[1] + cos*p[3]; p[1] = temp; } final void zwRotate(double p[], double sin, double cos) { double temp; temp = cos*p[2] + sin*p[3]; p[3] = -sin*p[2] + cos*p[3]; p[2] = temp; } final void drawEdge(double x1, double y1, double x2, double y2, Graphics g) { g.drawLine((int)(100*x1) + 200, (int)(100*(-y1)) + 240, (int)(100*x2) + 200, (int)(100*(-y2)) + 240); } final void drawVertex(double x, double y, Graphics g) { g.setColor(Color.yellow); g.fillOval((int)(100*x) + 200 - 2 , (int)(100*(-y)) + 240 - 2, 4, 4); g.setColor(Color.black); g.drawOval((int)(100*x) + 200 - 2 , (int)(100*(-y)) + 240 - 2, 4, 4); } }