morey.spore
Class RPolygon

java.lang.Object
  |
  +--morey.spore.RPolygon

public class RPolygon
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable

a regular polygon with unit length sides. This fundamental class has two functions that it serves the shape of the spores as well as the shape of the claims. Since these objects are meant to be passed around a lot the only the essential information is kept. The essentials are:

The collisions are probably the most interesting methods and require the most explaination.

Here is a link to the source code

See Also:
Spore, Link, Serialized Form

Field Summary
protected static int EAST
          references to the worlds edge (a temperary thing)
protected  int heading
          the heading relative to the remembered side.
protected static int highestSerialID
           
protected static java.lang.Object junk
           
protected static double[] knownAngles
          the table of known angles for the number of sides
protected static double[] knownAreas
          the table of known areas for the number of sides
protected static double[] knownMinRadii
          the table of known inner radii for the number of sides
protected static double[] knownRadii
          the table of known outer radii for the number of sides
protected static int MAX_NUM_REC
          the maximum number of sides which the tables apply
protected static int NORTH
          references to the worlds edge (a temperary thing)
protected  double[] one
          the first point of the remembered side.
protected static double RADBREATH
          the spacing between spores and claims!!! so that spores don't bump into their progeny automatically
protected  int serialID
           
protected  int sides
          the number of sides the polygon has
protected static int SOUTH
          references to the worlds edge (a temperary thing)
protected  double[] two
          the second point of the remembered side.
protected  int type
          the type of polygon (right now I think of this as the colour)
protected static int WEST
          references to the worlds edge (a temperary thing)
 
Constructor Summary
RPolygon(int sides)
          simple constructor for a regular polygon.
RPolygon(int sides, double[] one, double[] two, int type)
          constructor for a regular polygon.
 
Method Summary
 double angle()
          compute the angle that two sides meet at.
static double angle(int n)
          compute the angle that two sides meet on a polygon with n sides.
 double area()
          compute the area of the RPolygon
static double area(int n)
          compute the area of the RPolygon with n sides
 java.lang.Object clone()
           
 boolean collision(RPolygon p, double x, double y)
          compute if a collision of RPolygons has occured.
 boolean collisionBoundary(int dir, double cutoff)
          the temporary method is here to impose a boundary.
 void correctHeading()
          make the current side the one in the direction of the heading.
static double distance(double[] p1, double[] p2)
          compute the distance between p1 and p2.
 boolean equals(java.lang.Object obj)
           
static double findAngle(double[] p1, double[] p2)
          compute the angle of p2 around p1.
 void flip()
          flips the polygon over the side that it is heading.
 double[] getCentre()
          compute the centre of the RPolygon
 void getNewSerialID()
          used to add the RPolygon into a hashTable
 double[][] getPoints()
          Compute all the vertices of the RPolygon.
 java.awt.Polygon getPolygon(int w, int h, double sx, double sy)
          a scaled translated drawing of the polygon.
 int getSides()
          accessor to number of sides.
 int getType()
          accessor of type.
 int hashCode()
          used to add the RPolygon into a hashTable
 void left()
          change the heading of the polygon one side to the left
 double minRadius()
          compute the inner radius of this RPolygon
static double minRadius(int n)
          compute the inner radius of the RPolygon with n sides
 double radius()
          compute the outer radius of this RPolygon
static double radius(int n)
          compute the outer radius of the RPolygon with n sides
 void right()
          change the heading of the polygon one side to the right
 void setType(int newType)
          allows for changing types.
 java.lang.String thePoints()
          get a string of all the vertices (for debugging)
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

highestSerialID

protected static int highestSerialID

junk

protected static java.lang.Object junk

serialID

protected int serialID

NORTH

protected static final int NORTH
references to the worlds edge (a temperary thing)

WEST

protected static final int WEST
references to the worlds edge (a temperary thing)

SOUTH

protected static final int SOUTH
references to the worlds edge (a temperary thing)

EAST

protected static final int EAST
references to the worlds edge (a temperary thing)

MAX_NUM_REC

protected static final int MAX_NUM_REC
the maximum number of sides which the tables apply

RADBREATH

protected static final double RADBREATH
the spacing between spores and claims!!! so that spores don't bump into their progeny automatically

knownAreas

protected static double[] knownAreas
the table of known areas for the number of sides

knownAngles

protected static double[] knownAngles
the table of known angles for the number of sides

knownRadii

protected static double[] knownRadii
the table of known outer radii for the number of sides

knownMinRadii

protected static double[] knownMinRadii
the table of known inner radii for the number of sides

type

protected int type
the type of polygon (right now I think of this as the colour)

sides

protected int sides
the number of sides the polygon has

one

protected double[] one
the first point of the remembered side. The remembered side is the only side that is kept to locate the polygon. the order of the one and two are important since it is used to determine where the centre of the polygon is. The heading is relative to this side.

two

