org.geotools.resources.geometry
Class XRectangle2D

java.lang.Object
  extended by java.awt.geom.RectangularShape
      extended by java.awt.geom.Rectangle2D
          extended by org.geotools.resources.geometry.XRectangle2D
All Implemented Interfaces:
java.awt.Shape, java.io.Serializable, java.lang.Cloneable

public class XRectangle2D
extends java.awt.geom.Rectangle2D
implements java.io.Serializable

Serializable, high-performance double-precision rectangle. Instead of using x, y, width and height, this class store rectangle's coordinates into the following fields: xmin, xmax, ymin et ymax. Methods likes contains and intersects are faster, which make this class more appropriate for using intensively inside a loop. Furthermore, this class work correctly with infinites and NaN values.

Since:
2.0
Version:
$Id: XRectangle2D.java 30641 2008-06-12 17:42:27Z acuster $
Author:
Martin Desruisseaux (IRD)
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.geom.Rectangle2D
java.awt.geom.Rectangle2D.Double, java.awt.geom.Rectangle2D.Float
 
Field Summary
static java.awt.geom.Rectangle2D INFINITY
          An immutable instance of a Rectangle2D with bounds extending toward infinities.
protected  double xmax
          Maximal x coordinate.
protected  double xmin
          Minimal x coordinate.
protected  double ymax
          Maximal y coordinate.
protected  double ymin
          Minimal y coordinate.
 
Fields inherited from class java.awt.geom.Rectangle2D
OUT_BOTTOM, OUT_LEFT, OUT_RIGHT, OUT_TOP
 
Constructor Summary
XRectangle2D()
          Construct a default rectangle.
XRectangle2D(double x, double y, double width, double height)
          Construct a rectangle with the specified location and dimension.
XRectangle2D(java.awt.geom.Rectangle2D rect)
          Construct a rectangle with the same coordinates than the supplied rectangle.
 
Method Summary
 void add(double x, double y)
          Adds a point, specified by the double precision arguments x and y, to this Rectangle2D.
 void add(java.awt.geom.Rectangle2D rect)
          Adds a Rectangle2D object to this Rectangle2D.
 boolean contains(double x, double y)
          Tests if a specified coordinate is inside the boundary of this Rectangle2D.
 boolean contains(double x, double y, double width, double height)
          Tests if the interior of this Rectangle2D entirely contains the specified set of rectangular coordinates.
 boolean contains(java.awt.geom.Rectangle2D rect)
          Tests if the interior of this shape entirely contains the specified rectangle.
static boolean containsInclusive(java.awt.geom.Rectangle2D outter, java.awt.geom.Rectangle2D inner)
          Tests if the interior of the inner rectangle is contained in the interior and/or the edge of the outter rectangle.
static XRectangle2D createFromExtremums(double xmin, double ymin, double xmax, double ymax)
          Create a rectangle using maximal x and y values rather than width and height.
 java.awt.geom.Rectangle2D createIntersection(java.awt.geom.Rectangle2D rect)
          Returns a new Rectangle2D object representing the intersection of this Rectangle2D with the specified Rectangle2D.
 java.awt.geom.Rectangle2D createUnion(java.awt.geom.Rectangle2D rect)
          Returns a new Rectangle2D object representing the union of this Rectangle2D with the specified Rectangle2D.
static boolean equalsEpsilon(java.awt.geom.Rectangle2D rect1, java.awt.geom.Rectangle2D rect2)
          Returns true if the two rectangles are equals up to an epsilon value.
 double getCenterX()
          Returns the X coordinate of the center of the rectangle.
 double getCenterY()
          Returns the Y coordinate of the center of the rectangle.
 double getHeight()
          Returns the height of the framing rectangle in double precision.
 double getMaxX()
          Returns the largest X coordinate of the rectangle.
 double getMaxY()
          Returns the largest Y coordinate of the rectangle.
 double getMinX()
          Returns the smallest X coordinate of the rectangle.
 double getMinY()
          Returns the smallest Y coordinate of the rectangle.
 double getWidth()
          Returns the width of the framing rectangle in double precision.
 double getX()
          Returns the X coordinate of the upper left corner of the framing rectangle in double precision.
 double getY()
          Returns the Y coordinate of the upper left corner of the framing rectangle in double precision.
static boolean intersectInclusive(java.awt.geom.Rectangle2D rect1, java.awt.geom.Rectangle2D rect2)
          Tests if the interior and/or the edge of two rectangles intersect.
