com.brackeen.javagamebook.math3D
Class Polygon3D

java.lang.Object
  extended bycom.brackeen.javagamebook.math3D.Polygon3D
All Implemented Interfaces:
Transformable
Direct Known Subclasses:
SolidPolygon3D, TexturedPolygon3D

public class Polygon3D
extends java.lang.Object
implements Transformable

The Polygon3D class represents a polygon as a series of vertices.


Constructor Summary
Polygon3D()
          Creates an empty polygon that can be used as a "scratch" polygon for transforms, projections, etc.
Polygon3D(Vector3D[] vertices)
          Creates a new Polygon3D with the specified vertices.
Polygon3D(Vector3D v0, Vector3D v1, Vector3D v2)
          Creates a new Polygon3D with the specified vertices.
Polygon3D(Vector3D v0, Vector3D v1, Vector3D v2, Vector3D v3)
          Creates a new Polygon3D with the specified vertices.
 
Method Summary
 void add(Transform3D xform)
           
 void add(Vector3D u)
           
 void addRotation(Transform3D xform)
           
 Rectangle3D calcBoundingRectangle()
          Calculates and returns the smallest bounding rectangle for this polygon.
 Vector3D calcNormal()
          Calculates the unit-vector normal of this polygon.
 boolean clip(float clipZ)
          Clips this polygon so that all vertices are in front of the clip plane, clipZ (in other words, all vertices have z <= clipZ).
 Vector3D getNormal()
          Gets the normal of this polygon.
 int getNumVertices()
          Gets the number of vertices this polygon has.
 Vector3D getVertex(int index)
          Gets the vertex at the specified index.
 void insertVertex(int index, Vector3D vertex)
          Inserts a vertex into this polygon at the specified index.
 boolean isFacing(Vector3D u)
          Tests if this polygon is facing the specified location.
 void project(ViewWindow view)
          Projects this polygon onto the view window.
 void setNormal(Vector3D n)
          Sets the normal of this polygon.
 void setTo(Polygon3D polygon)
          Sets this polygon to the same vertices as the specfied polygon.
 void subtract(Transform3D xform)
           
 void subtract(Vector3D u)
           
 void subtractRotation(Transform3D xform)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Polygon3D

public Polygon3D()
Creates an empty polygon that can be used as a "scratch" polygon for transforms, projections, etc.


Polygon3D

public Polygon3D(Vector3D v0,
                 Vector3D v1,
                 Vector3D v2)
Creates a new Polygon3D with the specified vertices.


Polygon3D

public Polygon3D(Vector3D v0,
                 Vector3D v1,
                 Vector3D v2,
                 Vector3D v3)
Creates a new Polygon3D with the specified vertices. All the vertices are assumed to be in the same plane.


Polygon3D

public Polygon3D(Vector3D[] vertices)
Creates a new Polygon3D with the specified vertices. All the vertices are assumed to be in the same plane.

Method Detail

setTo

public void setTo(Polygon3D polygon)
Sets this polygon to the same vertices as the specfied polygon.


getNumVertices

public int getNumVertices()
Gets the number of vertices this polygon has.


getVertex

public Vector3D getVertex(int index)
Gets the vertex at the specified index.


project

public void project(ViewWindow view)
Projects this polygon onto the view window.


add

public void add(Vector3D u)
Specified by:
add in interface Transformable

subtract

public void subtract(Vector3D u)
Specified by:
subtract in interface Transformable

add

public void add(Transform3D xform)
Specified by:
add in interface Transformable

subtract

public void subtract(Transform3D xform)
Specified by:
subtract in interface Transformable

addRotation

public void addRotation(Transform3D xform)
Specified by:
addRotation in interface Transformable

subtractRotation

public void subtractRotation(Transform3D xform)
Specified by:
subtractRotation in interface Transformable

calcNormal

public Vector3D calcNormal()
Calculates the unit-vector normal of this polygon. This method uses the first, second, and third vertices to calcuate the normal, so if these vertices are collinear, this method will not work. In this case, you can get the normal from the bounding rectangle. Use setNormal() to explicitly set the normal. This method uses static objects in the Polygon3D class for calculations, so this method is not thread-safe across all instances of Polygon3D.


getNormal

public Vector3D getNormal()
Gets the normal of this polygon. Use calcNormal() if any vertices have changed.


setNormal

public void setNormal(Vector3D n)
Sets the normal of this polygon.


isFacing

public boolean isFacing(Vector3D u)
Tests if this polygon is facing the specified location. This method uses static objects in the Polygon3D class for calculations, so this method is not thread-safe across all instances of Polygon3D.


clip

public boolean clip(float clipZ)
Clips this polygon so that all vertices are in front of the clip plane, clipZ (in other words, all vertices have z <= clipZ). The value of clipZ should not be 0, as this causes divide-by-zero problems. Returns true if the polygon is at least partially in front of the clip plane.


insertVertex

public void insertVertex(int index,
                         Vector3D vertex)
Inserts a vertex into this polygon at the specified index. The exact vertex in inserted (not a copy).


calcBoundingRectangle

public Rectangle3D calcBoundingRectangle()
Calculates and returns the smallest bounding rectangle for this polygon.