protected double[] two
the second point of the remembered side.
See Also:
one

heading

protected int heading
the heading relative to the remembered side. The heading is an integer which refers to the side relate to this one where spawning or flipping will next take place.
See Also:
one
Constructor Detail

RPolygon

public RPolygon(int sides)
simple constructor for a regular polygon.

pre: sides is greater than 1

Parameters:
sides - the number of sides of the new regular polygon

RPolygon

public RPolygon(int sides,
                double[] one,
                double[] two,
                int type)
constructor for a regular polygon.

pre: one is distance 1 away from two

Parameters:
sides - the number of sides of the new regular polygon
one - the first point of a side of the new regular polygon
two - the second point of a side of the new regular polygon
type - the type/team/colour of the new regular polygon
Method Detail

getType

public int getType()
accessor of type.
Returns:
the type or team (colour) of this polygon

setType

public void setType(int newType)
allows for changing types. It is necessary for the nice looking patterns.

getSides

public int getSides()
accessor to number of sides.
Returns:
the number of sides of this polygon

area

public double area()
compute the area of the RPolygon
Returns:
the area of this polygon

area

public static double area(int n)
compute the area of the RPolygon with n sides
Parameters:
n - the number of sides the polygon has
Returns:
the area of a polygon with n sides

radius

public double radius()
compute the outer radius of this RPolygon
Returns:
the outer radius of this polygon

radius

public static double radius(int n)
compute the outer radius of the RPolygon with n sides
Parameters:
n - the number of sides the polygon has
Returns:
the outer radius of a polygon with n sides

minRadius

public double minRadius()
compute the inner radius of this RPolygon
Returns:
the inner radius of this polygon

minRadius

public static double minRadius(int n)
compute the inner radius of the RPolygon with n sides
Parameters:
n - the number of sides the polygon has
Returns:
the inner radius of a polygon with n sides

angle

public double angle()
compute the angle that two sides meet at.
Returns:
the angle that two sides meet at

angle

public static double angle(int n)
compute the angle that two sides meet on a polygon with n sides.
Parameters:
n - the number of sides the polygon has
Returns:
the angle that two sides meet on a polygon with n sides

findAngle

public static double findAngle(double[] p1,
                               double[] p2)
compute the angle of p2 around p1.
Parameters:
p1 - a point
p2 - another point
Returns:
the angle of p2 around p1

distance

public static double distance(double[] p1,
                              double[] p2)
compute the distance between p1 and p2.
Parameters:
p1 - a point
p2 - another point
Returns:
the distance between p1 and p2

getCentre

public double[] getCentre()
compute the centre of the RPolygon
Returns:
a pair of doubles that represent the centre of this polygon

collision

public boolean collision(RPolygon p,
                         double x,
                         double y)
compute if a collision of RPolygons has occured. this is not yet to the point of using the x,y parameters -- the x,y parameters would be a centre point for the "map" used --if I ever get to the point of using a torus for a board....

The code is a little complicated and requires a diagram... I have a hunch that it is efficient but I will need to look into it one day.

Parameters:
p - the polygon to check if it overlaps this one.
x - for future use
y - for future use
Returns:
true if this polygon overlaps p and false otherwise. (within RADBREATH)
See Also:
RADBREATH

collisionBoundary

public boolean collisionBoundary(int dir,
                                 double cutoff)
the temporary method is here to impose a boundary. It will be thrown out as soon as I get the torus calculations running. It checks against the NORTH,EAST,SOUTH, and WEST boundaries.

pre: dir must be NORTH, EAST, SOUTH, or WEST.

Parameters:
dir - is the direction to be checked
cutoff - is the distance range (depends on dir)
Returns:
true if it is beyond the boundary, otherwise false

right

public void right()
change the heading of the polygon one side to the right

left

public void left()
change the heading of the polygon one side to the left

flip

public void flip()
flips the polygon over the side that it is heading. flip achieved by swapping the points one and two after the heading has been considered.
See Also:
correctHeading

correctHeading

public void correctHeading()
make the current side the one in the direction of the heading. Just a matter of getting the points and using the right ones for the new remembered side.

getPolygon

public java.awt.Polygon getPolygon(int w,
                                   int h,
                                   double sx,
                                   double sy)
a scaled translated drawing of the polygon. This is the Polygon from the java.awt.
Parameters:
w - the x coordinate of origin
h - the y coordinate of origin
sx - the scaling factor in the x coordinate
sy - the scaling factor in the y coordinate
See Also:
Polygon

getPoints

public double[][] getPoints()
Compute all the vertices of the RPolygon.
See Also:
correctHeading()

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

toString

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

thePoints

public java.lang.String thePoints()
get a string of all the vertices (for debugging)

hashCode

public int hashCode()
used to add the RPolygon into a hashTable
Overrides:
hashCode in class java.lang.Object

getNewSerialID

public void getNewSerialID()
used to add the RPolygon into a hashTable

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object