static boolean intersectInclusive(java.awt.Shape shape, java.awt.geom.Rectangle2D rect)
          Tests if the interior of the Shape intersects the interior of a specified rectangle.
 boolean intersects(double x, double y, double width, double height)
          Tests if the interior of this Rectangle2D intersects the interior of a specified set of rectangular coordinates.
 boolean intersects(java.awt.geom.Rectangle2D rect)
          Tests if the interior of this shape intersects the interior of a specified rectangle.
 boolean isEmpty()
          Determines whether the RectangularShape is empty.
 int outcode(double x, double y)
          Determines where the specified coordinates lie with respect to this Rectangle2D.
 void setRect(double x, double y, double width, double height)
          Sets the location and size of this Rectangle2D to the specified double values.
 void setRect(java.awt.geom.Rectangle2D r)
          Sets this Rectangle2D to be the same as the specified Rectangle2D.
 java.lang.String toString()
          Returns the String representation of this Rectangle2D.
 
Methods inherited from class java.awt.geom.Rectangle2D
add, equals, getBounds2D, getPathIterator, getPathIterator, hashCode, intersect, intersectsLine, intersectsLine, outcode, setFrame, union
 
Methods inherited from class java.awt.geom.RectangularShape
clone, contains, getBounds, getFrame, setFrame, setFrame, setFrameFromCenter, setFrameFromCenter, setFrameFromDiagonal, setFrameFromDiagonal
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

INFINITY

public static final java.awt.geom.Rectangle2D INFINITY
An immutable instance of a Rectangle2D with bounds extending toward infinities. The getMinX() and getMinY() methods return always Double.NEGATIVE_INFINITY, while the getMaxX() and getMaxY() methods return always Double.POSITIVE_INFINITY. This rectangle can be used as argument in the XRectangle2D constructor for initializing a new XRectangle2D to infinite bounds.


xmin

protected double xmin
Minimal x coordinate.


ymin

protected double ymin
Minimal y coordinate.


xmax

protected double xmax
Maximal x coordinate.


ymax

protected double ymax
Maximal y coordinate.

Constructor Detail

XRectangle2D

public XRectangle2D()
Construct a default rectangle. Initial coordinates are (0,0,0,0).


XRectangle2D

public XRectangle2D(double x,
                    double y,
                    double width,
                    double height)
Construct a rectangle with the specified location and dimension. This constructor uses the same signature than Rectangle2D for consistency.


XRectangle2D

public XRectangle2D(java.awt.geom.Rectangle2D rect)
Construct a rectangle with the same coordinates than the supplied rectangle.

Parameters:
rect - The rectangle, or null in none (in which case this constructor is equivalents to the no-argument constructor). Use INFINITY for initializing this XRectangle2D with infinite bounds.
Method Detail

createFromExtremums

public static XRectangle2D createFromExtremums(double xmin,
                                               double ymin,
                                               double xmax,
                                               double ymax)
Create a rectangle using maximal x and y values rather than width and height. This factory avoid the problem of NaN values when extremums are infinite numbers.


isEmpty

public boolean isEmpty()
Determines whether the RectangularShape is empty. When the RectangularShape is empty, it encloses no area.

Specified by:
isEmpty in class java.awt.geom.RectangularShape
Returns:
true if the RectangularShape is empty; false otherwise.

getX

public double getX()
Returns the X coordinate of the upper left corner of the framing rectangle in double precision.

Specified by:
getX in class java.awt.geom.RectangularShape
Returns:
the x coordinate of the upper left corner of the framing rectangle.

getY

public double getY()
Returns the Y coordinate of the upper left corner of the framing rectangle in double precision.

Specified by:
getY in class java.awt.geom.RectangularShape
Returns:
the y coordinate of the upper left corner of the framing rectangle.

getWidth

public double getWidth()
Returns the width of the framing rectangle in double precision.

Specified by:
getWidth in class java.awt.geom.RectangularShape
Returns:
the width of the framing rectangle.

getHeight

public double getHeight()
Returns the height of the framing rectangle in double precision.

Specified by:
getHeight in class java.awt.geom.RectangularShape
Returns:
the height of the framing rectangle.

getMinX

public double getMinX()
Returns the smallest X coordinate of the rectangle.

Overrides:
getMinX in class java.awt.geom.RectangularShape

getMinY

public double getMinY()
Returns the smallest Y coordinate of the rectangle.

Overrides:
getMinY in class java.awt.geom.RectangularShape

getMaxX

public double getMaxX()
Returns the largest X coordinate of the rectangle.

Overrides:
getMaxX in class java.awt.geom.RectangularShape

