hermes.hshape
Class HPolygon

java.lang.Object
  extended by hermes.hshape.HShape
      extended by hermes.hshape.HPolygon

public class HPolygon
extends HShape

Represents an arbitrary convex polygon.

Position represents 'center.'

Vertex Points are positioned relative center. Each point is assumed to be next to points before and after it in list. Make sure your List of points is ordered correctly!

A HPolygon must also be convex, concave polygons will break collision detection.


Field Summary
 
Fields inherited from class hermes.hshape.HShape
_position
 
Constructor Summary
HPolygon(PVector position, java.util.ArrayList<PVector> points)
          Creates a new HPolygon.
HPolygon(PVector position, PVector... points)
          Creates a new HPolygon.
 
Method Summary
 void addPoint(PVector point)
          Adds a point to the polygon.
 boolean collide(HCircle other)
           
 boolean collide(HPolygon other)
           
 boolean collide(HRectangle other)
           
 boolean collide(HShape other)
          Detects a collision with another shape
 boolean contains(float x, float y)
           
 boolean contains(PVector point)
          Whether the Shape contains the given point coordinates (boundary is inclusive).
static HPolygon createRegularHPolygon(PVector pos, int sides, float radius)
          Creates a new regular polygon with a given number of sides at the given location.
 void draw()
          Shortcut method for drawing the shape on screen
protected  java.util.ArrayList<PVector> getAxes()
          Getter for axes list - only for internal use within shape classes
 java.util.ArrayList<PVector> getAxesCopy()
           
 HRectangle getBoundingBox()
          Finds the smallest Rectangle that encloses the shape.
 java.util.ArrayList<PVector> getPoints()
           
 java.util.ArrayList<PVector> getPointsCopy()
           
 PVector projectionVector(HCircle other)
           
 PVector projectionVector(HPolygon other)
           
 PVector projectionVector(HRectangle other)
           
 PVector projectionVector(HShape other)
          Finds the projection vector for a collision with another shape.
 void rotate(double theta)
          Rotates polygon counter-clockwise around polygon's position ((0,0) in polygon coordinates).
 void rotate(PVector pivotLoc, double theta)
          Rotates polygon counter-clockwise around given position in polygon coordinates ((0,0) is polygon's position).
 java.lang.String toString()
           
 
Methods inherited from class hermes.hshape.HShape
getPosition
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HPolygon

public HPolygon(PVector position,
                java.util.ArrayList<PVector> points)
Creates a new HPolygon.

List of vertex points must be ordered such that each point is connected to points before and after it in list.

Parameters:
position - - Reference to Shape's position
points - - List of vertex points defined relative to position, must be ordered

HPolygon

public HPolygon(PVector position,
                PVector... points)
Creates a new HPolygon.

Takes a variable number of PVectors (must give at least 3) for vertices.

Vertex points must be given in order such that each point is connected to points given before and after it.

Parameters:
position - - Reference to Shape's position
points - - the PVectors defining the verticies of the polygon
Method Detail

getPoints

public java.util.ArrayList<PVector> getPoints()
Returns:
List of vertex points.

getPointsCopy

public java.util.ArrayList<PVector> getPointsCopy()
Returns:
Copy of list of vertex points.

addPoint

public void addPoint(PVector point)
Adds a point to the polygon.

Point is assumed to be connected to the last point added and the first point added.

Parameters:
point - - point to be added

getAxes

protected java.util.ArrayList<PVector> getAxes()
Getter for axes list - only for internal use within shape classes

Returns:
Axes list, do not modify contents!

getAxesCopy

public java.util.ArrayList<PVector> getAxesCopy()
Returns:
Copy of axes list.

rotate

public void rotate(double theta)
Rotates polygon counter-clockwise around polygon's position ((0,0) in polygon coordinates).

Parameters:
theta - Angle to rotate by

rotate

public void rotate(PVector pivotLoc,
                   double theta)
Rotates polygon counter-clockwise around given position in polygon coordinates ((0,0) is polygon's position).

Parameters:
pivotLoc - The point to rotate polygon around
theta - Angle to rotate by

collide

public boolean collide(HShape other)
Description copied from class: HShape
Detects a collision with another shape

Overrides:
collide in class HShape
Parameters:
other - the shape to collide with
Returns:
whether the shapes have collided

collide

public boolean collide(HRectangle other)

collide

public boolean collide(HCircle other)

collide

public boolean collide(HPolygon other)

projectionVector

public PVector projectionVector(HShape other)
Description copied from class: HShape
Finds the projection vector for a collision with another shape.

The projection vector is the vector by which one body could be displaced such that it no longer intersects the other body, by the shortest possible distance.

Each shape must contain methods for collision with all other types of HShapes. If you create your own type of shape, you will have to modify HShape (as well as its subclasses) to contain methods colliding it with the new type.

Specified by:
projectionVector in class HShape
Parameters:
other - the shape to collide with
Returns:
the projection vector from this body to other, or null if they aren't colliding

projectionVector

public PVector projectionVector(HRectangle other)
Specified by:
projectionVector in class HShape

projectionVector

public PVector projectionVector(HCircle other)
Specified by:
projectionVector in class HShape

projectionVector

public PVector projectionVector(HPolygon other)
Specified by:
projectionVector in class HShape

contains

public boolean contains(PVector point)
Description copied from class: HShape
Whether the Shape contains the given point coordinates (boundary is inclusive).

Specified by:
contains in class HShape
Parameters:
point - the point vector
Returns:
true if (x,y) lies within the Shape

contains

public boolean contains(float x,
                        float y)
Specified by:
contains in class HShape
Parameters:
x - the x coordinate
y - the y coordinate
Returns:
true if (x,y) lies within the Shape

getBoundingBox

public HRectangle getBoundingBox()
Description copied from class: HShape
Finds the smallest Rectangle that encloses the shape.

Specified by:
getBoundingBox in class HShape
Returns:
the bounding box

draw

public void draw()
Description copied from class: HShape
Shortcut method for drawing the shape on screen

Specified by:
draw in class HShape

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

createRegularHPolygon

public static HPolygon createRegularHPolygon(PVector pos,
                                             int sides,
                                             float radius)
Creates a new regular polygon with a given number of sides at the given location.

Radius determines how far away the vertices are from the center.

Parameters:
pos - position of polygon
sides - number of sides in the polygon
radius - determines size of polygon