-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProposedAlgorithm.java
More file actions
62 lines (55 loc) · 1.49 KB
/
ProposedAlgorithm.java
File metadata and controls
62 lines (55 loc) · 1.49 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
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class ProposedAlgorithm {
private BPTree tree;
private Event[] events;
public ProposedAlgorithm(Line[] lines) {
this(lines, true);
}
public ProposedAlgorithm(Line[] lines, boolean hasDupes) {
this.tree = new BPTree(hasDupes);
this.events = createEvents(lines);
Arrays.sort(events);
}
public Event[] createEvents(Line[] l) {
Event[] evts = new Event[l.length*3/2];
int i = 0;
for (Line currentLine: l) {
if (currentLine.isVertical()) {
evts[i] = new Event(currentLine, true);
i++;
evts[i] = new Event(currentLine, false);
i++;
}
else {
evts[i] = new Event(currentLine, true); //it doesn't really matter what the second parameter is for a horizontal line
i++;
}
}
return evts;
}
public LinkedList<Point> findIntersects() {
LinkedList<Point> list = new LinkedList<Point>();
for (int i = 0; i < events.length; i++) {
Event curEvent = events[i];
if (curEvent.isVertical()) {
VerticalLine vertLine = new VerticalLine(curEvent.getLine());
KVPair<VerticalLine> lineWrapper = new KVPair<>(vertLine);
if (curEvent.isLesser()) {
tree.insert(lineWrapper);
}
else {
tree.delete(lineWrapper);
}
}
else {
List<Point> intersections = tree.findRange(curEvent.getLine().getLesser().getX(),
curEvent.getLine().getGreater().getX(),
curEvent.getY());
list.addAll(intersections);
}
}
return list;
}
}