-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectingLines.java
More file actions
74 lines (63 loc) · 2.27 KB
/
IntersectingLines.java
File metadata and controls
74 lines (63 loc) · 2.27 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
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* @author Anuj, Sam, and Max
*
*/
public class IntersectingLines {
/**
* @param args
*/
public static void main(String[] args) {
int[] lineSegments = {10, 100, 500, 1000, 2000, 10000, 20000};
System.out.println("Line Segments (n) \tBrute Force \tOur Algorithm \tIntersections");
for (int i = 0; i < lineSegments.length; i++) {
RandomLines r = new RandomLines(lineSegments[i]);
Line[] lines = r.lines();
Result res1 = bruteForce(lines);
long totalTime1 = res1.getTotalTime();
Result res2 = proposedAlgorithm(lines, containsDuplicate(res1));
long totalTime2 = res2.getTotalTime();
System.out.println(lineSegments[i] + "\t\t\t" + totalTime1 + "\t\t"
+ totalTime2 + "\t\t" +
res1.getIntersections() + res2.getIntersections());
}
}
private static boolean containsDuplicate(Result r) {
List<Point> pointList = r.getIntersections();
Point[] points = new Point[pointList.size()];
points = pointList.toArray(points);
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
if (points[i].getX() == points[j].getX() && points[i].getY() == points[j].getY())
return true;
}
}
return false;
}
private static boolean containsDuplicate(Line[] lines) {
for (int i = 0; i < lines.length; i++) {
for (int j = i + 1; j < lines.length; j++) {
if (lines[i].isVertical() && lines[j].isVertical() && lines[i].getGreater().getX() == lines[j].getGreater().getX())
return true;
}
}
return false;
}
private static Result bruteForce(Line[] lines) {
long startTime = System.currentTimeMillis();
BruteForceAlgorithm bruteForce = new BruteForceAlgorithm(lines);
List<Point> intersections = bruteForce.findIntersects();
long endTime = System.currentTimeMillis();
return new Result(endTime - startTime, intersections);
}
private static Result proposedAlgorithm(Line[] lines, boolean hasDupes) {
long startTime = System.currentTimeMillis();
Arrays.sort(lines);
ProposedAlgorithm proposed = new ProposedAlgorithm(lines, hasDupes);
List<Point> intersections = proposed.findIntersects();
long endTime = System.currentTimeMillis();
return new Result(endTime - startTime, intersections);
}
}