It's useful to have our own friendly, simple rectangles and points independent from the various graphics systems that may or may not be available (e.g. java.awt)
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6648 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -0,0 +1,24 @@
|
|||||||
|
package com.threerings.geom;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A very simple representation of the geometric concept, because sometimes we can't use awt.
|
||||||
|
*/
|
||||||
|
public class Point
|
||||||
|
{
|
||||||
|
/** The x-coordinate. */
|
||||||
|
public int x;
|
||||||
|
|
||||||
|
/** The y-coordinate. */
|
||||||
|
public int y;
|
||||||
|
|
||||||
|
public Point ()
|
||||||
|
{
|
||||||
|
this(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Point (int x, int y)
|
||||||
|
{
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package com.threerings.geom;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A very simple representation of the geometric shape, because sometimes we can't use awt.
|
||||||
|
*/
|
||||||
|
public class Rectangle
|
||||||
|
{
|
||||||
|
/** The left-most x-coordinate. */
|
||||||
|
public int x;
|
||||||
|
|
||||||
|
/** The top-most y-coordinate. */
|
||||||
|
public int y;
|
||||||
|
|
||||||
|
/** The width of the rectangle. */
|
||||||
|
public int width;
|
||||||
|
|
||||||
|
/** The height of the rectangle. */
|
||||||
|
public int height;
|
||||||
|
|
||||||
|
public Rectangle ()
|
||||||
|
{
|
||||||
|
this(0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Rectangle (int x, int y, int width, int height)
|
||||||
|
{
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Rectangle (Rectangle other)
|
||||||
|
{
|
||||||
|
x = other.x;
|
||||||
|
y = other.y;
|
||||||
|
width = other.width;
|
||||||
|
height = other.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the area of intersection of ourselves and another rectangle.
|
||||||
|
*/
|
||||||
|
public Rectangle intersect (Rectangle other)
|
||||||
|
{
|
||||||
|
int x1 = Math.max(x, other.x), y1 = Math.max(y, other.y),
|
||||||
|
x2 = Math.min(x + width, other.x + other.width),
|
||||||
|
y2 = Math.min(y + height, other.y + other.height);
|
||||||
|
return new Rectangle(x1, y1, Math.max(0, x2 - x1), Math.max(0, y2 - y1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the specified coordinates are within the rectangle.
|
||||||
|
*/
|
||||||
|
public boolean contains (int x, int y)
|
||||||
|
{
|
||||||
|
return (x >= this.x && x <= (this.x + width) && y >= this.y && y <= (this.y + height));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether this rectangle covers any area.
|
||||||
|
*/
|
||||||
|
public boolean isEmpty ()
|
||||||
|
{
|
||||||
|
return width <= 0 || height <= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets our position and size.
|
||||||
|
*/
|
||||||
|
public void set (int x, int y, int width, int height)
|
||||||
|
{
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user