ch.aplu.jgamegrid
Class GGRectangle

java.lang.Object
  extended by ch.aplu.jgamegrid.GGRectangle

public class GGRectangle
extends java.lang.Object

Class representing a rotatable rectangle. The following terms for a rectangle with direction = 0 are used:

(ulx, uly)            (urx, ury)
vertex[0]             vertex[1]
o--------------------->o    ==========>> direction    ---------->x
^      edges[0]        |                              |
|                      |                              |
|  e                e  |                              |
|  d                d  |                              v
|  g                g  |                              y
|  e                e  | height
|  s                s  |
| [3}              [1] |
|                      |
|      edges[2]        v
o<---------------------o
vertex[3]   width     vertex[2]
(llx, lly)            (lrx, lry)
 
The direction of the rectangle is defined as the angle (0..2*pi) of the vector direction of edges[0] clockwise with reference to the x-coordinate direction. When defining the rectangle instance by the 4 vertexes, it is assumed that the shape is a rectangle.


Constructor Summary
GGRectangle(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3)
          Creates a new GGRectangle from given 8 x-y-coordinates.
GGRectangle(GGRectangle rect)
          Creates a new GGRectangle from given GGRectangle.
GGRectangle(GGVector[] vertexes)
          Creates a new GGRectangle from given vertexes.
GGRectangle(GGVector center, double direction, double width, double height)
          Creates a new GGRectangle from given center, direction, width and height.
GGRectangle(java.awt.geom.Point2D.Double pt0, java.awt.geom.Point2D.Double pt1, java.awt.geom.Point2D.Double pt2, java.awt.geom.Point2D.Double pt3)
          Creates a new GGRectangle from given 4 vertex points.
GGRectangle(java.awt.Rectangle rect)
          Creates a new GGRectangle from given java.awt.Rectangle.
 
Method Summary
 GGRectangle clone()
          Returns a new rectangle with same vertices and egdes as the original.
 GGVector getCenter()
          Returns a vector that points to the center of the rectangle.
 double getCircumradius()
          Returns the circumradius of the rectangle.
 double getDirection()
          Direction of edges[0], zero to west, clockwise 0..2*pi
 GGVector[] getEdges()
          Returns a GGVector array with 4 GGVectors whoses values are copies of the original edges.
 double getHeight()
          Returns the height of the rectangle.
 GGVector[] getVertexes()
          Returns a GGVector array with 4 GGVectors whoses values are copies of the original vertexes.
 double getWidth()
          Returns the width of the rectangle.
 boolean isEqual(GGRectangle rect)
          Returns true, if the current rectangle is identical to the given rectangle.
 boolean isIntersecting(GGCircle circle)
          Returns true if the current rectangle intersects with the given circle.
 boolean isIntersecting(GGLine line)
          Returns true if the current rectangle intersects with the given line segment.
 boolean isIntersecting(GGRectangle rect)
          Returns true if the current rectangle intersects with the given rectangle.
 boolean isIntersecting(GGVector vector, boolean isRotatable)
          Returns true if the given points is part of the rectangle area.
 void rotate(double angle)
          Rotates with rotation center at (0, 0).
 void rotate(GGVector rotationCenter, double angle)
          Rotates with given rotation center.
 java.lang.String toString()
          Returns a string that enumerates vertexes, edges, center, width, height and direction.
 void translate(GGVector v)
          Translates by the given vector.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GGRectangle

public GGRectangle(GGRectangle rect)
Creates a new GGRectangle from given GGRectangle.

Parameters:
rect - the rectangle from where the values of vertexes and edges are copied

GGRectangle

public GGRectangle(GGVector[] vertexes)
Creates a new GGRectangle from given vertexes. The edges are recalculated.

Parameters:
vertexes - the 4 vertexes from where the values of new vertexes are copied

GGRectangle

public GGRectangle(double x0,
                   double y0,
                   double x1,
                   double y1,
                   double x2,
                   double y2,
                   double x3,
                   double y3)
Creates a new GGRectangle from given 8 x-y-coordinates. The edges are calculated.

Parameters:
x0 - coordinate of lower left x (llx)
y0 - coordinate of lower left y (lly)
x1 - coordinate of lower right x (lrx)
y1 - coordinate of lower right y (lry)
x2 - coordinate of upper right x (urx)
y2 - coordinate of upper right y (ury)
x3 - coordinate of upper left x (ulx)
y3 - coordinate of upper left y (uly)

GGRectangle

public GGRectangle(java.awt.geom.Point2D.Double pt0,
                   java.awt.geom.Point2D.Double pt1,
                   java.awt.geom.Point2D.Double pt2,
                   java.awt.geom.Point2D.Double pt3)
Creates a new GGRectangle from given 4 vertex points. The edges are calculated.

Parameters:
pt0 - coordinates of lower left vertex
pt1 - coordinates of lower right vertex
pt2 - coordinates of upper right vertex
pt3 - coordinates of upper right vertex

GGRectangle

public GGRectangle(GGVector center,
                   double direction,
                   double width,
                   double height)
Creates a new GGRectangle from given center, direction, width and height. The edges are calculated.

Parameters:
center - vector to the center of the rectangle
direction - direction of edges[0] (in arc, 0..2*pi, positive clockwise)
width - the length of edges[0] and edges[2]
height - the length of edges[1] and edges[3]

GGRectangle

public GGRectangle(java.awt.Rectangle rect)
Creates a new GGRectangle from given java.awt.Rectangle.

Parameters:
rect - the rectangle from where the values of vertexes are taken
Method Detail

rotate

public void rotate(double angle)
Rotates with rotation center at (0, 0).

Parameters:
angle -

rotate

public void rotate(GGVector rotationCenter,
                   double angle)
Rotates with given rotation center.

Parameters:
rotationCenter - the center of the rotation
angle - the angle (in radian, clockwise)

translate

public void translate(GGVector v)
Translates by the given vector.

Parameters:
v - the translatin vector

getVertexes

public GGVector[] getVertexes()
Returns a GGVector array with 4 GGVectors whoses values are copies of the original vertexes.

Returns:
a GGVector array of size 4

getEdges

public GGVector[] getEdges()
Returns a GGVector array with 4 GGVectors whoses values are copies of the original edges.

Returns:
a GGVector array of size 4

getCenter

public GGVector getCenter()
Returns a vector that points to the center of the rectangle.

Returns:
a GGVector that points to the center

getWidth

public double getWidth()
Returns the width of the rectangle.

Returns:
the width (length of edges[0])

getHeight

public double getHeight()
Returns the height of the rectangle.

Returns:
the height (length of edges[1])

getDirection

public double getDirection()
Direction of edges[0], zero to west, clockwise 0..2*pi

Returns:
the direction (arc 0.. 2*pi)

getCircumradius

public double getCircumradius()
Returns the circumradius of the rectangle.

Returns:
the circumradius, e.g. the radius of the circle through the vertexes

clone

public GGRectangle clone()
Returns a new rectangle with same vertices and egdes as the original.

Overrides:
clone in class java.lang.Object
Returns:
a clone of the original rectangle

toString

public java.lang.String toString()
Returns a string that enumerates vertexes, edges, center, width, height and direction.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the rectangle

isIntersecting

public boolean isIntersecting(GGRectangle rect)
Returns true if the current rectangle intersects with the given rectangle.

Parameters:
rect - the rectangle to be checked for intersection with the current rectangle
Returns:
true, if the two rectangles intersects; otherwise false

isIntersecting

public boolean isIntersecting(GGLine line)
Returns true if the current rectangle intersects with the given line segment.

Parameters:
line - the line segment to be checked for intersection with the current rectangle
Returns:
true, if the line and the rectangle intersects; otherwise false

isIntersecting

public boolean isIntersecting(GGCircle circle)
Returns true if the current rectangle intersects with the given circle.

Parameters:
circle - the circle to be checked for intersection with the current rectangle
Returns:
true, if the two figures intersects; otherwise false

isIntersecting

public boolean isIntersecting(GGVector vector,
                              boolean isRotatable)
Returns true if the given points is part of the rectangle area.

Parameters:
vector - the vector to be checked for intersection
isRotatable - if false, the axis-parallel rectangle is assumed; if true, the rectangle may be rotated
Returns:
true, if the vector (end point) lays inside or on the border of the rectangle; otherwise false

isEqual

public boolean isEqual(GGRectangle rect)
Returns true, if the current rectangle is identical to the given rectangle.

Parameters:
rect - the rectangle to compare
Returns:
true, if both rectangles are identical