Fix circular

This commit is contained in:
Love 2024-09-05 23:59:19 +02:00
parent 95e06a3f18
commit 46e0756949
2 changed files with 17 additions and 3 deletions

View File

@ -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;

View File

@ -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();
}