Created
July 21, 2015 17:46
-
-
Save fever324/ca2a6a69b4748b564d92 to your computer and use it in GitHub Desktop.
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Rectangle Area Assume that the total area is never beyond the maximum possible value of int.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { | |
| int areaA = (D - B) * (C - A); | |
| int areaB = (H - F) * (G - E); | |
| Point a = new Point(A,B); | |
| Point b = new Point(C,D); | |
| Point c = new Point(E,F); | |
| Point d = new Point(G,H); | |
| Rectangle x = new Rectangle(a,b); | |
| Rectangle y = new Rectangle(c,d); | |
| if(noOverlap(x,y)) { | |
| return areaA+areaB; | |
| } else { | |
| Point xBottomRight = new Point(C, B); | |
| Point yBottomRight = new Point(G, F); | |
| Line bottomX = new Line(a, xBottomRight); | |
| Line bottomY = new Line(c, yBottomRight); | |
| Line rightX = new Line(b, xBottomRight); | |
| Line rightY = new Line(d, yBottomRight); | |
| return areaA + areaB - bottomX.overLapLengthWith(bottomY) * rightX.overLapLengthWith(rightY); | |
| } | |
| } | |
| private boolean noOverlap(Rectangle a, Rectangle b) { | |
| return (a.r.y <= b.l.y) || (a.l.y >= b.r.y) || (a.r.x <= b.l.x) || (a.l.x >= b.r.x); | |
| } | |
| private class Line { | |
| // Horizontal and verticle lines only | |
| // a is always left or top | |
| public Point a; | |
| public Point b; | |
| public Line(Point a, Point b) { | |
| this.a = a; | |
| this.b = b; | |
| } | |
| public int overLapLengthWith(Line l) { | |
| if (isHorizontal()) { | |
| if(this.b.x <= l.a.x || this.a.x >= l.b.x) { | |
| return 0; | |
| } | |
| if(this.a.x <= l.a.x) { | |
| if(this.b.x <= l.b.x) { | |
| return this.b.x - l.a.x; | |
| } else { | |
| return l.b.x - l.a.x; | |
| } | |
| } else { | |
| if(this.b.x <= l.b.x) { | |
| return this.b.x - this.a.x; | |
| } else { | |
| return l.b.x - this.a.x; | |
| } | |
| } | |
| } else { | |
| if (this.b.y >= l.a.y || this.a.y <= this.b.y) { | |
| return 0; | |
| } | |
| if(this.a.y >= l.a.y) { | |
| return this.b.y >= l.b.y ? l.a.y - this.b.y : l.a.y - l.b.y; | |
| } else { | |
| if(this.b.y <= l.b.y) { | |
| return this.a.y - l.b.y; | |
| } else { | |
| return this.a.y - this.b.y; | |
| } | |
| } | |
| } | |
| } | |
| private boolean isHorizontal() { | |
| return a.y == b.y; | |
| } | |
| } | |
| private class Rectangle { | |
| public Point l; | |
| public Point r; | |
| public Rectangle(Point l, Point r) { | |
| this.l = l; | |
| this.r = r; | |
| } | |
| } | |
| private class Point { | |
| public int x; | |
| public int y; | |
| public Point(int x, int y) { | |
| this.x = x; | |
| this.y = y; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment