forked from careercup/ctci
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion.java
More file actions
83 lines (65 loc) · 2.7 KB
/
Question.java
File metadata and controls
83 lines (65 loc) · 2.7 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package Question7_5;
public class Question {
public static int randomInt(int n) {
return (int) (Math.random() * n);
}
public static void printLine(Line l) {
System.out.println(l.start.x + "\t" + l.start.y);
System.out.println(l.end.x + "\t" + l.end.y);
}
public static void printSquare(Square s) {
System.out.println(s.left + "\t" + s.top + "\t" + s.size);
}
public static boolean isApproxEqual(double d1, double d2) {
double epsilon = .001;
if (Math.abs(d1 - d2) < epsilon) {
return true;
}
return false;
}
public static boolean isApproxEqual(Point p1, Point p2) {
return isApproxEqual(p1.x, p2.x) && isApproxEqual(p1.y, p2.y);
}
public static boolean doTest(Square s1, Square s2, Point start, Point end) {
Line line = s1.cut(s2);
boolean r = (isApproxEqual(line.start, start) && isApproxEqual(line.end, end)) || (isApproxEqual(line.start, end) && isApproxEqual(line.end, start));
if (!r) {
printSquare(s1);
printSquare(s2);
printLine(line);
System.out.println(start.toString());
System.out.println(end.toString());
System.out.println();
return r;
}
return r;
}
public static boolean doTestFull(Square s1, Square s2, Point start, Point end) {
return doTest(s1, s2, start, end) && doTest(s2, s1, start, end);
}
public static void doTests() {
// Equal
doTestFull(new Square(1, 1, 5), new Square(1, 1, 5), new Point(3.5, 1), new Point(3.5, 6));
// Concentric
doTestFull(new Square(1, 1, 5), new Square(2, 2, 3), new Point(3.5, 1), new Point(3.5, 6));
// Partially overlapping -- side by side
doTestFull(new Square(10, 10, 10), new Square(8, 10, 10), new Point(8, 15), new Point(20, 15));
// Partially overlapping -- corners
doTestFull(new Square(10, 10, 10), new Square(8, 7, 7), new Point(8.777777, 7), new Point(18.8888888, 20));
// Partially overlapping -- on top of each other
doTestFull(new Square(10, 10, 10), new Square(8, 7, 15), new Point(8, 22), new Point(23, 7));
// Not overlapping -- side by side
doTestFull(new Square(10, 10, 10), new Square(19, 25, 4), new Point(12.5, 10), new Point(22, 29));
// Not overlapping -- on top of each other
doTestFull(new Square(10, 10, 10), new Square(4, 4, 3), new Point(4, 4), new Point(20, 20));
// Contained
doTestFull(new Square(10, 10, 10), new Square(12, 14, 3), new Point(10, 16.66666), new Point(20, 13.333));
}
public static void main(String[] args) {
/* For an easy way to test these, open up Square Cut Tester.xlsx
* in the Chapter 7, Problem 5 folder. Copy and paste the exact
* output from below into the file (including all tabs).
*/
doTests();
}
}