// a triangle that can remember if it has been clicked, and can shrink on request class Triangle implements Comparable { Vector3d a,b,c; Vector3d pa,pb,pc; boolean clicked = false; Triangle() { this.a = new Vector3d(); this.b = new Vector3d(); this.c = new Vector3d(); } void project() { pa = a.project(); pb = b.project(); pc = c.project(); } boolean mouseOver() { // "cheating" using Java's generic polygon class instead of writing my own triangle intersection routine, which probably would be faster Polygon p = new Polygon( new int [] { (int)pa.x, (int)pb.x, (int)pc.x }, new int [] { (int)pa.y, (int)pb.y, (int)pc.y }, 3); return p.inside(mouseX,mouseY); } int compareTo(Object other) { // compare average depth float d1 = pa.z+pb.z+pc.z; Triangle t2 = (Triangle)other; float d2 = t2.pa.z+t2.pb.z+t2.pc.z; if (d1>d2) { return 1; } else if (d1==d2) { return 0; } else { return -1; } } // NB: will never stop shrinking, which is silly. void shrink() { Vector3d av = new Vector3d((a.x+b.x+c.x)/3.0,(a.y+b.y+c.y)/3.0,(a.z+b.z+c.z)/3.0); a.x += (av.x-a.x)/5.0; a.y += (av.y-a.y)/5.0; a.z += (av.z-a.z)/5.0; b.x += (av.x-b.x)/5.0; b.y += (av.y-b.y)/5.0; b.z += (av.z-b.z)/5.0; c.x += (av.x-c.x)/5.0; c.y += (av.y-c.y)/5.0; c.z += (av.z-c.z)/5.0; } }