diff options
| author | Gustav Sörnäs <gusso230@student.liu.se> | 2020-08-17 19:34:09 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gusso230@student.liu.se> | 2020-08-17 19:34:09 +0200 |
| commit | 403ce5391e0fecc51bf50c86070ebc0e6d7e05af (patch) | |
| tree | 248c47b8687baa820d4803784c15a2a6d43405c4 /labb3/tsp/src/tsp.cpp | |
| parent | c0a7e9ae20f29bb5d344cbb3923e0967034cadf1 (diff) | |
| download | tddd86-L3.tar.gz | |
add tsp codeL3
Diffstat (limited to 'labb3/tsp/src/tsp.cpp')
| -rw-r--r-- | labb3/tsp/src/tsp.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/labb3/tsp/src/tsp.cpp b/labb3/tsp/src/tsp.cpp new file mode 100644 index 0000000..297cc26 --- /dev/null +++ b/labb3/tsp/src/tsp.cpp @@ -0,0 +1,63 @@ +/* + * TDDD86 TSP + * This client program uses your Tour class and contains the 'main' + * function to open the input file and set up the program's primitive GUI. + */ + +#include <QApplication> +#include <QGraphicsView> +#include <QGraphicsScene> +#include <chrono> +#include <thread> +#include <fstream> +#include <iostream> +#include <iomanip> +#include "Point.h" +#include "Tour.h" + +int main(int argc, char *argv[]) { + QApplication a(argc, argv); + + string filename = "tsp100.txt"; + ifstream input; + input.open(filename); + + // get dimensions + int width; + int height; + input >> width; + input >> height; + + // setup graphical window + QGraphicsView *view = new QGraphicsView(); + QGraphicsScene *scene = new QGraphicsScene(); + view->setScene(scene); + view->scale(1, -1); //screen y-axis is inverted + view->setSceneRect(0, 0, width, height); + view->show(); + + // run insertion heuristic + Tour tour; + double x; + double y; + while (input >> x >> y) { + Point p(x, y); + tour.insertNearest(p); + // uncomment the 4 lines below to animate + // tour.draw(scene); + // std::chrono::milliseconds dura(50); + // std::this_thread::sleep_for(dura); + // a.processEvents(); + } + input.close(); + + // print tour to standard output + cout << "Tour distance: " << std::fixed << std::setprecision(4) + << std::showpoint << tour.distance() << endl; + cout << "Number of points: " << tour.size() << endl; + tour.show(); + + // draw tour + tour.draw(scene); + return a.exec(); // start Qt event loop +} |
