public class RectF
extends java.lang.Object
float coordinates.
The rectangle is represented by four edges: left, top, right,
bottom. This is similar in spirit to Android's android.graphics.RectF.
Unless otherwise stated, this class does not enforce that left <= right and
top <= bottom. Some operations assume a non-empty rectangle.
| Modifier and Type | Field and Description |
|---|---|
float |
bottom
The Y coordinate of the bottom edge of the rectangle.
|
float |
left
The X coordinate of the left edge of the rectangle.
|
float |
right
The X coordinate of the right edge of the rectangle.
|
float |
top
The Y coordinate of the top edge of the rectangle.
|
| Constructor and Description |
|---|
RectF()
Creates an empty rectangle with all coordinates initialized to
0. |
RectF(float left,
float top,
float right,
float bottom)
Creates a rectangle with the specified coordinates.
|
RectF(RectF r)
Copy constructor.
|
| Modifier and Type | Method and Description |
|---|---|
float |
centerX()
Returns the horizontal center coordinate of the rectangle:
(left + right) / 2. |
float |
centerY()
Returns the vertical center coordinate of the rectangle:
(top + bottom) / 2. |
boolean |
contains(float x,
float y)
Returns whether the point
(x, y) is inside the rectangle. |
boolean |
contains(float left,
float top,
float right,
float bottom)
Returns whether the rectangle defined by the specified edges is fully contained within this rectangle.
|
boolean |
contains(RectF r)
Returns whether
r is fully contained within this rectangle. |
boolean |
equals(java.lang.Object o)
Indicates whether some other object is "equal to" this one.
|
int |
hashCode()
Returns a hash code value for this rectangle.
|
float |
height()
Returns the rectangle's height:
bottom - top. |
void |
inset(float dx,
float dy)
Insets the rectangle by
(dx, dy). |
boolean |
intersect(float left,
float top,
float right,
float bottom)
Intersects this rectangle with the rectangle defined by the specified edges.
|
boolean |
intersect(RectF r)
Intersects this rectangle with
r. |
boolean |
intersects(float left,
float top,
float right,
float bottom)
Returns whether this rectangle intersects the rectangle defined by the specified edges.
|
static boolean |
intersects(RectF a,
RectF b)
Returns whether rectangles
a and b intersect. |
boolean |
isEmpty()
Returns
true if the rectangle is empty (i.e. |
void |
offset(float dx,
float dy)
|
void |
offsetTo(float newLeft,
float newTop)
Moves the rectangle so that its top-left corner becomes
(newLeft, newTop),
keeping its width and height unchanged. |
void |
printShortString(java.io.PrintWriter pw)
Prints the compact representation of this rectangle to the given
PrintWriter. |
void |
scale(float scale)
Scales this rectangle in place by the given factor.
|
void |
set(float left,
float top,
float right,
float bottom)
Sets this rectangle's coordinates to the specified values.
|
void |
set(RectF src)
Copies the coordinates from
src into this rectangle. |
void |
setEmpty()
Sets this rectangle to
(0,0,0,0). |
boolean |
setIntersect(RectF a,
RectF b)
Sets this rectangle to the intersection of
a and b. |
void |
sort()
Ensures that
left <= right and top <= bottom by swapping edges if needed. |
java.lang.String |
toShortString()
Returns a compact string representation of this rectangle.
|
java.lang.String |
toShortString(java.lang.StringBuilder sb)
Returns a compact string representation of this rectangle using the provided
StringBuilder. |
java.lang.String |
toString()
Returns a string representation of this rectangle.
|
void |
union(float x,
float y)
Expands this rectangle to include the point
(x, y). |
void |
union(float left,
float top,
float right,
float bottom)
Unions this rectangle with the rectangle defined by the specified edges.
|
void |
union(RectF r)
Unions this rectangle with
r. |
float |
width()
Returns the rectangle's width:
right - left. |
public float left
public float top
public float right
public float bottom
public RectF()
0.public RectF(float left,
float top,
float right,
float bottom)
No range checking is performed; the caller must ensure that left <= right
and top <= bottom if a non-empty rectangle is required.
left - x coordinate of the left edgetop - y coordinate of the top edgeright - x coordinate of the right edgebottom - y coordinate of the bottom edgepublic RectF(RectF r)
r.
If r is null, this rectangle is set to empty (all zeros).
r - the rectangle to copy from; may be nullpublic boolean equals(java.lang.Object o)
equals in class java.lang.Objecto - the reference object with which to comparetrue if this rectangle has the same edge values as o; otherwise falsepublic int hashCode()
hashCode in class java.lang.Objectpublic java.lang.String toString()
toString in class java.lang.ObjectRectF(left, top, right, bottom)public java.lang.String toShortString()
[left,top][right,bottom]public java.lang.String toShortString(java.lang.StringBuilder sb)
StringBuilder.sb - builder to write into (will be cleared)[left,top][right,bottom]java.lang.NullPointerException - if sb is nullpublic void printShortString(java.io.PrintWriter pw)
PrintWriter.pw - writer to print tojava.lang.NullPointerException - if pw is nullpublic final boolean isEmpty()
true if the rectangle is empty (i.e. left >= right or top >= bottom).true if empty; otherwise falsepublic final float width()
right - left.
No validation is performed; the result may be negative if left > right.
public final float height()
bottom - top.
No validation is performed; the result may be negative if top > bottom.
public final float centerX()
(left + right) / 2.
No validation is performed.
public final float centerY()
(top + bottom) / 2.
No validation is performed.
public void setEmpty()
(0,0,0,0).public void set(float left,
float top,
float right,
float bottom)
No range checking is performed; the caller must ensure that left <= right
and top <= bottom if a non-empty rectangle is required.
left - x coordinate of the left edgetop - y coordinate of the top edgeright - x coordinate of the right edgebottom - y coordinate of the bottom edgepublic void set(RectF src)
src into this rectangle.src - the source rectanglejava.lang.NullPointerException - if src is nullpublic void offset(float dx,
float dy)
dx - delta xdy - delta ypublic void offsetTo(float newLeft,
float newTop)
(newLeft, newTop),
keeping its width and height unchanged.newLeft - new left coordinatenewTop - new top coordinatepublic void inset(float dx,
float dy)
(dx, dy).
dx > 0, the rectangle becomes narrower; if dx < 0, it becomes wider.dy > 0, the rectangle becomes shorter; if dy < 0, it becomes taller.dx - amount to add/subtract on left/right edgesdy - amount to add/subtract on top/bottom edgespublic boolean contains(float x,
float y)
(x, y) is inside the rectangle.
The left and top edges are considered inside; the right and bottom edges are considered outside.
That is, containment means left <= x < right and top <= y < bottom.
An empty rectangle never contains any point.
x - x coordinate to testy - y coordinate to testtrue if the point is contained; otherwise falsepublic boolean contains(float left,
float top,
float right,
float bottom)
left - left edge of the rectangle to testtop - top edge of the rectangle to testright - right edge of the rectangle to testbottom - bottom edge of the rectangle to testtrue if the specified rectangle is contained; otherwise falsepublic boolean contains(RectF r)
r is fully contained within this rectangle.r - rectangle to testtrue if r is contained; otherwise falsejava.lang.NullPointerException - if r is nullpublic boolean intersect(float left,
float top,
float right,
float bottom)
If the rectangles intersect, this rectangle is updated to the intersection and true is returned.
Otherwise this rectangle is not modified and false is returned.
left - left edge of the other rectangletop - top edge of the other rectangleright - right edge of the other rectanglebottom - bottom edge of the other rectangletrue if an intersection occurred and this rectangle was updated; otherwise falsepublic boolean intersect(RectF r)
r.r - other rectangletrue if an intersection occurred and this rectangle was updated; otherwise falsejava.lang.NullPointerException - if r is nullpublic boolean setIntersect(RectF a, RectF b)
a and b.a - first rectangleb - second rectangletrue if a and b intersect and this rectangle was updated; otherwise falsejava.lang.NullPointerException - if a or b is nullpublic boolean intersects(float left,
float top,
float right,
float bottom)
This method does not modify this rectangle.
left - left edge of the other rectangletop - top edge of the other rectangleright - right edge of the other rectanglebottom - bottom edge of the other rectangletrue if the rectangles intersect; otherwise falsepublic static boolean intersects(RectF a, RectF b)
a and b intersect.a - first rectangleb - second rectangletrue if they intersect; otherwise falsejava.lang.NullPointerException - if a or b is nullpublic void union(float left,
float top,
float right,
float bottom)
If this rectangle is empty, it becomes the specified rectangle. If the specified rectangle is empty, no changes are made.
left - left edge to uniontop - top edge to unionright - right edge to unionbottom - bottom edge to unionpublic void union(RectF r)
r.r - rectangle to union withjava.lang.NullPointerException - if r is nullpublic void union(float x,
float y)
(x, y).
No check is performed to see whether this rectangle is non-empty.
x - x coordinate of the point to includey - y coordinate of the point to includepublic void sort()
left <= right and top <= bottom by swapping edges if needed.
If the edges are already in order, nothing changes.
public void scale(float scale)
scale - scale factor (e.g. 2.0f doubles the size)