us.zuercher.gpx2map.util
Class Rectangle

java.lang.Object
  extended by us.zuercher.gpx2map.util.Rectangle

public class Rectangle
extends Object

Rectangle represents a set of four Points that describe the four corners of a rectangle.

Author:
Stephan Zuercher

Field Summary
private  Point lowerLeft
          Lower left Point.
private  Point lowerRight
          Lower right Point.
private  Point upperLeft
          Upper left Point.
private  Point upperRight
          Upper right Point.
 
Constructor Summary
Rectangle(int x1, int y1, int x2, int y2)
          Constructs a new Rectangle from upper left and lower right coordinates.
Rectangle(Point upperLeft, Point lowerRight)
          Constructs a new Rectangle from upper left and lower right coordinates.
 
Method Summary
 int calculateArea()
          Calculates the area of this rectangle in pixels.
 double calculateOverlap(Rectangle other)
          Calculates the percentage overlap of this rectangle and another rectangle.
 boolean equals(Object other)
          Tests whether this rectangle is identical to another.
 boolean equals(Rectangle other)
          Tests whether this rectangle is identical to another.
 int getHeight()
          Returns the rectangle's height.
 Point getLowerLeftPoint()
          Returns the lower left corner, as a Point.
 Point getLowerRightPoint()
          Returns the lower right corner, as a Point.
 Point getUpperLeftPoint()
          Returns the upper left corner, as a Point.
 Point getUpperRightPoint()
          Returns the upper right corner, as a Point.
 int getWidth()
          Returns the rectangle's width.
 boolean overlaps(Rectangle other)
          Tests whether this rectangle overlaps another rectangle.
 boolean within(Dimension bounds)
          Tests whether this rectangle is entirely contained within the given Dimension.
 boolean within(Rectangle bounds)
          Tests whether this rectangle is entire contained within the given Rectangle
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

upperLeft

private final Point upperLeft
Upper left Point.


upperRight

private final Point upperRight
Upper right Point.


lowerRight

private final Point lowerRight
Lower right Point.


lowerLeft

private final Point lowerLeft
Lower left Point.

Constructor Detail

Rectangle

public Rectangle(int x1,
                 int y1,
                 int x2,
                 int y2)
Constructs a new Rectangle from upper left and lower right coordinates.

Parameters:
x1 - upper left X coordinate
y1 - upper left Y coordinate
x2 - lower right X coordinate
y2 - lower right Y coordinate

Rectangle

public Rectangle(Point upperLeft,
                 Point lowerRight)
Constructs a new Rectangle from upper left and lower right coordinates.

Parameters:
upperLeft - upper left coordinate
lowerRight - lower right coordinate
Method Detail

getUpperLeftPoint

public Point getUpperLeftPoint()
Returns the upper left corner, as a Point.

Returns:
the upper left corner, as a Point.

getUpperRightPoint

public Point getUpperRightPoint()
Returns the upper right corner, as a Point.

Returns:
the upper right corner, as a Point.

getLowerRightPoint

public Point getLowerRightPoint()
Returns the lower right corner, as a Point.

Returns:
the lower right corner, as a Point.

getLowerLeftPoint

public Point getLowerLeftPoint()
Returns the lower left corner, as a Point.

Returns:
the lower left corner, as a Point.

getWidth

public int getWidth()
Returns the rectangle's width.

Returns:
the rectangle's width.

getHeight

public int getHeight()
Returns the rectangle's height.

Returns:
the rectangle's height.

overlaps

public boolean overlaps(Rectangle other)
Tests whether this rectangle overlaps another rectangle.

Parameters:
other - another Rectangle
Returns:
true if the Rectangles overlap at any point.

calculateArea

public int calculateArea()
Calculates the area of this rectangle in pixels.

Returns:
the area of this rectangle in pixels.

calculateOverlap

public double calculateOverlap(Rectangle other)
Calculates the percentage overlap of this rectangle and another rectangle. The returned value is the percentage overlap of the smaller rectangle to the larger rectangle, irrespective of whether this or other is larger.

Parameters:
other - another Rectangle
Returns:
a value in the range [0.0, 1.0] where 0.0 indicates the rectangles do not overlap at all, and 1.0 indicates the rectangles have same the area and location.

within

public boolean within(Dimension bounds)
Tests whether this rectangle is entirely contained within the given Dimension.

Parameters:
bounds - the dimension to test against
Returns:
true if all four corners of the rectangle are within the given bounds.

within

public boolean within(Rectangle bounds)
Tests whether this rectangle is entire contained within the given Rectangle

Parameters:
bounds - the rectangle to test against
Returns:
true if all four corners of the rectangle are within the given bounds

equals

public boolean equals(Rectangle other)
Tests whether this rectangle is identical to another.

Parameters:
other - the other Rectangle
Returns:
true if both rectangles have the same size and position.

equals

public boolean equals(Object other)
Tests whether this rectangle is identical to another.

Overrides:
equals in class Object
Parameters:
other - the other Rectangle
Returns:
true if both rectangles have the same size and position.
Throws:
ClassCastException - if other is not a Rectangle.