Game Development Reference
In-Depth Information
Constructors : The default constructor will create a four-sided polygon. You can
also define an N-sided polygon by giving two arrays representing the X0,X1,. . .Xn
and Y0,Y1,. . .Yn coordinates of the vertices where (X0,Y0) represents the
coordinates of the first vertex and so forth, The constructor also takes the number
of sides.
calculateBounds(int xpoints[], int ypoints[], int npoints) : This method
calculates the bounds of the polygon given a set of X and Y coordinates and
number of points with respect to maximum integer values.
addPoint(int x, int y) : This one adds a point to the polygon given its X and Y
coordinates.
updateBounds(int x, int y) : This method updates the bounds of the polygon to
include a new point (X,Y).
getBoundingBox() : This returns the rectangular bounds of the polygon.
contains(int x, int y) : This one checks if the polygon contains a given point
(X,Y).
float[] getPoints() : This method returns the X and Y coordinates of the vertices
of the polygon.
Both the Polygon and Rectangle classes are used by the next class, PolygonSprite , to define all game
objects.
Listing 4-2. The Polygon Class for Asteroids
package ch04.common;
public class Polygon {
public int npoints;
public int[] ypoints;
public int[] xpoints;
protected Rectangle bounds;
public Polygon() {
xpoints = new int[4];
ypoints = new int[4];
}
public Polygon(int xpoints[], int ypoints[], int npoints) {
// Fix 4489009: should throw IndexOutofBoundsException instead
// of OutofMemoryException if npoints is huge
// and > {x,y}points.length
if (npoints > xpoints.length || npoints > ypoints.length) {
throw new IndexOutOfBoundsException(
"npoints > xpoints.length || npoints > ypoints.length");
}
Search WWH ::




Custom Search