-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.java
More file actions
139 lines (127 loc) · 4.5 KB
/
Copy pathFunctions.java
File metadata and controls
139 lines (127 loc) · 4.5 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package hw8;
import java.util.*;
import hw5.Edge;
import hw5.Graph;
/**
* Functions has the ability to do various tasks like printing the menu, finding
* path between two buildings, and listing buildings.
*
*
* @author RibhavHora
*/
public class Functions {
// This class does not represent an ADT.
/**
* Prints the menu of functions
*
* @effects Prints the menu of functions
*/
public static void menu() {
System.out.println("Menu: ");
System.out.println("\tr to find a route");
System.out.println("\tb to see a list of all buildings");
System.out.println("\tq to quit");
System.out.println();
}
/**
* Prints the shortest path between two buildings
*
* @param console
* the Scanner which is used to take in input
* @effects Prints the shortest path between two buildings on UW campus in
* the format - Walk ___ feet N to this location.
*/
public static void findPath(Scanner console) {
System.out.print("Abbreviated name of starting building: ");
String startNode = console.nextLine();
Buildings start = CampusMain.fullBuilding(startNode, "src/hw8/data/campus_buildings.dat");
System.out.print("Abbreviated name of ending building: ");
String endNode = console.nextLine();
Buildings end = CampusMain.fullBuilding(endNode, "src/hw8/data/campus_buildings.dat");
Graph<Buildings, Double> graph = new Graph<Buildings, Double>();
try {
graph = PathParser.parsePathData("src/hw8/data/campus_buildings.dat", "src/hw8/data/campus_paths.dat");
} catch (Exception e) {
System.err.println("Caught Exceptionnn: " + e.toString());
}
List<Edge<Buildings, Double>> edges = CampusMain.findPath(start, end, graph);
if (!graph.checkNode(start) || !graph.checkNode(end)) {
if (!graph.checkNode(start))
System.out.println("Unknown building: " + startNode);
if (!graph.checkNode(end))
System.out.println("Unknown building: " + endNode);
} else {
System.out.println("Path from " + start.getLong() + " to " + end.getLong() + ":");
if (edges == null) {
System.out.println("no path found");
} else if (edges.isEmpty()) {
System.out.println("Total distance: 0 feet");
} else {
double totalFeet = 0.0;
for (Edge<Buildings, Double> edge : edges) {
System.out.println("\tWalk " + Math.round(edge.getEdgeLabel()) + " feet " + degreesFinder(edge)
+ " to " + edge.getEndNode());
totalFeet += edge.getEdgeLabel();
}
System.out.println("Total distance: " + Math.round(totalFeet) + " feet");
}
}
System.out.println();
}
/**
* Finds the direction between two points on the map
*
* @param edge
* the edge whose starting and ending is used
*
* @returns a string which shows the direction in going from the starting
* edge to the ending edge
*/
private static String degreesFinder(Edge<Buildings, Double> edge) {
Buildings b1 = edge.getStartNode();
Buildings b2 = edge.getEndNode();
Double degrees = Math.atan2(b1.getY() - b2.getY(), b2.getX() - b1.getX());
if (degrees > (Math.PI / 8) && degrees < ((3 * Math.PI) / 8))
return "NE";
else if (degrees >= ((3 * Math.PI) / 8) && degrees <= ((5 * Math.PI) / 8))
return "N";
else if (degrees > ((5 * Math.PI) / 8) && degrees < ((7 * Math.PI) / 8))
return "NW";
else if (degrees >= ((Math.PI) / -8) && degrees <= ((Math.PI) / 8))
return "E";
else if (degrees > ((-7 * Math.PI) / 8) && degrees < ((-5 * Math.PI) / 8))
return "SW";
else if (degrees > ((-5 * Math.PI) / 8) && degrees < ((-3 * Math.PI) / 8))
return "S";
else if (degrees > ((-3 * Math.PI) / 8) && degrees < ((Math.PI) / -8))
return "SE";
else
return "W";
}
/**
* Prints a list of all the buildings on UW campus
*
* @returns Prints a list of all the buildings on UW campus
*/
public static void listBuildings() {
Set<Buildings> nameBuildings = new HashSet<Buildings>();
Map<String, Buildings> shorts = new HashMap<String, Buildings>();
try {
PathParser.parseBuildingData("src/hw8/data/campus_buildings.dat", nameBuildings, shorts);
} catch (Exception e) {
System.err.println(e.toString());
e.printStackTrace(System.err);
}
List<Buildings> sortedList = new ArrayList<Buildings>(nameBuildings);
Collections.sort(sortedList, new Comparator<Buildings>() {
public int compare(Buildings b1, Buildings b2) {
return b1.getShort().compareTo(b2.getShort());
}
});
System.out.println("Buildings:");
for (Buildings b : sortedList) {
System.out.println("\t" + b.getShort() + ": " + b.getLong());
}
System.out.println();
}
}