-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreenLight.java
More file actions
90 lines (62 loc) · 2.49 KB
/
GreenLight.java
File metadata and controls
90 lines (62 loc) · 2.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cas2xb3.greenlight;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import org.json.JSONException;
public class GreenLight {
public static void main(String[] args) throws IOException {
double originLat = 0;
double originLng = 0;
double destLat = 0;
double destLng = 0;
String origin = null;
String destination = null;
// Initialize and sort the collision array:
Collision[] collisions = Data.getArray();
Heap.sort(collisions);
// Take the input from the user:
try {
Scanner sc = new Scanner(System.in);
// Get the origin and turn it into lat/lng points:
System.out.println("Enter the origin: ");
origin = sc.nextLine();
GeoCodingSample.getCoordinates(origin);
originLat = GeoCodingSample.getLatitude();
originLng = GeoCodingSample.getlongitude();
// Get the destination and turn it into lat/lng points:
System.out.println("Enter the destination: ");
destination = sc.nextLine();
GeoCodingSample.getCoordinates(destination);
destLat = GeoCodingSample.getLatitude();
destLng = GeoCodingSample.getlongitude();
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Find the collisions closest to the origin and destination:
int originPointIndex = ClosestPoint.closest(collisions, originLat, originLng);
int destPointIndex = ClosestPoint.closest(collisions, destLat, destLng);
System.out.println(originPointIndex);
System.out.println(destPointIndex);
// Build the graph
EdgeWeightedDigraph G = CrashGraph.FinalGraph();
// Then find the shortest path from the origin to the destination:
DijkstraSP sp = new DijkstraSP(G, originPointIndex);
// Make the array of the collisions:
ArrayList<Collision> tempCollisionPath = new ArrayList<Collision>();
tempCollisionPath.add(collisions[originPointIndex]);
if (sp.hasPathTo(destPointIndex)) {
System.out.printf("%s to %s (%.2f) ", origin, destination, sp.distTo(destPointIndex));
for (DirectedEdge e : sp.pathTo(destPointIndex)) {
tempCollisionPath.add(collisions[e.to()]);
}
}
Object[] collisionPath = tempCollisionPath.toArray();
for (int i = 0; i < collisionPath.length; i++){
System.out.println(((Collision) collisionPath[i]).getLat() + " , " + ((Collision) collisionPath[i]).getLng());
}
}
}