/* Face.java
 * Author: David Wanqian Liu
 * Date: Dec 20, 1995
 */

import java.awt.*;


/** Representation of a polygon face in a 3D model
 */
class Face {
  public int    nvertices;
  public Vertex vertices[];
  public Color  color;

  public float  xmin, xmax;
  public float  ymin, ymax;
  public float  zmin, zmax;


  public Face() {
    nvertices = 0;
    vertices = null;
  }


  public Face(int nvertices) {
    this.nvertices = nvertices;
    vertices = new Vertex[nvertices];
  }




  void calcParam() {
    if (nvertices < 1) return;

    /*    float zsum = 0.0f;
     *    for (int i=0; i<nvertices; i++) {
     *      zsum += vertices[i].z;
     *    }
     *    z = zsum/nvertices;
     */

    xmin = vertices[0].x; xmax = xmin;
    ymin = vertices[0].y; ymax = ymin;
    zmin = vertices[0].z; zmax = zmin;

    for (int i=1; i<nvertices; i++) {
      xmin = Math.min(xmin, vertices[i].x);
      xmax = Math.max(xmax, vertices[i].x);
      ymin = Math.min(ymin, vertices[i].y);
      ymax = Math.max(ymax, vertices[i].y);
      zmin = Math.min(zmin, vertices[i].z);
      zmax = Math.max(zmax, vertices[i].z);
    }
  }


  /** Determine if face 'this' is obscuredby face f.
      This function is used in "depth-sort algorithm" or simplified version
      "painter's algorithm" to determine hidden faces. Normally either
      farthest z coordinate or average of z depths is used to sort polygons.
      Ref: "Computer Graphics, Principles and Practice, Second Edition"
      by Foley, et. al. Pg 673.
   */
  public boolean notObscuredBy(Face f) {
    /* Depth-sort algorithm
     * 1. Do the polygons' x extents not overlap
     * 2. Do the polygon's y extends not overlap
     * 3. Is 'this' entirely on the opposite side of f
     * 4. Is f entirely on the opposite side of 'this'
     * 5. Do the projection of the polygons onto the (x,y) plane not
     *    overlap
     * If none of the above is ture, then the model is not what we can
     * process, throws an exception.
     */

    /* I've only done zmax comparison for now */
    return (zmax < f.zmax);
  }
}
