Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Rectangle {
- private int ix1, iy1, ix2, iy2;
- public Rectangle() {}
- public Rectangle(int x1, int y1, int x2, int y2) {
- ix1 = x1; iy1 = y1; ix2 = x2; iy2 = y2;
- }
- public int getX1() {
- return ix1;
- }
- public int getY1() {
- return iy1;
- }
- public int getX2() {
- return ix2;
- }
- public int getY2() {
- return iy2;
- }
- public int calcArea() {
- return Math.abs((ix2 - ix1) * (iy2 - iy1));
- }
- public int compareTo(Object r) {
- if(this.calcArea() < ((Rectangle)r).calcArea())
- return -1;
- if(this.calcArea() > ((Rectangle)r).calcArea())
- return 1;
- return 0;
- }
- public String toString() {
- return "ix1: " + ix1 + ", iy1: " + iy1 + ", ix2: " + ix2 + ", iy2: " + iy2;
- }
- public boolean equals(Rectangle r) {
- return this.calcArea() == r.calcArea();
- }
- public void translateX(int iPoints) {
- ix1 += iPoints;
- ix2 += iPoints;
- }
- public void translateY(int iPoints) {
- iy1 += iPoints;
- iy2 += iPoints;
- }
- public void translateXY(int iPoints) {
- translateX(iPoints);
- translateY(iPoints);
- }
- public boolean isInside(int ptx, int pty) {
- return (ix1 < ptx && ix2 > ptx && iy1 < pty && iy2 > pty);
- }
- public Rectangle unionRect(Rectangle r) {
- int x1 = Math.min(ix1, r.ix1);
- int y1 = Math.min(iy1, r.iy1);
- int x2 = Math.max(ix2, r.ix2);
- int y2 = Math.max(iy2, r.iy2);
- return new Rectangle(x1, y1, x2, y2);
- }
- public Rectangle intersectionRect(Rectangle r) {
- int x1 = Math.max(ix1, r.ix1);
- int y1 = Math.max(iy1, r.iy1);
- int x2 = Math.min(ix2, r.ix2);
- int y2 = Math.min(iy2, r.iy2);
- return new Rectangle(x1, y1, x2, y2);
- }
- public static void main(String[] args) {
- Rectangle rect = new Rectangle(2,2,10,10);
- System.out.println(rect.toString());
- Rectangle rect2 = new Rectangle(5,5,8,8);
- System.out.println(rect2.toString());
- if(rect.equals(rect2))
- System.out.println("rect == rect2");
- else System.out.println("rect != rect2");
- if(rect.compareTo(rect2) == -1)
- System.out.println("rect < rect2");
- else if(rect.compareTo(rect2) == 1) System.out.println("rect > rect2");
- else System.out.println("rect == rect2");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement