Region.java
package ac.essex.ooechs.imaging.commons.util;
import java.awt.event.MouseEvent;
import java.awt.*;
/**
* <p/>
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version,
* provided that any use properly credits the author.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details at http://www.gnu.org
* </p>
*
* @author Olly Oechsle, University of Essex, Date: 17-Oct-2006
* @version 1.0
*/
public class Region {
public int x1;
public int y1;
public int x2;
public int y2;
public Region(int x1, int y1) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x1;
this.y2 = y1;
}
public Region(int x1, int y1, int width, int height) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x1 + width;
this.y2 = y1 + height;
}
public Region(Region r) {
this.x1 = r.x1;
this.y1 = r.y1;
this.x2 = r.x2;
this.y2 = r.y2;
}
public boolean contains(MouseEvent e) {
return contains(e.getX(), e.getY());
}
/**
* Determines if a point (x, y) is contains this rectangle.
*/
public boolean contains(int x, int y) {
return (x >= getStartX() && x <= (getStartX() + getWidth()) && y >= getStartY() && y <= (getStartY() + getHeight()));
}
/**
* Determines if another rectangle is contained fully inside this one
*/
public boolean contains(Region other) {
int x1 = other.getStartX();
int y1 = other.getStartY();
int x2 = x1 + other.getWidth();
int y2 = y1 + other.getWidth();
return contains(x1, y1) && contains(x2, y2);
}
/**
* Determines the percentage of another rectangle within this rectangle
*/
public double overlap(Region other) {
int pixelCount = 0;
for (int y = other.getStartY(); y < (other.getStartY() + other.getHeight()); y++) {
for (int x = other.getStartX(); x < (other.getStartX() + other.getWidth()); x++) {
if (contains(x, y)) pixelCount++;
}
}
return pixelCount / (double) other.getArea();
}
public int getArea() {
return getWidth() * getHeight();
}
public int getWidth() {
return Math.abs(x2 - x1);
}
public int getHeight() {
return Math.abs(y2 - y1);
}
public int getStartX() {
return Math.min(x1, x2);
}
public int getStartY() {
return Math.min(y1, y2);
}
public void move(Region r, int dX, int dY) {
this.x1 = r.x1 + dX;
this.x2 = r.x2 + dX;
this.y1 = r.y1 + dY;
this.y2 = r.y2 + dY;
}
public void move(int dX, int dY) {
this.x1 += dX;
this.x2 += dX;
this.y1 += dY;
this.y2 += dY;
}
public void draw(Graphics g) {
g.drawRect(getStartX(), getStartY(), getWidth(), getHeight());
}
public void fill(Graphics g) {
g.fillRect(getStartX(), getStartY(), getWidth(), getHeight());
}
public void setWidth(int width) {
if (x2 > x1) {
x2 = x1 + width;
} else {
x2 = x1 - width;
}
}
public void setHeight(int height) {
if (y2 > y1) {
y2 = y1 + height;
} else {
y2 = y1 - height;
}
}
protected int hits;
public int getHits() {
return hits;
}
public void incrementHits() {
hits++;
}
public void setHits(int hits) {
this.hits = hits;
}
}