getMaxY

public double getMaxY()
Returns the largest Y coordinate of the rectangle.

Overrides:
getMaxY in class java.awt.geom.RectangularShape

getCenterX

public double getCenterX()
Returns the X coordinate of the center of the rectangle.

Overrides:
getCenterX in class java.awt.geom.RectangularShape

getCenterY

public double getCenterY()
Returns the Y coordinate of the center of the rectangle.

Overrides:
getCenterY in class java.awt.geom.RectangularShape

setRect

public void setRect(double x,
                    double y,
                    double width,
                    double height)
Sets the location and size of this Rectangle2D to the specified double values.

Specified by:
setRect in class java.awt.geom.Rectangle2D
Parameters:
x - the x coordinates to which to set the location of the upper left corner of this Rectangle2D
y - the y coordinates to which to set the location of the upper left corner of this Rectangle2D
width - the value to use to set the width of this Rectangle2D
height - the value to use to set the height of this Rectangle2D

setRect

public void setRect(java.awt.geom.Rectangle2D r)
Sets this Rectangle2D to be the same as the specified Rectangle2D.

Overrides:
setRect in class java.awt.geom.Rectangle2D
Parameters:
r - the specified Rectangle2D

intersects

public boolean intersects(double x,
                          double y,
                          double width,
                          double height)
Tests if the interior of this Rectangle2D intersects the interior of a specified set of rectangular coordinates.

Specified by:
intersects in interface java.awt.Shape
Overrides:
intersects in class java.awt.geom.Rectangle2D
Parameters:
x - the x coordinates of the upper left corner of the specified set of rectangular coordinates
y - the y coordinates of the upper left corner of the specified set of rectangular coordinates
width - the width of the specified set of rectangular coordinates
height - the height of the specified set of rectangular coordinates
Returns:
true if this Rectangle2D intersects the interior of a specified set of rectangular coordinates; false otherwise.

intersects

public boolean intersects(java.awt.geom.Rectangle2D rect)
Tests if the interior of this shape intersects the interior of a specified rectangle. This methods overrides the default Rectangle2D implementation in order to work correctly with infinites and NaN values.

Specified by:
intersects in interface java.awt.Shape
Overrides:
intersects in class java.awt.geom.RectangularShape
Parameters:
rect - the specified rectangle.
Returns:
true if this shape and the specified rectangle intersect each other.
See Also:
intersectInclusive(Rectangle2D, Rectangle2D)

intersectInclusive

public static boolean intersectInclusive(java.awt.geom.Rectangle2D rect1,
                                         java.awt.geom.Rectangle2D rect2)
Tests if the interior and/or the edge of two rectangles intersect. This method is similar to intersects(Rectangle2D) except for the following points: This method is said inclusive because it tests bounds as closed interval rather then open interval (the default Java2D behavior). Usage of closed interval is required if at least one rectangle may be the bounding box of a perfectly horizontal or vertical line; such a bounding box has 0 width or height.

Parameters:
rect1 - The first rectangle to test.
rect2 - The second rectangle to test.
Returns:
true if the interior and/or the edge of the two specified rectangles intersects.

intersectInclusive

public static boolean intersectInclusive(java.awt.Shape shape,
                                         java.awt.geom.Rectangle2D rect)
Tests if the interior of the Shape intersects the interior of a specified rectangle. This method might conservatively return true when there is a high probability that the rectangle and the shape intersect, but the calculations to accurately determine this intersection are prohibitively expensive. This is similar to Shape.intersects(Rectangle2D), except that this method tests also rectangle with zero width or height (which are empty according Shape contract). However, rectangle with negative width or height are still considered as empty.

This method is said inclusive because it try to mimic intersectInclusive(Rectangle2D, Rectangle2D) behavior, at least for rectangle with zero width or height.

Parameters:
shape - The shape.
rect - The rectangle to test for inclusion.
Returns:
true if the interior of the shape and the interior of the specified rectangle intersect, or are both highly likely to intersect.

equalsEpsilon

public static boolean equalsEpsilon(java.awt.geom.Rectangle2D rect1,
                                    java.awt.geom.Rectangle2D rect2)
Returns true if the two rectangles are equals up to an epsilon value.


contains

public boolean contains(double x,
                        double y,
                        double width,
                        double height)
Tests if the interior of this Rectangle2D entirely contains the specified set of rectangular coordinates.

