diff options
| author | Gustav Sörnäs <gusso230@student.liu.se> | 2020-08-17 19:34:09 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gusso230@student.liu.se> | 2020-08-17 19:34:09 +0200 |
| commit | 403ce5391e0fecc51bf50c86070ebc0e6d7e05af (patch) | |
| tree | 248c47b8687baa820d4803784c15a2a6d43405c4 /labb3/tsp/src/Tour.cpp | |
| parent | c0a7e9ae20f29bb5d344cbb3923e0967034cadf1 (diff) | |
| download | tddd86-L3.tar.gz | |
add tsp codeL3
Diffstat (limited to 'labb3/tsp/src/Tour.cpp')
| -rw-r--r-- | labb3/tsp/src/Tour.cpp | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/labb3/tsp/src/Tour.cpp b/labb3/tsp/src/Tour.cpp new file mode 100644 index 0000000..8f5dd99 --- /dev/null +++ b/labb3/tsp/src/Tour.cpp @@ -0,0 +1,143 @@ +#include <iostream> +#include "Tour.h" +#include "Node.h" +#include "Point.h" + +Tour::Tour() : first(nullptr) +{ } + +Tour::~Tour() +{ + if (!first) { + cout << "Empty tour" << endl; + return; + } + Node *node = first; + Node *next = first->next; + do { + delete node; + node = next; + next = node->next; + } + while (node != first); + first = nullptr; +} + +void Tour::show() const +{ + if (!first) { + cout << "Empty tour" << endl; + return; + } + Node *node = first; + do { + cout << node << " " << node->next << endl; + node = node->next; + } + while (node != first); +} + +void Tour::draw(QGraphicsScene *scene) const +{ + if (!first) { + cout << "Empty tour" << endl; + return; + } + Node *node = first; + do { + node->point.drawTo(node->next->point, scene); + node = node->next; + } + while (node != first); +} + +int Tour::size() const +{ + if (!first) { + cout << "Empty tour" << endl; + return 0; + } + Node *node = first; + int amount = 0; + do { + amount++; + node = node->next; + } + while (node != first); + return amount; +} + +double Tour::distance() const +{ + if (!first) { + cout << "Empty tour" << endl; + return 0; + } + Node *node = first; + double sum = 0; + do { + sum += node->point.distanceTo(node->next->point); + node = node->next; + } + while (node != first); + return sum; +} + +void Tour::insertNearest(Point p) +{ + if (!first) { + Node *node = new Node(p); + first = node; + node->next = node; + return; + } + if (first == first->next) { + Node *node = new Node(p, first); + first->next = node; + return; + } + double smallestDistance = first->point.distanceTo(p); + Node *smallestDistanceNode = first; + Node *node = first->next; + do { + double distance = node->point.distanceTo(p); + if (distance < smallestDistance) { + smallestDistance = distance; + smallestDistanceNode = node; + } + node = node->next; + } + while (node != first); + Node *newNode = new Node(p, smallestDistanceNode->next); + smallestDistanceNode->next = newNode; +} + +void Tour::insertSmallest(Point p) +{ + if (!first) { + Node *node = new Node(p); + first = node; + node->next = node; + return; + } + if (first == first->next) { + Node *node = new Node(p, first); + first->next = node; + return; + } + + double smallestDiff = first->point.distanceTo(p) + p.distanceTo(first->next->point) - first->point.distanceTo(first->next->point); + Node *smallestDiffNode = first; + Node *node = first->next; + do { + double diff = node->point.distanceTo(p) + p.distanceTo(node->next->point) - node->point.distanceTo(node->next->point); + if (diff < smallestDiff) { + smallestDiff = diff; + smallestDiffNode = node; + } + node = node->next; + } + while (node != first); + Node *newNode = new Node(p, smallestDiffNode->next); + smallestDiffNode->next = newNode; +} |
