-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectTest.java
More file actions
68 lines (61 loc) · 1.58 KB
/
RectTest.java
File metadata and controls
68 lines (61 loc) · 1.58 KB
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
64
65
66
67
68
public class RectTest {
static class Rect {
int left;
int right;
int top;
int bottom;
public String toString() {
return "[" + left + " " + right + " " + top + " " + bottom + "]";
}
}
public static void main(String[] a) {
Rect[] rects = genRects(5, 5);
p(rects);
p(naiveShadow(rects, 5) + "\n");
}
static <E> void p(E[] array) {
p("(");
for (E elem : array) {
p(elem.toString());
p(" ");
}
p(")");
p("\n");
}
static void p(String s) {
System.out.print(s);
}
static Rect[] genRects(int num, int scale) {
Rect[] rects = new Rect[num];
for (int i = 0; i < num; i++) {
rects[i] = new Rect();
rects[i].left = (int) (Math.random() * scale);
rects[i].right = ((int) (Math.random() * (scale - rects[i].left - 1))) + rects[i].left + 1;
rects[i].top = (int) (Math.random() * scale);
rects[i].bottom = ((int) (Math.random() * (scale - rects[i].top - 1))) + rects[i].top + 1;
}
return rects;
}
static int naiveShadow(Rect[] rects, int scale) {
boolean[][] grid = new boolean[scale][scale];
for (int i = 0; i < scale; i++) {
for (int j = 0; j < scale; j++) {
grid[i][j] = false;
}
}
for (Rect thisRect : rects) {
for (int i = thisRect.left; i < thisRect.right; i++) {
for (int j = thisRect.top; j < thisRect.bottom; j++) {
grid[i][j] = true;
}
}
}
int total = 0;
for (boolean[] row : grid) {
for (boolean val : row) {
total += val ? 1 : 0;
}
}
return total;
}
}