From a27291b430a9331eb9ac3060d9c2dca3b0f84dc6 Mon Sep 17 00:00:00 2001 From: Mike Thomas Date: Thu, 2 Jun 2011 22:31:44 +0000 Subject: [PATCH] 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 --- src/main/java/com/threerings/geom/Point.java | 24 ++++++ .../java/com/threerings/geom/Rectangle.java | 78 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/main/java/com/threerings/geom/Point.java create mode 100644 src/main/java/com/threerings/geom/Rectangle.java diff --git a/src/main/java/com/threerings/geom/Point.java b/src/main/java/com/threerings/geom/Point.java new file mode 100644 index 000000000..8967e68b6 --- /dev/null +++ b/src/main/java/com/threerings/geom/Point.java @@ -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; + } +} diff --git a/src/main/java/com/threerings/geom/Rectangle.java b/src/main/java/com/threerings/geom/Rectangle.java new file mode 100644 index 000000000..9715f050b --- /dev/null +++ b/src/main/java/com/threerings/geom/Rectangle.java @@ -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; + } +}