rectangle2d.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 987 行 · 第 1/2 页
JAVA
987 行
}
/**
* Return the hashcode for this rectangle. The formula is not documented, but
* appears to be the same as:
* <pre>
* long l = Double.doubleToLongBits(getX())
* + 37 * Double.doubleToLongBits(getY())
* + 43 * Double.doubleToLongBits(getWidth())
* + 47 * Double.doubleToLongBits(getHeight());
* return (int) ((l >> 32) ^ l);
* </pre>
*
* @return the hashcode
*/
public int hashCode()
{
// Talk about a fun time reverse engineering this one!
long l = java.lang.Double.doubleToLongBits(getX())
+ 37 * java.lang.Double.doubleToLongBits(getY())
+ 43 * java.lang.Double.doubleToLongBits(getWidth())
+ 47 * java.lang.Double.doubleToLongBits(getHeight());
return (int) ((l >> 32) ^ l);
}
/**
* Tests this rectangle for equality against the specified object. This
* will be true if an only if the specified object is an instance of
* Rectangle2D with the same coordinates and dimensions.
*
* @param obj the object to test against for equality
* @return true if the specified object is equal to this one
*/
public boolean equals(Object obj)
{
if (! (obj instanceof Rectangle2D))
return false;
Rectangle2D r = (Rectangle2D) obj;
return r.getX() == getX() && r.getY() == getY()
&& r.getWidth() == getWidth() && r.getHeight() == getHeight();
}
/**
* This class defines a rectangle in <code>double</code> precision.
*
* @author Eric Blake <ebb9@email.byu.edu>
* @since 1.2
* @status updated to 1.4
*/
public static class Double extends Rectangle2D
{
/** The x coordinate of the lower left corner. */
public double x;
/** The y coordinate of the lower left corner. */
public double y;
/** The width of the rectangle. */
public double width;
/** The height of the rectangle. */
public double height;
/**
* Create a rectangle at (0,0) with width 0 and height 0.
*/
public Double()
{
}
/**
* Create a rectangle with the given values.
*
* @param x the x coordinate
* @param y the y coordinate
* @param w the width
* @param h the height
*/
public Double(double x, double y, double w, double h)
{
this.x = x;
this.y = y;
width = w;
height = h;
}
/**
* Return the X coordinate.
*
* @return the value of x
*/
public double getX()
{
return x;
}
/**
* Return the Y coordinate.
*
* @return the value of y
*/
public double getY()
{
return y;
}
/**
* Return the width.
*
* @return the value of width
*/
public double getWidth()
{
return width;
}
/**
* Return the height.
*
* @return the value of height
*/
public double getHeight()
{
return height;
}
/**
* Test if the rectangle is empty.
*
* @return true if width or height is not positive
*/
public boolean isEmpty()
{
return width <= 0 || height <= 0;
}
/**
* Set the contents of this rectangle to those specified.
*
* @param x the x coordinate
* @param y the y coordinate
* @param w the width
* @param h the height
*/
public void setRect(double x, double y, double w, double h)
{
this.x = x;
this.y = y;
width = w;
height = h;
}
/**
* Set the contents of this rectangle to those specified.
*
* @param r the rectangle to copy
* @throws NullPointerException if r is null
*/
public void setRect(Rectangle2D r)
{
x = r.getX();
y = r.getY();
width = r.getWidth();
height = r.getHeight();
}
/**
* Determine where the point lies with respect to this rectangle. The
* result will be the binary OR of the appropriate bit masks.
*
* @param x the x coordinate to check
* @param y the y coordinate to check
* @return the binary OR of the result
* @see #OUT_LEFT
* @see #OUT_TOP
* @see #OUT_RIGHT
* @see #OUT_BOTTOM
* @since 1.2
*/
public int outcode(double x, double y)
{
int result = 0;
if (width <= 0)
result |= OUT_LEFT | OUT_RIGHT;
else if (x < this.x)
result |= OUT_LEFT;
else if (x > this.x + width)
result |= OUT_RIGHT;
if (height <= 0)
result |= OUT_BOTTOM | OUT_TOP;
else if (y < this.y) // Remember that +y heads top-to-bottom.
result |= OUT_TOP;
else if (y > this.y + height)
result |= OUT_BOTTOM;
return result;
}
/**
* Returns the bounds of this rectangle. A pretty useless method, as this
* is already a rectangle.
*
* @return a copy of this rectangle
*/
public Rectangle2D getBounds2D()
{
return new Double(x, y, width, height);
}
/**
* Return a new rectangle which is the intersection of this and the given
* one. The result will be empty if there is no intersection.
*
* @param r the rectangle to be intersected
* @return the intersection
* @throws NullPointerException if r is null
*/
public Rectangle2D createIntersection(Rectangle2D r)
{
Double res = new Double();
intersect(this, r, res);
return res;
}
/**
* Return a new rectangle which is the union of this and the given one.
*
* @param r the rectangle to be merged
* @return the union
* @throws NullPointerException if r is null
*/
public Rectangle2D createUnion(Rectangle2D r)
{
Double res = new Double();
union(this, r, res);
return res;
}
/**
* Returns a string representation of this rectangle. This is in the form
* <code>getClass().getName() + "[x=" + x + ",y=" + y + ",w=" + width
* + ",h=" + height + ']'</code>.
*
* @return a string representation of this rectangle
*/
public String toString()
{
return getClass().getName() + "[x=" + x + ",y=" + y + ",w=" + width
+ ",h=" + height + ']';
}
} // class Double
/**
* This class defines a rectangle in <code>float</code> precision.
*
* @author Eric Blake <ebb9@email.byu.edu>
* @since 1.2
* @status updated to 1.4
*/
public static class Float extends Rectangle2D
{
/** The x coordinate of the lower left corner. */
public float x;
/** The y coordinate of the lower left corner. */
public float y;
/** The width of the rectangle. */
public float width;
/** The height of the rectangle. */
public float height;
/**
* Create a rectangle at (0,0) with width 0 and height 0.
*/
public Float()
{
}
/**
* Create a rectangle with the given values.
*
* @param x the x coordinate
* @param y the y coordinate
* @param w the width
* @param h the height
*/
public Float(float x, float y, float w, float h)
{
this.x = x;
this.y = y;
width = w;
height = h;
}
/**
* Create a rectangle with the given values.
*
* @param x the x coordinate
* @param y the y coordinate
* @param w the width
* @param h the height
*/
Float(double x, double y, double w, double h)
{
this.x = (float) x;
this.y = (float) y;
width = (float) w;
height = (float) h;
}
/**
* Return the X coordinate.
*
* @return the value of x
*/
public double getX()
{
return x;
}
/**
* Return the Y coordinate.
*
* @return the value of y
*/
public double getY()
{
return y;
}
/**
* Return the width.
*
* @return the value of width
*/
public double getWidth()
{
return width;
}
/**
* Return the height.
*
* @return the value of height
*/
public double getHeight()
{
return height;
}
/**
* Test if the rectangle is empty.
*
* @return true if width or height is not positive
*/
public boolean isEmpty()
{
return width <= 0 || height <= 0;
}
/**
* Set the contents of this rectangle to those specified.
*
* @param x the x coordinate
* @param y the y coordinate
* @param w the width
* @param h the height
*/
public void setRect(float x, float y, float w, float h)
{
this.x = x;
this.y = y;
width = w;
height = h;
}
/**
* Set the contents of this rectangle to those specified.
*
* @param x the x coordinate
* @param y the y coordinate
* @param w the width
* @param h the height
*/
public void setRect(double x, double y, double w, double h)
{
this.x = (float) x;
this.y = (float) y;
width = (float) w;
height = (float) h;
}
/**
* Set the contents of this rectangle to those specified.
*
* @param r the rectangle to copy
* @throws NullPointerException if r is null
*/
public void setRect(Rectangle2D r)
{
x = (float) r.getX();
y = (float) r.getY();
width = (float) r.getWidth();
height = (float) r.getHeight();
}
/**
* Determine where the point lies with respect to this rectangle. The
* result will be the binary OR of the appropriate bit masks.
*
* @param x the x coordinate to check
* @param y the y coordinate to check
* @return the binary OR of the result
* @see #OUT_LEFT
* @see #OUT_TOP
* @see #OUT_RIGHT
* @see #OUT_BOTTOM
* @since 1.2
*/
public int outcode(double x, double y)
{
int result = 0;
if (width <= 0)
result |= OUT_LEFT | OUT_RIGHT;
else if (x < this.x)
result |= OUT_LEFT;
else if (x > this.x + width)
result |= OUT_RIGHT;
if (height <= 0)
result |= OUT_BOTTOM | OUT_TOP;
else if (y < this.y) // Remember that +y heads top-to-bottom.
result |= OUT_TOP;
else if (y > this.y + height)
result |= OUT_BOTTOM;
return result;
}
/**
* Returns the bounds of this rectangle. A pretty useless method, as this
* is already a rectangle.
*
* @return a copy of this rectangle
*/
public Rectangle2D getBounds2D()
{
return new Float(x, y, width, height);
}
/**
* Return a new rectangle which is the intersection of this and the given
* one. The result will be empty if there is no intersection.
*
* @param r the rectangle to be intersected
* @return the intersection
* @throws NullPointerException if r is null
*/
public Rectangle2D createIntersection(Rectangle2D r)
{
Float res = new Float();
intersect(this, r, res);
return res;
}
/**
* Return a new rectangle which is the union of this and the given one.
*
* @param r the rectangle to be merged
* @return the union
* @throws NullPointerException if r is null
*/
public Rectangle2D createUnion(Rectangle2D r)
{
Float res = new Float();
union(this, r, res);
return res;
}
/**
* Returns a string representation of this rectangle. This is in the form
* <code>getClass().getName() + "[x=" + x + ",y=" + y + ",w=" + width
* + ",h=" + height + ']'</code>.
*
* @return a string representation of this rectangle
*/
public String toString()
{
return getClass().getName() + "[x=" + x + ",y=" + y + ",w=" + width
+ ",h=" + height + ']';
}
} // class Float
} // class Rectangle2D
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?