checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / Triangle.java
1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
3
4 /**
5  *  an oriented triangle, defined by three points in clockwise order.
6  */
7 public abstract class Triangle implements HasBoundingBox {
8     public abstract Point p1();
9     public abstract Point p2();
10     public abstract Point p3();
11
12     /** the face normal vector */
13     public Vec norm() {
14         return p2().minus(p1()).cross(p3().minus(p1())).norm();
15     }
16
17     /** the area of the triangle */
18     public float area() {
19         return
20             (float)Math.abs(0.5*p1().distance(p2())
21                             * new Vec(p1(), p2()).norm().dot(new Vec(p2(), p3())));
22     }
23
24     /** issue gl.glVertex() for each of the triangle's points */
25     public void glVertices(GL gl) {
26         p1().glVertex(gl);
27         p2().glVertex(gl);
28         p3().glVertex(gl);
29     }
30
31     /** the triangle's centroid */
32     public Point centroid() {
33         return new Point((p1().x+p2().x+p3().x)/3,
34                          (p1().y+p2().y+p3().y)/3, 
35                          (p1().z+p2().z+p3().z)/3);
36     }
37
38     /** ratio of the area of the triangle to that of the square formed from its longest edge */
39     public float aspect() {
40         float max = Math.max(Math.max(p1().distance(p2()),
41                                       p2().distance(p3())),
42                              p3().distance(p1())) / 2;
43         return 1/(1+area()/(max*max));
44     }
45
46     public float getMaxX() { return Math.max(p1().x, Math.max(p2().x, p3().x)); }
47     public float getMinX() { return Math.min(p1().x, Math.min(p2().x, p3().x)); }
48     public float getMaxY() { return Math.max(p1().y, Math.max(p2().y, p3().y)); }
49     public float getMinY() { return Math.min(p1().y, Math.min(p2().y, p3().y)); }
50     public float getMaxZ() { return Math.max(p1().z, Math.max(p2().z, p3().z)); }
51     public float getMinZ() { return Math.min(p1().z, Math.min(p2().z, p3().z)); }
52 }