From 403ce5391e0fecc51bf50c86070ebc0e6d7e05af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 17 Aug 2020 19:34:09 +0200 Subject: add tsp code --- labb3/tsp/src/tsp.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 labb3/tsp/src/tsp.cpp (limited to 'labb3/tsp/src/tsp.cpp') 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 +#include +#include +#include +#include +#include +#include +#include +#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 +} -- cgit v1.2.1