import java.awt.*; import java.awt.geom.*; import javax.swing.*; /** * A triangle in 3D, with three Vec vertices and a reflectivity (a Color). * A triangle is represented by the Vec objects of its three vertices. * A triangle has a color associated with it, for filling, illumination, etc. *
* For CSU540 Computer Graphics class, Spring 2005 * CCIS, Northeastern University *
* Includes linear transform using a Mat
*
* Method names ending with "Same" alter the given triangle.
* Others return a copy.
* @author Bob Futrelle
* @version 26 January 2005
*/
public class Triangle {
/** The three vertices */
public Vec[] tri;
/** The unit outward normal */
public Vec normal;
/** Color, for filling, illumination, etc. */
public Color color;
/**
* Tests and prints results of all methods (all are static)
*/
public static void main(String[] args) {
Vec v0 = new Vec(0.0,0.0,0.0);
Vec v1 = new Vec(100.0,0.0,0.0);
Vec v2 = new Vec(0.0,100.0,0.0);
Triangle t1 = new Triangle(v0, v1, v2, Color.red);
System.out.println("Triangle vertices are:" + t1);
Mat trans = Mat.transMat(1.0, 2.0, 3.0);
System.out.println("Translation matrix: " + trans);
Triangle t2 = Triangle.transformTriangle(trans, t1);
System.out.println("Translated triangle vertices are:" + t2);
}
/**
* Lists the three vertices
*/
public String toString(){
return "\n" +
tri[0] + "\n" +
tri[1] + "\n" +
tri[2] + "\n";
}
/**
* Creates black Triangle with the 3-element Vec array.
*/
public Triangle() {
tri = new Vec[3];
for(int trindex = 0; trindex < 3; trindex++)
tri[trindex] = new Vec();
color = Color.black;
}
/**
* Creates colored Triangle with the three given vertices
*/
public Triangle(Vec v0, Vec v1, Vec v2, Color c) {
this(); // calls default constructor above
tri[0] = v0;
tri[1] = v1;
tri[2] = v2;
color = c;
}
/**
* Produces a copy of the triangle after a linear transform is applied.
*
* TO BE COMPLETED:
* THIS SHOULD NOT BE A STATIC METHOD. CHANGE IT.
* @param mat The linear transform.
* @param t1 The triangle to be transformed.
* @return A new triangle which has the transformed vertices.
*/
public static Triangle transformTriangle(Mat mat, Triangle t1){
Triangle triReturn = new Triangle();
for(int vert = 0; vert < 3; vert++)
triReturn.tri[vert] = Mat.matrixXvector(mat, t1.tri[vert]);
return triReturn;
}
/**
* Alters this triangle by applying a linear transform is applied.
*
* TO BE COMPLETED:
*
* @param mat The linear transform.
*/
public void transformTriangleSame(Mat mat){
// TO BE COMPLETED
}
/**
* Finds the unit normal to a triangle.
* The perimeter is traversed in order v0, v1, v2.
* Sets the normal field of Triangle.
*
* TO BE COMPLETED
*
*/
public Vec normalToTriangle(){
Vec normal = new Vec();
// TO BE COMPLETED
return normal;
}
/**
* Used to match to standard moveTo() which takes two
* integer arguments. (private, so normally, no javadoc).
*/
private void pthMoveToVec(GeneralPath path, Vec v){
path.moveTo(Math.round(v.vec[0]), Math.round(v.vec[1]));
}
/**
* Used to match to standard lineTo() which takes two
* integer arguments. (private, so normally, no javadoc).
*/
private void pthLineToVec(GeneralPath path, Vec v){
path.lineTo(Math.round(v.vec[0]), Math.round(v.vec[1]));
}
/**
* Creates and closes a GeneralPath for the triangle, sets the color and fills it.
*/
public void drawTri(Graphics g){
Graphics2D g2d = (Graphics2D)g;
GeneralPath pth = new GeneralPath();
pthMoveToVec(pth,tri[0]);
pthLineToVec(pth,tri[1]);
pthLineToVec(pth,tri[2]);
pth.closePath();
g2d.setPaint(color);
g2d.fill(pth);
}
} // class Triangle