-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplate.cpp
More file actions
85 lines (68 loc) · 4.88 KB
/
template.cpp
File metadata and controls
85 lines (68 loc) · 4.88 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
#include <string> // C++ standard string class - used to store file names and read text data
#include <sstream> // stringstreams make it easier to read data from strings
#include <vector> // contains the std::vector class, which is an easy-to-use array class
#include <matplot/matplot.h> // Matplot++ library (uses GNUplot)
/**
* Loads a "gravity" scene composed of black holes with (x, y) positions and associated masses (in kilograms). The
* file is composed of three numbers on each line separated by white space:
* x1.xxx y1.yyy m1.mmm
* x2.xxx y2.yyy m2.mmm
* ...
* xn.xxx yn.yyy mn.mmm
*
* @param filename is the name of the text file to be loaded as an std::sting
* @param Gx is an std::vector<double> that will store the X coordinates for each gravity well
* @param Gy is an std::vector<double> that will store the Y coordinates for each gravity well
* @param Gm is an std::vector<double> that will store the masses of each gravity well
*/
void load_scene(const std::string filename, std::vector<double> &Gx, std::vector<double> &Gy, std::vector<double> &Gm) {
// open the file for reading
std::ifstream file(filename); // create a file stream associated with the file
if (!file.is_open()) // if the file was not successfully opened, throw a runtime error
throw std::runtime_error("Could not open file " + filename);
// The arrays are passed by reference and may not be empty, so clear all of them
Gx.clear();
Gy.clear();
Gm.clear();
// read each line of the file and store the coordinates and masses for each gravity well in the passed arrays
while (!file.eof()) { // while we are not at the end of the file
std::string line; // create a string that holds a single line entry from the file
std::getline(file, line); // read a line from the file and store it in the string "line"
if (line.empty()) break; // if "line" is empty (if the last line in the file is blank), break out of the loop
std::stringstream ss(line); // create a stringstream from the line (makes it easier to read numbers)
double x, y, m; // allocate temporary variables to store the (x,y) coordinates and mass m
ss >> x; // read numerical values from the string stream
ss >> y;
ss >> m;
Gx.push_back(x); // push the associated values into the array
Gy.push_back(y);
Gm.push_back(m);
}
// at this point the function returns and the values of Gx, Gy, and Gm contain the positions and masses of each gravity well
}
int main(const int argc, char *argv[]) {
// make sure that the appropriate input is provided (input file containing gravity wells, and output file for plotting)
if (argc <= 2) { // if fewer than 2 arguments are provided
std::cout<<"An input and output file name are required. For example:"<<std::endl; // give an example for the input and exit
std::cout<<"kesselrun /scenes/gravity_10.txt gravity_10.pdf"<<std::endl;
return 1; // return 1 (indicating an error)
}
std::string in_filename(argv[1]); // get the input file name as a string from the command line
std::string out_filename(argv[2]); // get the output file name as a string from the command line
std::vector<double> Gx; // create three std::vector<double> arrays to store the gravity well positions and masses
std::vector<double> Gy;
std::vector<double> Gm;
load_scene(in_filename, Gx, Gy, Gm); // call the function to load the scene file and fill the gravity well arrays
// To Do: calculate the shortest and longest paths
// This is the main part of your assignment. Your goal is to fill std::vector<double> arrays with the following:
// 1) The (x, y) coordinates for the shortest path through the scene
// 2) The (x, y) coordinates for the longest path through the scene
// output the lengths of the shortest and longest paths in parsecs
std::cout << "Shortest Path: " << "(TO DO: output shortest path length)" << " parsecs" << std::endl;
std::cout << "Longest Path: " << "(TO DO: output longest path length)" << " parsecs" << std::endl;
// plot the gravity wells and paths, saving the results to the specified output file (probably a PDF)
matplot::axis({-10, 10, 0, 10}); // set the axes so that the entire game field is shown
matplot::scatter(Gx, Gy); // plot the gravity wells as small circles
matplot::save(out_filename); // save the plot using the user-specified output file name
return 0; // return 0, indicating success
}