From 46e07569494677aebe28944c7ccf383dfd8715d3 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Thu, 5 Sep 2024 23:59:19 +0200 Subject: [PATCH] Fix circular --- src/Tour.cpp | 14 ++++++++++++-- src/tsp.cpp | 6 +++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Tour.cpp b/src/Tour.cpp index 5631c44..1e7a26f 100755 --- a/src/Tour.cpp +++ b/src/Tour.cpp @@ -50,10 +50,10 @@ void Tour::draw(QGraphicsScene *scene) const { return; Node *node = m_startNode; - do { + while (node->next != nullptr) { node->point.drawTo(node->next->point, scene); node = node->next; - } while (node != m_startNode); + } } int Tour::size() const { @@ -89,6 +89,11 @@ void Tour::insertNearest(Point p) { m_startNode = new Node(p); return; } + if (m_startNode->next == nullptr) { + auto newNode = new Node(p, m_startNode); + m_startNode->next = newNode; + return; + } Node *nearest = m_startNode; double minDistance = INFINITY; @@ -112,6 +117,11 @@ void Tour::insertSmallest(Point p) { m_startNode = new Node(p); return; } + if (m_startNode->next == nullptr) { + auto newNode = new Node(p, m_startNode); + m_startNode->next = newNode; + return; + } Node *bestNode = m_startNode; double minIncrease = INFINITY; diff --git a/src/tsp.cpp b/src/tsp.cpp index 566bf7c..e8740ff 100755 --- a/src/tsp.cpp +++ b/src/tsp.cpp @@ -19,6 +19,10 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); string filename = "tsp10.txt"; +#ifdef __APPLE__ + filename.insert(0, "../../../"); +#endif + ifstream input; input.open(filename); @@ -45,7 +49,7 @@ int main(int argc, char *argv[]) { tour.insertNearest(p); //uncomment the 4 lines below to animate //tour.draw(scene); - //std::chrono::milliseconds dura(50); + //std::chrono::milliseconds dura(150); //std::this_thread::sleep_for(dura); //a.processEvents(); }