/** * @file Tetrahedron.java * @see Cube.java *
* modified Monday 09 November 1998 * modified by Robert S Laramee *
* description The tetrahedron constructor was modified so that it is * passed a reference to the new BSPTree data structure * Same goes for the makefaces() method. */ import java.awt.*; import java.math.*; import java.util.*; //import matrix.*; class Tetrahedron extends Solid { private Face _faces[]; private Point4 _vtx[]; // vertices of the cube //-------- Tetrahedron ( Matrix4 xform, Color rgb ) ------------- public Tetrahedron ( Matrix4 xform, Color rgb, BSPTree tree) { super( xform, rgb ); makeFaces(tree); } //-------- Tetrahedron ( Color rgb ) ------------- public Tetrahedron ( Color rgb, BSPTree tree ) { super( rgb ); makeFaces(tree); } //-------- Tetrahedron ( Tetrahedron ) ----------------------------------- public Tetrahedron ( Tetrahedron l ) { super( l ); } //---------------------- copy () ----------------- public Shape copy ( ) { return new Tetrahedron( this ); } /** * ---------------------- makeFaces ( ) * The makeFaces() method was modified. It gets a reference to the BSP * Tree list so each of the new faces can be added to the tree. * @param tree -a reference to the BSP Tree */ private void makeFaces (BSPTree tree) { Point4 temp[]={ new Point4( 0, 0, 0 ), // left lower front new Point4( 1, 0, 0 ), // lower right front new Point4( 0.5, 1, -0.5 ), // middle top new Point4( 0, 0.5, -1 ), // middle bottom back }; _vtx = temp; _faces = new Face[4]; for (int i = 0; i < _vtx.length; i++) { _vtx[i] = _xform.timesPoint( _vtx[i] ); } Color cols[] = { _color, darken(_color, 0.6), darken(_color, 0.7), darken(_color, 0.8) }; _faces[0] = new Face(cols[0],_vtx[0],_vtx[1],_vtx[2]); // front _faces[1] = new Face(cols[1],_vtx[0],_vtx[2],_vtx[3]); // left _faces[2] = new Face(cols[2],_vtx[1],_vtx[3],_vtx[2]); // right _faces[3] = new Face(cols[3],_vtx[0],_vtx[3],_vtx[1]); // bottom /** add the faces to the BSP Tree */ for (int i = 0; i < _faces.length; i++) { tree.addFace(new String("tetra_face["+i+"]"), _faces[i]); } } //---------------------- draw (Graphics, Matrix4) ----------------- public void draw ( Graphics g, Matrix4 view ) { for (int i = 0; i < _faces.length; i++ ) _faces[i].draw(g , view); } //---------------------- brighten (Color, double brightenRatio ) -------- // This uses the "brightenRatio" parameter as a % amount to brighten the // Color. So if you want the Color to be 110% of its current // brightness, pass in 1.1 as the brightenRatio parameter. // It is actually public Color brighten ( Color rgb, double brightenRatio ) { if ( brightenRatio < 0 ) brightenRatio = -brightenRatio; return new Color ( Math.min(255, (int)(rgb.getRed()*brightenRatio)), Math.min(255, (int)(rgb.getGreen()*brightenRatio)), Math.min(255, (int)(rgb.getBlue()*brightenRatio)) ); } //---------------------- darken (Color, double darkenRatio ) -------- // This uses the "darkenRatio" parameter as a % amount to darken the // Color. So if you want the Color to be 90% of its current // brightness, pass in .9 as the darkenRatio parameter. public Color darken ( Color rgb, double darkenRatio ) { if ( darkenRatio < 0 ) darkenRatio = -darkenRatio; if ( darkenRatio > 1 ) darkenRatio = 1; return new Color ( (int)(rgb.getRed()*darkenRatio), (int)(rgb.getGreen()*darkenRatio), (int)(rgb.getBlue()*darkenRatio) ); } }