Example 23.22: The Triangle class

Example 23.22 is a class that represents a triangle. Although a more general face class would be possible, it's probably better to work with just the triangles out of which other faces are built.

The triangle is stored as three Point objects. As in the Line class, the draw method uses the Point's non-public fields x, y and z. This is permissible because the Triangle class and the Point class are in the same package, elharo.vrml.

package elharo.vrml;

public class Triangle extends shape {

  // a triangle is defined by three points
  Point p1;
  Point p2;
  Point p3;

  public Triangle(Point p1, Point p2, Point p3) {
    this.p1 = p1;
    this.p2 = p2;
    this.p3 = p3;
  }

  public Triangle(double x1, double y1, double z1,
   double x2, double y2, double z2,
   double x3, double y3, double z3) {
    p1 = new Point(x1, y1, z1);
    p2 = new Point(x2, y2, z2);
    p3 = new Point(x3, y3, z3);
  }

  public String draw() {
  
    String s1 = "Coordinate3 { point [ ";  
    String s2 = p1.x + " " + p1.y + " " + p1.z + ",\n";
    String s3 = p2.x + " " + p2.y + " " + p2.z + ",\n";
    String s4 = p3.x + " " + p3.y + " " + p3.z + ",\n";
    String s5 = "] }\n\n"; 
    String s6 = "IndexedFaceSet {\n coordIndex [ " +
      "0, 1, 2\n] \n}";       
  
    return s1 + s2 + s3 + s4 + s5 + s6;
    
  }

}

Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home