Specified by:
contains in interface java.awt.Shape
Overrides:
contains in class java.awt.geom.Rectangle2D
Parameters:
x - the x coordinates of the upper left corner of the specified set of rectangular coordinates
y - the y coordinates of the upper left corner of the specified set of rectangular coordinates
width - the width of the specified set of rectangular coordinates
height - the height of the specified set of rectangular coordinates
Returns:
true if this Rectangle2D entirely contains specified set of rectangular coordinates; false otherwise.

contains

public boolean contains(java.awt.geom.Rectangle2D rect)
Tests if the interior of this shape entirely contains the specified rectangle. This methods overrides the default Rectangle2D implementation in order to work correctly with infinites and NaN values.

Specified by:
contains in interface java.awt.Shape
Overrides:
contains in class java.awt.geom.RectangularShape
Parameters:
rect - the specified rectangle.
Returns:
true if this shape entirely contains the specified rectangle.

contains

public boolean contains(double x,
                        double y)
Tests if a specified coordinate is inside the boundary of this Rectangle2D.

Specified by:
contains in interface java.awt.Shape
Overrides:
contains in class java.awt.geom.Rectangle2D
Parameters:
x - the x coordinates to test.
y - the y coordinates to test.
Returns:
true if the specified coordinates are inside the boundary of this Rectangle2D; false otherwise.

containsInclusive

public static boolean containsInclusive(java.awt.geom.Rectangle2D outter,
                                        java.awt.geom.Rectangle2D inner)
Tests if the interior of the inner rectangle is contained in the interior and/or the edge of the outter rectangle. This method is similar to contains(Rectangle2D) except for the following points: This method is said inclusive because it tests bounds as closed interval rather then open interval (the default Java2D behavior). Usage of closed interval is required if at least one rectangle may be the bounding box of a perfectly horizontal or vertical line; such a bounding box has 0 width or height.

Parameters:
outter - The first rectangle to test.
inner - The second rectangle to test.
Returns:
true if the interior of inner is inside the interior and/or the edge of outter.

outcode

public int outcode(double x,
                   double y)
Determines where the specified coordinates lie with respect to this Rectangle2D. This method computes a binary OR of the appropriate mask values indicating, for each side of this Rectangle2D, whether or not the specified coordinates are on the same side of the edge as the rest of this Rectangle2D.

Specified by:
outcode in class java.awt.geom.Rectangle2D
Returns:
the logical OR of all appropriate out codes.
See Also:
Rectangle2D.OUT_LEFT, Rectangle2D.OUT_TOP, Rectangle2D.OUT_RIGHT, Rectangle2D.OUT_BOTTOM

createIntersection

public java.awt.geom.Rectangle2D createIntersection(java.awt.geom.Rectangle2D rect)
Returns a new Rectangle2D object representing the intersection of this Rectangle2D with the specified Rectangle2D.

Specified by:
createIntersection in class java.awt.geom.Rectangle2D
Parameters:
rect - the Rectangle2D to be intersected with this Rectangle2D
Returns:
the largest Rectangle2D contained in both the specified Rectangle2D and in this Rectangle2D.

createUnion

public java.awt.geom.Rectangle2D createUnion(java.awt.geom.Rectangle2D rect)
Returns a new Rectangle2D object representing the union of this Rectangle2D with the specified Rectangle2D.

Specified by:
createUnion in class java.awt.geom.Rectangle2D
Parameters:
rect - the Rectangle2D to be combined with this Rectangle2D
Returns:
the smallest Rectangle2D containing both the specified Rectangle2D and this Rectangle2D.

add

public void add(double x,
                double y)
Adds a point, specified by the double precision arguments x and y, to this Rectangle2D. The resulting Rectangle2D is the smallest Rectangle2D that contains both the original Rectangle2D and the specified point.

After adding a point, a call to contains with the added point as an argument does not necessarily return true. The contains method does not return true for points on the right or bottom edges of a rectangle. Therefore, if the added point falls on the left or bottom edge of the enlarged rectangle, contains returns false for that point.

Overrides:
add in class java.awt.geom.Rectangle2D

add

public void add(java.awt.geom.Rectangle2D rect)
Adds a Rectangle2D object to this Rectangle2D. The resulting Rectangle2D is the union of the two Rectangle2D objects.

Overrides:
add in class java.awt.geom.Rectangle2D
Parameters:
rect - the Rectangle2D to add to this Rectangle2D.

toString

public java.lang.String toString()
Returns the String representation of this Rectangle2D.

Overrides:
toString in class java.lang.Object
Returns:
a String representing this Rectangle2D.


Copyright © 1996-2010 Geotools. All Rights Reserved.