Skip to content

Instantly share code, notes, and snippets.

@kien-truong
Forked from ducngtuan/Rectangle.java
Last active December 15, 2015 03:39
Show Gist options
  • Save kien-truong/5196438 to your computer and use it in GitHub Desktop.
Save kien-truong/5196438 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Rectangle {
public static class Point {
public float x, y;
public Point(float x, float y) {
this.x = x;
this.y = y;
}
public Point shift(float dx, float dy) {
this.x += dx;
this.y += dy;
return this;
}
@Override
public String toString() {
return x + ", " + y;
}
}
public final Point topLeft, bottomRight;
public Rectangle(Point topLeft, Point bottomRight) throws Exception {
if(topLeft.x >= bottomRight.x || topLeft.y <= bottomRight.y) {
throw new Exception("Illegal rectangle");
}
this.topLeft = topLeft;
this.bottomRight = bottomRight;
}
public Rectangle unify(Rectangle other) throws Exception {
float x0 = Math.min(topLeft.x, other.topLeft.x);
float y0 = Math.max(topLeft.y, other.topLeft.y);
float x1 = Math.max(bottomRight.x, other.bottomRight.x);
float y1 = Math.min(bottomRight.y, other.bottomRight.y);
return new Rectangle(new Point(x0, y0), new Point(x1, y1));
}
public boolean contains(Point p) {
if (p.x >= topLeft.x && p.x <= bottomRight.x
&& p.y <= topLeft.y && p.y >= bottomRight.y) {
return true;
}
return false;
}
public Rectangle shift(float dx, float dy) {
topLeft.shift(dx, dy);
bottomRight.shift(dx, dy);
return this;
}
@Override
public String toString() {
return "Rect(" + topLeft.toString() + ", " + bottomRight.toString() + ")";
}
public Rectangle intersection(Rectangle other) throws Exception {
float x0 = Math.max(topLeft.x, other.topLeft.x);
float y0 = Math.min(topLeft.y, other.topLeft.y);
float x1 = Math.min(bottomRight.x, other.bottomRight.x);
float y1 = Math.max(bottomRight.y, other.bottomRight.y);
return new Rectangle(new Point(x0, y0), new Point(x1, y1));
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
Rectangle r1 = inputRectangle(scanner);
System.out.println("Rectangle 1: " + r1.toString());
Rectangle r2 = inputRectangle(scanner);
System.out.println("Rectangle 2: " + r2.toString());
try {
System.out.println("Unification: " + r1.unify(r2).toString());
} catch (Exception e) {
System.out.println("No unification");
}
try {
System.out.println("Intersection: " + r1.intersection(r2).toString());
} catch (Exception e) {
System.out.println("No intersection");
}
System.out.print("Enter a point: ");
Point p = new Point(scanner.nextFloat(), scanner.nextFloat());
System.out.println("Point is" + (r1.contains(p) ? "" : " NOT") + " in rectangle 1");
System.out.println("Point is" + (r2.contains(p) ? "" : " NOT") + " in rectangle 2");
System.out.print("Enter a vector to shift: ");
float dx = scanner.nextFloat();
float dy = scanner.nextFloat();
System.out.println("Result 1: " + r1.shift(dx, dy).toString());
System.out.println("Result 2: " + r2.shift(dx, dy).toString());
}
private static Rectangle inputRectangle(Scanner scanner) throws Exception {
System.out.println("Enter rectangle:");
System.out.print(" top left point: ");
float x0 = scanner.nextFloat();
float y0 = scanner.nextFloat();
System.out.print(" bottom right point: ");
float x1 = scanner.nextFloat();
float y1 = scanner.nextFloat();
return new Rectangle(new Point(x0, y0), new Point(x1, y1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment