Fix circular

This commit is contained in:
Love 2024-09-05 23:59:19 +02:00
parent a60df7848d
commit 93553a8081
2 changed files with 17 additions and 3 deletions

View File

@ -50,10 +50,10 @@ void Tour::draw(QGraphicsScene *scene) const {
return; return;
Node *node = m_startNode; Node *node = m_startNode;
do { while (node->next != nullptr) {
node->point.drawTo(node->next->point, scene); node->point.drawTo(node->next->point, scene);
node = node->next; node = node->next;
} while (node != m_startNode); }
} }
int Tour::size() const { int Tour::size() const {
@ -89,6 +89,11 @@ void Tour::insertNearest(Point p) {
m_startNode = new Node(p); m_startNode = new Node(p);
return; return;
} }
if (m_startNode->next == nullptr) {
auto newNode = new Node(p, m_startNode);
m_startNode->next = newNode;
return;
}
Node *nearest = m_startNode; Node *nearest = m_startNode;
double minDistance = INFINITY; double minDistance = INFINITY;
@ -112,6 +117,11 @@ void Tour::insertSmallest(Point p) {
m_startNode = new Node(p); m_startNode = new Node(p);
return; return;
} }
if (m_startNode->next == nullptr) {
auto newNode = new Node(p, m_startNode);
m_startNode->next = newNode;
return;
}
Node *bestNode = m_startNode; Node *bestNode = m_startNode;
double minIncrease = INFINITY; double minIncrease = INFINITY;

View File

@ -19,6 +19,10 @@ int main(int argc, char *argv[]) {
QApplication a(argc, argv); QApplication a(argc, argv);
string filename = "tsp10.txt"; string filename = "tsp10.txt";
#ifdef __APPLE__
filename.insert(0, "../../../");
#endif
ifstream input; ifstream input;
input.open(filename); input.open(filename);
@ -45,7 +49,7 @@ int main(int argc, char *argv[]) {
tour.insertNearest(p); tour.insertNearest(p);
//uncomment the 4 lines below to animate //uncomment the 4 lines below to animate
//tour.draw(scene); //tour.draw(scene);
//std::chrono::milliseconds dura(50); //std::chrono::milliseconds dura(150);
//std::this_thread::sleep_for(dura); //std::this_thread::sleep_for(dura);
//a.processEvents(); //a.processEvents();
} }