/* Will Dietz dtzTech.com (c) 2004 This is subject to the GNU GPL license see http://www.gnu.org/licenses/gpl.txt for a copy of the license */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class App7 extends Applet implements ActionListener { Timer t; final double FRAMERATE = 50,CUBE=10; //Frames per second final double SCALE_K=150*CUBE,SCALE_C=20*CUBE,SCALE_A=1.8; final int WIDTH = 1024, HEIGHT = 768; final double PI=Math.PI,ANGLE=30.0; public final int CX = WIDTH/2,CY = HEIGHT/2;//Middle of screen public final double ROTFACTOR=4,MOVFACTOR=6,ANG=.4; public final double PX=0,PY=800,PZ=1; public final double FACTOR = .2,CLOSERANGE=30; public Point PP= new Point(PX,PY,PZ);//,VP= new Point(PX,PY-200,PZ-100); public final double OFFX = 0, OFFY = -PY,OFFZ = -PZ; public Point LP= new Point(PX,PY,PZ); public Point OP = new Point(0,0,0),origin = new Point(0,0,0); public KeyStates key; public Vector vDir; public double[] arr,cc; public final int NUMOFCUBES=9; Prism c[] = new Prism[NUMOFCUBES]; Face f[] = new Face[NUMOFCUBES*6]; public void init() { // for (int a=0;a0)//is this farther?? { tmp = fce[a]; fce[a] = fce[a+1]; fce[a+1]=tmp; done = false; } } } System.out.println("sort done"); return fce; } public double[] bubbleSort(double[] dbl) { double[] arr = new double[dbl.length]; for (int a=0;aarr[a+1]) {//SWITCH: tmp = arr[a]; arr[a]=arr[a+1]; arr[a+1]=tmp; done = false; } } return arr; } public void paint(Graphics g) { // updateObjs(); Point lpp1,lpp2; Buffer.g.setColor(Color.black); Buffer.g.fillRect(0,0,WIDTH,HEIGHT); // Prism[] psm = sort(c); Face[] fce = sort(f); // for (int a=NUMOFCUBES-1;a>=0;a--) // for (int b=0;b1 for (int a=fce.length-1;a>=0;a--) if (isInView(fce[a].center()))fce[a].draw(Buffer.g); g.drawImage(Buffer.img,0,0,this); } public boolean isMoveInvalid(double x,double y,double z) { Point p = new Point(PP.x+x,PP.y+y,PP.z+z); boolean bool; for (int i=0;iANG); } public void actionPerformed(ActionEvent ae) { updateObjs(); paint(this.getGraphics()); } void mov() { double yDiff=0; if (key.UP && arr[0]>CLOSERANGE)yDiff=-MOVFACTOR;//PP.y-=MOVFACTOR; if (key.DOWN)yDiff=MOVFACTOR;//PP.y+=MOVFACTOR; if (key.LEFT)rot(-ROTFACTOR,PP);//PP.x++; if (key.RIGHT)rot(ROTFACTOR,PP);//PP.x--; if(!isMoveInvalid(0,yDiff,0))PP.y+=yDiff; // Point p = new Point(PP.x,PP.y+yDiff,PP.z); cc = new double[NUMOFCUBES]; for (int count=0;countCLOSERANGE)PP.y+=yDiff; } void updateObjs() { mov(); LP = PP; vDir = new Vector(OP,PP); OP = new Point(PP.x-OFFX,PP.y-OFFY,PP.z-OFFZ); for (double a=0;adbl2+LIMIT)?1:-1); } public void rot(double d,Point relO) { for (int a=0;a<8;a++) { p[a].rot(d,relO); } update(); } void update() { update2D(); for (int a=0;a<4;a++) dist[a]=f[a].getDist(PP); dist=bubbleSort(dist); center = p[0].midPoint(p[6]); updMins(); } void updMins() { minX=10000000; minY=10000000; minZ=10000000; for (int a=0;a<4;a++) { f[a].updMins(); if (f[a].minXdis)minD=dis; } return (minDdbl2) return 1; return -1; // return (Math.abs(dbl1-dbl2)dbl2+LIMIT)?1:-1); } public Face(int[] pts,Point[] ppp,Point[] pp2,Color col,int aa) { a=aa; len=pts.length; p = pts; for (int a=0;amax.x)max.x=xx; if (yy>max.y)max.y=yy; if (zz>max.z)max.z=zz; if (xxmax.x)xx=max.x; if (yy>max.y)yy=max.y; if (zz>max.z)zz=max.z; if (xxxxx?xx:xxx; maxY = yy>yyy?yy:yyy; maxZ = zz>zzz?zz:zzz; } public Point getPoint(int index) { return pp[p[index]]; } public Point get2DPoint(int index) { return p2[p[index]]; } public void draw(Graphics graph) { int b = a/6; Point center=c[a].center; Point[] pp2 = new Point[len]; Vector norm,light; for (int a=0;a