-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathDrawingEngine.java
executable file
·63 lines (48 loc) · 1.61 KB
/
DrawingEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package tutorialquestions.questionc2b8.beforerefactoring;
import java.util.HashSet;
import java.util.Set;
public class DrawingEngine {
private final Set<Rectangle> rectangles;
public DrawingEngine() {
rectangles = new HashSet<>();
}
public void addRectangle(Rectangle rectangle) {
rectangles.add(rectangle);
}
public int area(Rectangle rectangle) {
return rectangle.getWidth() * rectangle.getHeight();
}
public boolean contains(Rectangle r1, Rectangle r2) {
return
r1.getTopLeft().getCoordX() <= r2.getTopLeft().getCoordX()
&& r1.getTopLeft().getCoordY() <= r2.getTopLeft().getCoordY()
&& getBottomRight(r1).getCoordX() >= getBottomRight(r2).getCoordX()
&& getBottomRight(r1).getCoordY() >= getBottomRight(r2).getCoordY();
}
private Point getBottomRight(Rectangle r) {
return new Point(
r.getTopLeft().getCoordX() + r.getWidth(),
r.getTopLeft().getCoordY() + r.getHeight()
);
}
public int maxArea() {
int result = 0;
for (Rectangle r : rectangles) {
if (area(r) > result) {
result = area(r);
}
}
return result;
}
public String toString() {
final StringBuilder result = new StringBuilder("Drawing engine is looking after these rectangles:");
for (Rectangle r : rectangles) {
result.append("\n ").append(rectangleToString(r));
}
return result.toString();
}
public String rectangleToString(Rectangle rectangle) {
return "(top-left = " + rectangle.getTopLeft() + ", width = " + rectangle.getWidth()
+ ", height = " + rectangle.getHeight() + ")";
}
}