danger.ui
Class Rect

java.lang.Object
  extended by danger.ui.Rect


public class Rect
extends Object

Various methods for constructing and manipulating a 2-dimension rectangle Danger rectangles are exclusive meaning that the upper-left and bottom-right points define the upper-left corner of a pixel rather than the center of a pixel. This has the advantage that the width of the rectangle is (right - left) and the height is (bottom - top). All rational people define rectangles this way.


Field Summary
 int bottom
          Bottom coordinate of the rectangle
 int left
          Left coordinate of the rectangle
 int right
          Right coordinate of the rectangle
 int top
          Top coordinate of the rectangle
 
Constructor Summary
Rect()
          Generic constructor.
Rect(int width, int height)
          Construct which creates a zero-based rectangle with a specific width and height.
Rect(int left, int top, int right, int bottom)
          Constructor which creates a new rectangle and sets its ordinates to the specific values passed in.
Rect(Rect r)
          Copy constructor.
 
Method Summary
 Rect clip(Rect this_rect, Rect that_rect)
          Modifies this rectangle to be the intersection of two rectangles.
 Rect clipAdjust(Rect this_rect, Rect that_rect, int deltaX, int deltaY)
          Modifies this rectangle to be the intersection of two rectangles, offset by a specified amount.
 boolean contains(int x, int y)
          Returns whether or not a rectangle envelopes a specific point
 boolean contains(Point p)
          Returns whether or not a rectangle envelopes a specific point
 boolean contains(Rect r)
          Returns whether or not a rectangle envelopes a specific rectangle
 boolean equals(Object rhs)
           
 boolean equals(Rect r)
          Determines whether this rectangle is the same as the specified rectangle.
 int getHeight()
          Returns the height of a rectangle
 int getWidth()
          Returns the width of a rectangle
 int hashCode()
           
 void inset(int delta)
          Adjusts all the sides of a rectangle by a given amount
 void inset(int deltax, int deltay)
          Adjusts all the sides of a rectangle by a given horizontal and vertical amount
 Rect intersection(Rect inR, Rect outResult)
          Modifies one rectangle to be the intersection of it and another rectangle.
 boolean intersects(int inLeft, int inTop, int inRight, int inBottom)
          Returns whether or not a rectangle intersects another rectangle
 boolean intersects(Rect rect)
          Returns whether or not a rectangle intersects another rectangle
 boolean isEmpty()
          Determines whether this rectangle is empty.
static int manhattanDistance(Rect r1, Rect r2)
          Given two rectangles, return the "distance" between them in Manhattan geometry.
static Rect newXYWH(int left, int top, int width, int height)
          "Constructor" which creates a new rectangle and sets its ordinates based on a left/top, and a width/height.
 void setPosition(int x, int y)
          Moves a rectangle to a specific horizontal and vertical position
 void setSize(int width, int height)
          Adjusts the width and height of a rectangle
 void setTo(int inLeft, int inTop, int inRight, int inBottom)
          Sets the left, top, right and bottom values of the rectangle
 void setTo(Rect inRect)
          Sets the left, top, right and bottom values of the rectangle
 String toString()
           
 void translate(int inX, int inY)
          Move a rectangle by a specific horizontal and vertical amount
 void translate(Point inP)
          Move a rectangle by a specific horizontal and vertical amount
 void translate(Rect base, int deltaX, int deltaY)
          Modify a rectangle in place, changing it to be the same as the the specified rectangle, translated by the specified horizontal and vertical amount
 void union(Rect r)
          Sets the dimensions of this rectangle to the union of itself and the specificed second rectangle.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

left

public int left
Left coordinate of the rectangle


top

public int top
Top coordinate of the rectangle


right

public int right
Right coordinate of the rectangle


bottom

public int bottom
Bottom coordinate of the rectangle

Constructor Detail

Rect

public Rect(Rect r)
Copy constructor. Creates a new rectangle object and sets its values to the match those of the provided rectangle.

Parameters:
r - the source rectangle to be used for the values of the new rectangle

Rect

public Rect()
Generic constructor. Creates a new rectangle and sets all of its ordinates to zero.


Rect

public Rect(int width,
            int height)
Construct which creates a zero-based rectangle with a specific width and height.

Parameters:
width - value for the right side of the rect
height - value for the bottom of the rect

Rect

public Rect(int left,
            int top,
            int right,
            int bottom)
Constructor which creates a new rectangle and sets its ordinates to the specific values passed in.

Parameters:
left - value for the left side of the rect
top - value for the top of the rect
right - value for the right side of the rect
bottom - value for the bottom of the rect
Method Detail

newXYWH

public static Rect newXYWH(int left,
                           int top,
                           int width,
                           int height)
"Constructor" which creates a new rectangle and sets its ordinates based on a left/top, and a width/height.

Parameters:
left - value for the left side of the rect
top - value for the top of the rect
width - value for the width of the rect
height - value for the height of the rect
Returns:
non-null; an appropriately-constructed instance

translate

public final void translate(int inX,
                            int inY)
Move a rectangle by a specific horizontal and vertical amount

Parameters:
inX - distance to move the rect horizontally
inY - distance to move the rect vertically

translate

public final void translate(Point inP)
Move a rectangle by a specific horizontal and vertical amount

Parameters:
inP - distance to move the rect horizontally

translate

public final void translate(Rect base,
                            int deltaX,
                            int deltaY)
Modify a rectangle in place, changing it to be the same as the the specified rectangle, translated by the specified horizontal and vertical amount

Parameters:
base - the rectangle base the delta from
deltaX - the horizontal delta
deltaY - the vertical delta

union

public final void union(Rect r)
Sets the dimensions of this rectangle to the union of itself and the specificed second rectangle.

Parameters:
r - the rectangle to union with the current rect

clip

public final Rect clip(Rect this_rect,
                       Rect that_rect)
Modifies this rectangle to be the intersection of two rectangles. If the intersection is empty, this method may return a rectangle where the left coordinate is greater than or equal to the right, and the top coordinate is greater than or equal to the bottom.

Parameters:
this_rect - one of the rectangles to intersect
that_rect - one of the rectangles to intersect
Returns:
this rectangle

clipAdjust

public final Rect clipAdjust(Rect this_rect,
                             Rect that_rect,
                             int deltaX,
                             int deltaY)
Modifies this rectangle to be the intersection of two rectangles, offset by a specified amount. The rectangles are first intersected, and then the result is offset. Note that the offsets are interpreted as negative offsets, so positive values for deltaX and deltaY decrease the x and y coordinates of the rectangle. If the intersection is empty, this method may return a rectangle where the left coordinate is greater than or equal to the right, and the top coordinate is greater than or equal to the bottom.

Parameters:
this_rect - one of the rectangles to intersect
that_rect - one of the rectangles to intersect
deltaX - negative horizontal offset
deltaY - negative vertical offset
Returns:
this rectangle

setPosition

public final void setPosition(int x,
                              int y)
Moves a rectangle to a specific horizontal and vertical position

Parameters:
x - new horizontal position for the rect
y - new vertical position for the rect

setSize

public final void setSize(int width,
                          int height)
Adjusts the width and height of a rectangle

Parameters:
width - new width for the rect
height - new height for the rect

setTo

public final void setTo(Rect inRect)
Sets the left, top, right and bottom values of the rectangle

Parameters:
inRect - rectangle to grab new values from

setTo

public final void setTo(int inLeft,
                        int inTop,
                        int inRight,
                        int inBottom)
Sets the left, top, right and bottom values of the rectangle

Parameters:
inLeft - new value for left field
inTop - new value for top field
inRight - new value for right field
inBottom - new value for bottom field

inset

public final void inset(int deltax,
                        int deltay)
Adjusts all the sides of a rectangle by a given horizontal and vertical amount

Parameters:
deltax - amount to move in the left and right sides of the rect
deltay - amount to move in the top and bottom of the rect

inset

public final void inset(int delta)
Adjusts all the sides of a rectangle by a given amount

Parameters:
delta - amount to move in the sides of the rect

getWidth

public final int getWidth()
Returns the width of a rectangle

Returns:
the width of the rectangle

getHeight

public final int getHeight()
Returns the height of a rectangle

Returns:
the height of the rectangle

contains

public final boolean contains(int x,
                              int y)
Returns whether or not a rectangle envelopes a specific point

Parameters:
x - the horizontal offset of the point being checked
y - the vertical offset of the point being checked

contains

public final boolean contains(Point p)
Returns whether or not a rectangle envelopes a specific point

Parameters:
p - the point being checked

contains

public final boolean contains(Rect r)
Returns whether or not a rectangle envelopes a specific rectangle

Parameters:
r - the rectangle being checked

intersects

public final boolean intersects(Rect rect)
Returns whether or not a rectangle intersects another rectangle

Parameters:
rect - the rectangle being compared against

intersects

public final boolean intersects(int inLeft,
                                int inTop,
                                int inRight,
                                int inBottom)
Returns whether or not a rectangle intersects another rectangle

Parameters:
inLeft - left coordinate of the rectangle being compared against
inTop - top coordinate of the rectangle being compared against
inRight - right coordinate of the rectangle being compared against
inBottom - bottom coordinate of the rectangle being compared against

intersection

public final Rect intersection(Rect inR,
                               Rect outResult)
Modifies one rectangle to be the intersection of it and another rectangle. Similar to clip(Rect,Rect). One difference is that this method specifies a rectangle of (0,0,0,0) if the intersection is empty, whereas the other method may not.

Parameters:
inR - one of the rectangles to intersect
outResult - one of the rectangles to intersect, and to place the result in
Returns:
outResult

manhattanDistance

public static int manhattanDistance(Rect r1,
                                    Rect r2)
Given two rectangles, return the "distance" between them in Manhattan geometry.

Parameters:
r1 - One of the rectangles to compare
r2 - The other of the rectangles to compare
Returns:
The sum of the X and Y distances between the closest edges of the two rectangles. If they intersect, return 0.
Since:
4.5

isEmpty

public final boolean isEmpty()
Determines whether this rectangle is empty.


equals

public final boolean equals(Rect r)
Determines whether this rectangle is the same as the specified rectangle.

Parameters:
r - the rectangle to compare to

equals

public final boolean equals(Object rhs)
Overrides:
equals in class Object

hashCode

public final int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object