Class Rectangle2D
- java.lang.Object
-
- com.supermap.data.Rectangle2D
-
public class Rectangle2D extends java.lang.Object
The Rectangle2D class.The Rectangle2D class. Represents a rectangle whose coordinates are double-precision floating-point values, that is, the x-coordinate of the left edge, the y-coordinate of the bottom edge, the x-coordinate of the right edge and the y-coordinate of the top edge are all 64-bit floating-point values. Where, the x-coordinate of the left edge should be no greater than the x-coordinate of the right edge and the y-coordinate of the bottom edge should be no greater than the y-coordinate of the top edge.
The Rectangle2D object is often used to determine bounds, and often acts as the minimum rectangle that contains a geometric object, the visible bounds of map window, the bounds of a dataset, etc. Besides, when performing selecting by rectangle or querying by rectangle and suchlike operations, the Rectangle2D object will be useful.
The general way to construct a new Rectangle2D object is using the given x-coordinate of the left edge, y-coordinate of the bottom edge, x-coordinate of the right edge and y-coordinate of the top edge; The new Rectangle instance constructed using the default constructor is an empty object, namely x-coordinate of left edge, y-coordinate of bottom edge,x-coordinate of the right edge and y-coordinate of the top edge are all -1.7976931348623157e+308. You can also initialize a new Rectangle2D instance with given lower-left corner point and upper-right corner point, or with the specified lower-left corner point and the width and height of the rectangle.
This class provides methods for determining relationship between rectangles, and methods to get the intersection and union of two rectangles.You can inflate and offset the rectangle, and also create new rectangle with integer boundary coordinates by performing some calculations on the boundary coordinates of given rectangle using the methods provided by this class.
-
-
Constructor Summary
Constructors Constructor and Description Rectangle2D()
Constructs a new Rectangle2D object, its coordinate value is -1.7976931348623157e+308.Rectangle2D(double left, double bottom, double right, double top)
Creates a new Rectangle2D object according to the specified arguments.Rectangle2D(Point2D leftBottom, double width, double height)
Creates a new Rectangle2D object according to the specified arguments.Rectangle2D(Point2D leftBottom, Point2D rightTop)
Creates a new Rectangle2D object according to the specified arguments.Rectangle2D(Point2D center, Size2D size)
Creates a new Rectangle2D object according to the specified arguments.Rectangle2D(Rectangle2D rect)
Constructs a new object identical to the given Rectangle2D object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description static Rectangle2D
ceiling(Rectangle2D rect)
Use the minimum integral values larger than or equal to the x-coordinate of left edge, y-coordinate of bottom edge, x-coordinate of the right edge and y-coordinate of the top edge to construct a new Rectangle2D object, then return the object.Rectangle2D
clone()
Returns a copy of the current Rectangle2D object.boolean
contains(double x, double y)
Determines whether the point with the given coordinate is contained in the rectangle.boolean
contains(Point2D pt)
Determines whether the given point is contained in the rectangle.boolean
contains(Rectangle2D rect)
Determines whether the given rectangle area is contained in the rectangle.boolean
equals(java.lang.Object obj)
Overwrites the equal method of the base class.boolean
equals(Rectangle2D rect)
Determines whether this Rectangle2D object equals to the given Rectangle2D object, that is whether they have the same border coordinate.static Rectangle2D
floor(Rectangle2D rect)
Returns a Rectangle2D object, whose x-coordinate of left edge, y-coordinate of bottom edge, x-coordinate of the right edge and y-coordinate of the top edge is the maximum integral value not larger than the corresponding coordinate values of the specified Rectangle2D object.double
getBottom()
Returns the coordinate of the bottom edge of the Rectangle2D object.Point2D
getCenter()
Returns the center point of the Rectangle2D object.static Rectangle2D
getEMPTY()
Returns an empty Rectangle2D, whose x-coordinate of left edge, y-coordinate of bottom edge,x-coordinate of the right edge and y-coordinate of the top edge are all -1.7976931348623157e+308.double
getHeight()
Returns the height of this Rectangle2D object, and the value of the height of the rectangle is the result of subtracting the y-coordinate of the bottom edge from the y-coordinate of the top edge.double
getLeft()
Returns the coordinate of the left edge of the Rectangle2D object.double
getRight()
Returns the coordinate of the right edge of the Rectangle2D object.double
getTop()
Returns the coordinate of the top edge of the Rectangle2D object.double
getWidth()
Returns the width of the Rectangle2D object, it's the result of subtracting the x-coordinate of the left edge from the x-coordinate of the right edge.int
hashCode()
The equals method is overwritten, so hashcode must be overwritten, otherwise, the contains method in key-value will occur errors. .boolean
hasIntersection(Rectangle2D rect)
Determines whether this rectangle intersects with another rectangle, When this rectangle and the specified rectangle have the common point or common line, they are considered to have intersections.void
inflate(double x, double y)
This method zoom in/out the rectangle in the horizontal and vertical direction.void
intersect(Rectangle2D rect)
Replace the rectangle with the intersection of this rectangle and the specified rectangle.boolean
isEmpty()
Determines whether the rectangle is empty, that is the x-coordinate of left edge, y-coordinate of bottom edge, x-coordinate of the right edge and y-coordinate of the top edge are all -1.7976931348623157e+308.void
offset(double dx, double dy)
Moves the Rectangle2D along both x-axis direction and y-axis direction.static Rectangle2D
round(Rectangle2D rect)
To round up or down the x-coordinate of left edge, y-coordinate of bottom edge, x-coordinate of the right edge and y-coordinate of the top edge to construct a new Rectangle2D object, then return the object.void
setBottom(double value)
Sets the y-coordinate of the bottom edge of this Rectangle2D structure.void
setLeft(double value)
Sets the coordinate of the left edge of this Rectangle2D structure.void
setRight(double value)
Sets the coordinate of the right edge of this Rectangle2D structure.void
setTop(double value)
Sets the y-coordinate of the top edge of this Rectangle2D structure.java.lang.String
toString()
Retrieves a readable string that indicates the current Rectangle2D, and the format is {Left=,Bottom=,Right=,Top=}.void
union(Rectangle2D rect)
Replaces this Rectangle2D with the union of this Rectangle2D structure and the specified Rectangle2D object.
-
-
-
Constructor Detail
-
Rectangle2D
public Rectangle2D()
Constructs a new Rectangle2D object, its coordinate value is -1.7976931348623157e+308.
-
Rectangle2D
public Rectangle2D(Rectangle2D rect)
Constructs a new object identical to the given Rectangle2D object.- Parameters:
rect
- the given Rectangle2D object.
-
Rectangle2D
public Rectangle2D(Point2D leftBottom, double width, double height)
Creates a new Rectangle2D object according to the specified arguments.- Parameters:
leftBottom
- the bottom left point coordinate of the rectangle.width
- The width of rectangle.height
- The height of rectangle.
-
Rectangle2D
public Rectangle2D(Point2D leftBottom, Point2D rightTop)
Creates a new Rectangle2D object according to the specified arguments.- Parameters:
leftBottom
- the left bottom corner.rightTop
- the right top corner.
-
Rectangle2D
public Rectangle2D(double left, double bottom, double right, double top)
Creates a new Rectangle2D object according to the specified arguments.- Parameters:
left
- The x-coordinate of left edge of the rectangle.bottom
- the y-coordinate y of the bottom edge.right
- The x-coordinate of the right edge of the rectangle.top
- the y-coordinate of the top edge.
-
-
Method Detail
-
getEMPTY
public static final Rectangle2D getEMPTY()
Returns an empty Rectangle2D, whose x-coordinate of left edge, y-coordinate of bottom edge,x-coordinate of the right edge and y-coordinate of the top edge are all -1.7976931348623157e+308.- Returns:
- an empty Rectangle2D object.
-
isEmpty
public boolean isEmpty()
Determines whether the rectangle is empty, that is the x-coordinate of left edge, y-coordinate of bottom edge, x-coordinate of the right edge and y-coordinate of the top edge are all -1.7976931348623157e+308. The determine precision used is equal-zero precision by default. For more information about default equal-zero precision, please refer to theEnvironment
class.- Returns:
- returns true when the x-coordinate of left edge, y-coordinate of bottom edge, x-coordinate of the right edge and y-coordinate of the top edge are all -1.7976931348623157e+308.
- Default:
- The default value is true.
-
getCenter
public Point2D getCenter()
Returns the center point of the Rectangle2D object. The value of the x-coordinate of the center point is (the coordinate of the left edge + the coordinate of the right edge)/2, and the value of the y-coordinate of the center point is (the coordinate of the top edge + the coordinate of the bottom edge)/2.- Returns:
- the
Point2D
object representing the center of the rectangle. - Default:
- The default value is {X = -Infinity,Y = -Infinity}.
-
getTop
public double getTop()
Returns the coordinate of the top edge of the Rectangle2D object.- Returns:
- the coordinate of the top edge of the Rectangle2D object.
- Default:
- The default value is -1.79769313486232E+308.
-
setTop
public void setTop(double value)
Sets the y-coordinate of the top edge of this Rectangle2D structure.- Parameters:
value
- the coordinate of the top edge of the Rectangle2D object.
-
getBottom
public double getBottom()
Returns the coordinate of the bottom edge of the Rectangle2D object.- Returns:
- the coordinate of the bottom edge of the Rectangle2D object.
- Default:
- The default value is -1.7976931348623157e+308
-
setBottom
public void setBottom(double value)
Sets the y-coordinate of the bottom edge of this Rectangle2D structure.- Parameters:
value
- the coordinate of the bottom edge of the Rectangle2D object.
-
getLeft
public double getLeft()
Returns the coordinate of the left edge of the Rectangle2D object.- Returns:
- the coordinate of the left edge of the Rectangle2D object.
- Default:
- The default value is -1.79769313486232E+308.
-
setLeft
public void setLeft(double value)
Sets the coordinate of the left edge of this Rectangle2D structure.- Parameters:
value
- the coordinate of the left edge of the Rectangle2D object.
-
getRight
public double getRight()
Returns the coordinate of the right edge of the Rectangle2D object.- Returns:
- the coordinate of the right edge of the Rectangle2D object.
- Default:
- The default value is -1.79769313486232E+308.
-
setRight
public void setRight(double value)
Sets the coordinate of the right edge of this Rectangle2D structure.- Parameters:
value
- the coordinate of the right edge of the Rectangle2D object.
-
getWidth
public double getWidth()
Returns the width of the Rectangle2D object, it's the result of subtracting the x-coordinate of the left edge from the x-coordinate of the right edge.- Returns:
- the width of the Rectangle2D object.
- Default:
- The default value is -1.79769313486232E+308.
-
getHeight
public double getHeight()
Returns the height of this Rectangle2D object, and the value of the height of the rectangle is the result of subtracting the y-coordinate of the bottom edge from the y-coordinate of the top edge.- Returns:
- the height of the Rectangle2D object.
- Default:
- The default value is 0.
-
clone
public Rectangle2D clone()
Returns a copy of the current Rectangle2D object.- Overrides:
clone
in classjava.lang.Object
- Returns:
- The new Rectangle2D object generated from the clone operation.
-
ceiling
public static Rectangle2D ceiling(Rectangle2D rect)
Use the minimum integral values larger than or equal to the x-coordinate of left edge, y-coordinate of bottom edge, x-coordinate of the right edge and y-coordinate of the top edge to construct a new Rectangle2D object, then return the object.- Parameters:
rect
- The Rectanlge2D object to be converted.- Returns:
- the Rectangle2D object got by converting.
-
floor
public static Rectangle2D floor(Rectangle2D rect)
Returns a Rectangle2D object, whose x-coordinate of left edge, y-coordinate of bottom edge, x-coordinate of the right edge and y-coordinate of the top edge is the maximum integral value not larger than the corresponding coordinate values of the specified Rectangle2D object.- Parameters:
rect
- The Rectanlge2D object to be converted.- Returns:
- the Rectangle2D object got by converting.
-
round
public static Rectangle2D round(Rectangle2D rect)
To round up or down the x-coordinate of left edge, y-coordinate of bottom edge, x-coordinate of the right edge and y-coordinate of the top edge to construct a new Rectangle2D object, then return the object.- Parameters:
rect
- The Rectanlge2D object to be converted.- Returns:
- the Rectangle2D object got by converting.
-
contains
public boolean contains(Point2D pt)
Determines whether the given point is contained in the rectangle. The point on the border of the rectangle is considered in the rectangle.- Parameters:
pt
- the Point2D to test.- Returns:
- true if the point is in the rectangle or on the boundaries, or false otherwise.
-
contains
public boolean contains(Rectangle2D rect)
Determines whether the given rectangle area is contained in the rectangle. It is considered contained in the rectangle if they are coincide with each other.- Parameters:
rect
- The Rectanlge2D object to be tested.- Returns:
- if the given rectangle is inside the rectangle or coincide with it, returns true.
-
contains
public boolean contains(double x, double y)
Determines whether the point with the given coordinate is contained in the rectangle. The point on the border of the rectangle is considered in the rectangle.- Parameters:
x
- The X coordinate of the point that might be contained in this Rectangle2D.y
- The Y coordinate of the point that might be contained in this Rectangle2D.- Returns:
- true if the point formed of the specified coordinates is in the rectangle or on the boundaries, or false otherwise.
-
equals
public boolean equals(Rectangle2D rect)
Determines whether this Rectangle2D object equals to the given Rectangle2D object, that is whether they have the same border coordinate. Note that the comparison is performed in terms of the equal-zero precision. For more information about equal-zero precision, please refer to the class.- Parameters:
rect
- The Rectanlge2D object to be tested.- Returns:
- returns true if the border coordinates of the given rectangle equal to that of this rectangle.
-
inflate
public void inflate(double x, double y)
This method zoom in/out the rectangle in the horizontal and vertical direction.Resulting x-coordinate of the left edge = Min(the original x-coordinate of the left edge-dx, the original x-coordinate of the right edge+dx);
Resulting y-coordinate of the bottom edge = Min(the original y-coordinate of the bottom edge-dy, the original y-coordinate of the top edge+dy);
Resulting x-coordinate of the right edge = Max(the original x-coordinate of the left edge-dx, the original x-coordinate of the right edge+dx);
Resulting y-coordinate of the top edge = Max(the original y-coordinate of the bottom edge-dy, the original y-coordinate of the top edge+dy);
And the center point remains unchanged.
As shown on the left figure, the rectangle with dashed is the result of resizing the rectangle with real line; if dx, dy is negative, the rectangle with the real line is the result of resizing the rectangle with dashed.when dx, dy are negative and |dx| is larger than the width of the rectangle, |dy| is larger than the height of the rectangle, the original left border will become the right border after zoom in/out, The original top border will become the new bottom border, as shown in the right figure, the rectangle with the active line becomes the rectangle with the dotted line.
- Parameters:
x
- the horizontal zoom of the Rectangle2D.y
- the vertical zoom of the Rectangle2D.
-
intersect
public void intersect(Rectangle2D rect)
Replace the rectangle with the intersection of this rectangle and the specified rectangle. If they have no intersection, it will become a empty rectangle object.- Parameters:
rect
- the rectangle to perform the intersecting operation.
-
hasIntersection
public boolean hasIntersection(Rectangle2D rect)
Determines whether this rectangle intersects with another rectangle, When this rectangle and the specified rectangle have the common point or common line, they are considered to have intersections.- Parameters:
rect
- The Rectanlge2D object to be tested.- Returns:
- If the intersection of the two rectangles is not empty, this method returns true; otherwise it returns false.
-
offset
public void offset(double dx, double dy)
Moves the Rectangle2D along both x-axis direction and y-axis direction. The offset along x-axis direction is dx, while the offset along y-axis direction is dy.Resulting x-coordinate of the left edge = the original x-coordinate of the left edge+dx;
Resulting x-coordinate of the bottom edge = the original x-coordinate of the bottom edge+dy;
Resulting x-coordinate of the right edge = the original x-coordinate of the right edge+dx;
Resulting x-coordinate of the bottom edge = the original x-coordinate of the bottom edge+dy;
The image shows the effect of a rectangle being translated. The size of the rectangle is unchanged.
- Parameters:
dx
- The amount to offset the x-coordinate.dy
- The amount to offset the y-coordinate.
-
toString
public java.lang.String toString()
Retrieves a readable string that indicates the current Rectangle2D, and the format is {Left=,Bottom=,Right=,Top=}.- Overrides:
toString
in classjava.lang.Object
- Returns:
- A string that Rectangle2D represents this .
-
union
public void union(Rectangle2D rect)
Replaces this Rectangle2D with the union of this Rectangle2D structure and the specified Rectangle2D object.- Parameters:
rect
- The rectangle used to form the union.
-
hashCode
public int hashCode()
The equals method is overwritten, so hashcode must be overwritten, otherwise, the contains method in key-value will occur errors. .- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- int
-
equals
public boolean equals(java.lang.Object obj)
Overwrites the equal method of the base class.- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- Object- Returns:
- boolean
-
-