org.geotoolkit.display.shape
Class XRectangle2D

Object
  extended by RectangularShape
      extended by Rectangle2D
          extended by XRectangle2D
All Implemented Interfaces:
Shape, Serializable, Cloneable

public class XRectangle2D
extends Rectangle2D
implements Serializable

Serializable rectangle capable to handle infinite values. Instead of using x, y, width and height fields, this class uses xmin, xmax, ymin and ymax fields. This choice provides two benefits:

Since:
1.2
Version:
3.10
Author:
Martin Desruisseaux (IRD, Geomatys)
See Also:
Serialized Form
Module:
referencing/geotk-referencing (download)    View source code for this class

Nested Class Summary
 
Nested classes/interfaces inherited from class Rectangle2D
Rectangle2D.Double, Rectangle2D.Float
 
Field Summary
static Rectangle2D INFINITY
          An immutable instance of a Rectangle2D with bounds extending toward infinities.
protected  double xmax
          Maximal x ordinate value.
protected  double xmin
          Minimal x ordinate value.
protected  double ymax
          Maximal y ordinate value.
protected  double ymin
          Minimal y ordinate value.
 
Fields inherited from class Rectangle2D
OUT_BOTTOM, OUT_LEFT, OUT_RIGHT, OUT_TOP
 
Constructor Summary
XRectangle2D()
          Constructs a default rectangle initialized to (0,0,0,0).
XRectangle2D(double x, double y, double width, double height)
          Construct a rectangle with the specified location and dimension.
XRectangle2D(Rectangle2D rect)
          Constructs a rectangle with the same coordinates than the supplied rectangle.
 
Method Summary
 void add(double x, double y)
          Adds a point, specified by the arguments x and y, to this rectangle.
 void add(Rectangle2D rect)
          Adds a Rectangle2D object to this rectangle.
 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 rectangle entirely contains the specified set of rectangular coordinates.
 boolean contains(Rectangle2D rect)
          Tests if the interior of this shape entirely contains the specified rectangle.
static boolean containsInclusive(Rectangle2D outter, 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)
          Creates a rectangle using maximal x and y values rather than width and height.
 Rectangle2D createIntersection(Rectangle2D rect)
          Returns a new Rectangle2D object representing the intersection of this rectangle with the specified one.
 Rectangle2D createUnion(Rectangle2D rect)
          Returns a new Rectangle2D object representing the union of this rectangle with the specified one.
static boolean equalsEpsilon(Rectangle2D rect1, Rectangle2D rect2)
          Deprecated. Use your own comparison method instead, so you can specify different tolerance threshold along different axis.
 double getCenterX()
          Returns the x ordinate of the center of the rectangle.
 double getCenterY()
          Returns the y ordinate of the center of the rectangle.
 double getHeight()
          Returns the height of the rectangle.
 double getMaxX()
          Returns the maximal x ordinate value.
 double getMaxY()
          Returns the maximal y ordinate value.
 double getMinX()
          Returns the minimal x ordinate value.
 double getMinY()
          Returns the minimal y ordinate value.
 double getWidth()
          Returns the width of the rectangle.
 double getX()
          Returns the minimal x ordinate value.
 double getY()
          Returns the minimal y ordinate value.
 void intersect(Rectangle2D rect)
          Intersects a Rectangle2D object with this rectangle.
static boolean intersectInclusive(Rectangle2D rect1, Rectangle2D rect2)
          Tests if the interior and/or the edge of two rectangles intersect.
