tsp/src/tsp.cpp
2024-09-06 16:16:29 +02:00

70 lines
1.8 KiB
C++
Executable File

/*
* 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 = "tsp1000.txt";
#ifdef __APPLE__
// On macOS the resources are copeied to build/<release-target>/
// whilst, the running binary is under build/<release-target>/TSP.app/Contents/MacOS/
filename.insert(0, "../../../");
#endif
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.insertSmallest(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
}