// ortho.java ortho projection import java.awt.*; import java.awt.event.*; public class ortho extends Frame { int winWidth = 500; int winHeight = 500; double cube[][] = {{-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0, 1.0}, {1.0,-1.0, 1.0}, {1.0,1.0, 1.0}, {-1.0,1.0, 1.0}}; int colors[][] = {{255,0,0}, {255,255,0}, {0,255,0}, {0,0,255}, {255,0,255}, {0,255,255}, {128,128,128}, {0,0,0}}; double M_PI = 3.14159265358979323846; double alpha = M_PI/6.0; // 30 deg double beta = M_PI/4.0; // 45 deg boolean doedge = true; ortho() { System.out.println("ortho.java running"); setTitle("ortho"); setSize(winWidth,winHeight); setBackground(Color.white); setForeground(Color.black); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); this.addMouseListener (new mousePressHandler()); System.out.println("ortho.java finished, see ortho.png"); } // end ortho.java class mousePressHandler extends MouseAdapter { public void mousePressed (MouseEvent e) { int x, y, b; x = e.getX(); y = e.getY(); b = e.getButton(); if(b==3) doedge = !doedge; if(b==1) // quadrant select { if(xwinHeight/2) alpha = alpha - M_PI/36.0; if(x>winWidth/2 && ywinWidth/2 && y>winHeight/2) beta = beta - M_PI/36.0; } requestFocus(); repaint(); } } public void paint(Graphics g) { if(doedge) edgecube(g); else colorcube(g); g.drawString("ortho.java x,y,z", 20, winHeight-20); g.drawString("alpha="+Double.toString(alpha*180.0/M_PI), 20, 40); g.drawString("beta ="+Double.toString(beta*180.0/M_PI), 20, 60); g.drawString("angle alpha", winWidth/2-90, winHeight-30); g.drawString("angle beta" , winWidth/2+40, winHeight-30); } void polygon(int a, int b, int c , int d, int col, Graphics g) { int xp[] = {0, 1, 2, 3}; int yp[] = {0, 1, 2, 3}; int np = 4; int xc = winWidth/2; int yc = winHeight/2+10; double px1 = cube[a][0]; double py1 = cube[a][1]; double pz1 = cube[a][2]; double px2 = cube[b][0]; double py2 = cube[b][1]; double pz2 = cube[b][2]; double px3 = cube[c][0]; double py3 = cube[c][1]; double pz3 = cube[c][2]; double px4 = cube[d][0]; double py4 = cube[d][1]; double pz4 = cube[d][2]; double scale = (double)(winWidth/4-25); xp[0] = xc + (int)(scale*(px1*Math.cos(alpha) + py1*Math.cos(beta))); yp[0] = yc - (int)(scale*(py1*Math.sin(beta) - px1*Math.sin(alpha) + pz1)); xp[1] = xc + (int)(scale*(px2*Math.cos(alpha) + py2*Math.cos(beta))); yp[1] = yc - (int)(scale*(py2*Math.sin(beta) - px2*Math.sin(alpha) + pz2)); xp[2] = xc + (int)(scale*(px3*Math.cos(alpha) + py3*Math.cos(beta))); yp[2] = yc - (int)(scale*(py3*Math.sin(beta) - px3*Math.sin(alpha) + pz3)); xp[3] = xc + (int)(scale*(px4*Math.cos(alpha) + py4*Math.cos(beta))); yp[3] = yc - (int)(scale*(py4*Math.sin(beta) - px4*Math.sin(alpha) + pz4)); g.setColor(new Color(colors[col][0], colors[col][1], colors[col][2])); g.fillPolygon(xp, yp, np); } // end polygon void colorcube(Graphics g) { // map vertices with color to faces polygon(1, 0, 3, 2, 0, g); polygon(3, 7, 6, 2, 1, g); polygon(7, 3, 0, 4, 2, g); polygon(2, 6, 5, 1, 3, g); polygon(4, 5, 6, 7, 4, g); polygon(5, 4, 0, 1, 5, g); } // end colorcube void edge(int a, int b, int col, Graphics g) { int x1, y1, x2, y2; // screen x,y coordinates int xc = winWidth/2; int yc = winHeight/2+10; double px1 = cube[a][0]; double py1 = cube[a][1]; double pz1 = cube[a][2]; double px2 = cube[b][0]; double py2 = cube[b][1]; double pz2 = cube[b][2]; double scale = (double)(winWidth/4-25); x1 = xc + (int)(scale*(px1*Math.cos(alpha) + py1*Math.cos(beta))); y1 = yc - (int)(scale*(py1*Math.sin(beta) - px1*Math.sin(alpha) + pz1)); x2 = xc + (int)(scale*(px2*Math.cos(alpha) + py2*Math.cos(beta))); y2 = yc - (int)(scale*(py2*Math.sin(beta) - px2*Math.sin(alpha) + pz2)); g.setColor(new Color(colors[col][0], colors[col][1], colors[col][2])); g.drawLine(x1, y1, x2, y2); if(px1<0.0 && py1<0.0 && pz1<0.0) g.drawString("-1,-1,-1", x1, y1); if(px1>0.0 && py1>0.0 && pz1<0.0) g.drawString("1,1,-1", x1, y1); if(px1>0.0 && py1<0.0 && pz1<0.0) g.drawString("1,-1,-1", x1, y1); if(px1<0.0 && py1>0.0 && pz1<0.0) g.drawString("-1,1,-1", x1, y1); if(px1>0.0 && py1>0.0 && pz1>0.0) g.drawString("1,1,1", x1, y1); if(px1>0.0 && py1<0.0 && pz1>0.0) g.drawString("1,-1,1", x1, y1); if(px1<0.0 && py1>0.0 && pz1>0.0) g.drawString("-1,1,1", x1, y1); if(px1<0.0 && py1<0.0 && pz1>0.0) g.drawString("-1,-1,1", x1, y1); } // end polygon void edgecube(Graphics g) { // map vertices with color to faces edge(0, 1, 0, g); edge(1, 2, 1, g); edge(2, 3, 2, g); edge(3, 0, 3, g); edge(4, 5, 0, g); edge(5, 6, 1, g); edge(6, 7, 2, g); edge(7, 4, 3, g); edge(0, 4, 4, g); edge(1, 5, 5, g); edge(2, 6, 6, g); edge(3, 7, 7, g); } // end edgecube public static void main(String args[]) { new ortho(); } } // end ortho.java