static boolean intersectInclusive(Shape shape, 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 rectangle intersects the interior of a specified set of rectangular coordinates.
 boolean intersects(Rectangle2D rect)
          Tests if the interior of this shape intersects the interior of a specified rectangle.
 boolean isEmpty()
          Determines whether this rectangle is empty.
 int outcode(double x, double y)
          Determines where the specified coordinates lie with respect to this rectangle.
 void setFrame(Rectangle2D r)
          Sets the framing rectangle to the given rectangle.
 void setRect(double x, double y, double width, double height)
          Sets the location and size of this rectangle to the specified values.
 void setRect(Rectangle2D r)
          Sets this rectangle to be the same as the specified rectangle.
 String toString()
          Returns the String representation of this Rectangle2D.
 
Methods inherited from class Rectangle2D
add, equals, getBounds2D, getPathIterator, getPathIterator, hashCode, intersect, intersectsLine, intersectsLine, outcode, setFrame, union
 
Methods inherited from class RectangularShape
clone, contains, getBounds, getFrame, setFrame, setFrameFromCenter, setFrameFromCenter, setFrameFromDiagonal, setFrameFromDiagonal
 
Methods inherited from class Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

INFINITY

public static final Rectangle2D INFINITY
An immutable instance of a Rectangle2D with bounds extending toward infinities. The getMinX() and getMinY() methods return always negative infinity while the getMaxX() and getMaxY() methods return always positive infinity.

This rectangle can be given as argument to the constructor for initializing a new XRectangle2D to infinite bounds.


xmin

protected double xmin
Minimal x ordinate value.


ymin

protected double ymin
Minimal y ordinate value.


xmax

protected double xmax
Maximal x ordinate value.


ymax

protected double ymax
Maximal y ordinate value.

Constructor Detail

XRectangle2D

public XRectangle2D()
Constructs a default rectangle initialized to (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 with Java2D practice.

Parameters:
x - Minimal x ordinate value.
y - Minimal y ordinate value.
width - The rectangle width.
height - The rectangle height.

XRectangle2D

public XRectangle2D(Rectangle2D rect)
Constructs 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)
Creates a rectangle using maximal x and y values rather than width and height. This factory avoid the problem of NaN values when extremum are infinite numbers.

Parameters:
xmin - Minimal x ordinate value.
ymin - Minimal y ordinate value.
xmax - Maximal x ordinate value.
ymax - Maximal y ordinate value.
Returns:
A new rectangle initialized to the given bounds.

isEmpty

public boolean isEmpty()
Determines whether this rectangle is empty. If this rectangle has at least one NaN value, then it is considered empty.

Specified by:
isEmpty in class RectangularShape
Returns:
true if this rectangle is empty; false otherwise.

getX

public double getX()
Returns the minimal x ordinate value.

Specified by:
getX in class RectangularShape
Returns:
The minimal x ordinate value.

getY

public double getY()
Returns the minimal y ordinate value.

Specified by:
getY in class RectangularShape
Returns:
The minimal y ordinate value.

getWidth

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

Specified by:
getWidth in class RectangularShape
Returns:
The width of the rectangle.

getHeight

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

Specified by:
getHeight in class RectangularShape
Returns:
The height of the rectangle.

getMinX

public double getMinX()
Returns the minimal x ordinate value.

Overrides:
getMinX in class RectangularShape

getMinY

public double getMinY()
Returns the minimal y ordinate value.

Overrides:
getMinY in class RectangularShape

getMaxX

public double getMaxX()
Returns the maximal x ordinate value.

Overrides:
getMaxX in class RectangularShape

getMaxY

public double getMaxY()
Returns the maximal y ordinate value.

Overrides:
getMaxY in class RectangularShape

getCenterX

public double getCenterX()
Returns the x ordinate of the center of the rectangle.

Overrides:
getCenterX in class RectangularShape

getCenterY

public double getCenterY()
Returns the y ordinate of the center of the rectangle.

Overrides:
getCenterY in class RectangularShape

setRect

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

Specified by:
setRect in class Rectangle2D
Parameters:
x - The x minimal ordinate value.
y - The y minimal ordinate value.
width - The rectangle width.
height - The rectangle height.

setRect

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

Overrides:
setRect in class Rectangle2D
Parameters:
r - The rectangle to copy values from.

setFrame

public void setFrame(Rectangle2D r)
Sets the framing rectangle to the given rectangle. The default implementation delegates to setRect(Rectangle2D). This is consistent with the default implementation of Rectangle2D.setFrame(double, double, double, double), which delegates to the corresponding method of setRect.

Overrides:
setFrame in class RectangularShape

intersects

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

Specified by:
intersects in interface Shape
Overrides:
intersects in class Rectangle2D
Parameters:
x - The x minimal ordinate value.
y - The y minimal ordinate value.
width - The rectangle width.
height - The rectangle height.
Returns:
true if this rectangle intersects the interior of the specified set of rectangular coordinates; false otherwise.

intersects

public boolean intersects(Rectangle2D rect)
Tests if the interior of this shape intersects the interior of a specified rectangle.

Specified by:
intersects in interface Shape
Overrides:
intersects in class 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(Rectangle2D rect1,
                                         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(Shape shape,
                                         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 method is similar to Shape.intersects(Rectangle2D), except that it 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

@Deprecated
public static boolean equalsEpsilon(Rectangle2D rect1,
                                               Rectangle2D rect2)
Deprecated. Use your own comparison method instead, so you can specify different tolerance threshold along different axis.

Returns true if the two rectangles are equals up to an epsilon value.

Parameters:
rect1 - The first rectangle to test for equality.
rect2 - The second rectangle to test for equality.
Returns:
true if the rectangles are equal up to the tolerance value.

contains

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

Specified by:
contains in interface Shape
Overrides:
contains in class Rectangle2D
Parameters:
x - The x minimal ordinate value.
y - The y minimal ordinate value.
width - The rectangle width.
height - The rectangle height.
Returns:
true if this rectangle entirely contains specified set of rectangular coordinates; false otherwise.

contains

public boolean contains(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 Shape
Overrides:
contains in class 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 Shape
Overrides:
contains in class 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 rectangle, false otherwise.

containsInclusive

public static boolean containsInclusive(Rectangle2D outter,
                                        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.
TODO:
Check for negative width or height (should returns false).

outcode

public int outcode(double x,
                   double y)
Determines where the specified coordinates lie with respect to this rectangle. 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 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 Rectangle2D createIntersection(Rectangle2D rect)
Returns a new Rectangle2D object representing the intersection of this rectangle with the specified one.

Specified by:
createIntersection in class Rectangle2D
Parameters:
rect - The Rectangle2D to be intersected with this rectangle.
Returns:
The largest Rectangle2D contained in both the specified rectangle and this one.
See Also:
intersect(Rectangle2D)

createUnion

public Rectangle2D createUnion(Rectangle2D rect)
Returns a new Rectangle2D object representing the union of this rectangle with the specified one.

Specified by:
createUnion in class Rectangle2D
Parameters:
rect - The Rectangle2D to be combined with this rectangle.
Returns:
The smallest Rectangle2D containing both the specified Rectangle2D and this one.

add

public void add(double x,
                double y)
Adds a point, specified by the arguments x and y, to this rectangle. The resulting Rectangle2D is the smallest rectangle that contains both the original rectangle 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 Rectangle2D
Parameters:
x - X ordinate value of the point to add.
y - Y ordinate value of the point to add.

add

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

Overrides:
add in class Rectangle2D
Parameters:
rect - The Rectangle2D to add to this rectangle.

intersect

public void intersect(Rectangle2D rect)
Intersects a Rectangle2D object with this rectangle. The resulting rectangle is the intersection of the two Rectangle2D objects.

Invoking this method is equivalent to invoking the following code, except that this method behaves correctly with infinite values.

Rectangle2D.intersect(this, rect, this);

Parameters:
rect - The Rectangle2D to intersect with this rectangle.
Since:
3.10
See Also:
Rectangle2D.intersect(Rectangle2D, Rectangle2D, Rectangle2D), createIntersection(Rectangle2D)

toString

public String toString()
Returns the String representation of this Rectangle2D. The ordinate order is (xmin, ymin, xmax, ymax), which is consistent with the createFromExtremums(double, double, double, double) constructor and with the BBOX Well Known Text (WKT) syntax.

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


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.