Compare commits
No commits in common. "46e07569494677aebe28944c7ccf383dfd8715d3" and "da533a1f91310d09131fd073dfc83f8ac2609ffe" have entirely different histories.
46e0756949
...
da533a1f91
147
src/Tour.cpp
147
src/Tour.cpp
@ -4,140 +4,47 @@
|
|||||||
// Also remove these comments here and add your own.
|
// Also remove these comments here and add your own.
|
||||||
// TODO: remove this comment header
|
// TODO: remove this comment header
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#include "Tour.h"
|
#include "Tour.h"
|
||||||
#include "Node.h"
|
#include "Node.h"
|
||||||
#include "Point.h"
|
#include "Point.h"
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
Tour::Tour() : m_startNode(nullptr) {}
|
Tour::Tour()
|
||||||
|
{
|
||||||
Tour::~Tour() {
|
// TODO: write this member
|
||||||
if (m_startNode == nullptr)
|
|
||||||
return;
|
|
||||||
|
|
||||||
Node *node = m_startNode;
|
|
||||||
if (node->next == m_startNode) {
|
|
||||||
delete node;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
Node *next = node->next;
|
|
||||||
delete node;
|
|
||||||
node = next;
|
|
||||||
} while (node != m_startNode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tour::show() const {
|
Tour::~Tour()
|
||||||
if (m_startNode == nullptr)
|
{
|
||||||
return;
|
// TODO: write this member
|
||||||
|
|
||||||
Node *node = m_startNode;
|
|
||||||
do {
|
|
||||||
Point &p = node->point;
|
|
||||||
// I'm not using std::endl here to avoid flushing stdout for every
|
|
||||||
// iteration.
|
|
||||||
std::cout << '(' << p.x << ", " << p.y << ")\n";
|
|
||||||
node = node->next;
|
|
||||||
} while (node != m_startNode);
|
|
||||||
|
|
||||||
// Finally, flush!
|
|
||||||
std::flush(std::cout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tour::draw(QGraphicsScene *scene) const {
|
void Tour::show()
|
||||||
if (m_startNode == nullptr)
|
{
|
||||||
return;
|
// TODO: write this member
|
||||||
|
|
||||||
Node *node = m_startNode;
|
|
||||||
while (node->next != nullptr) {
|
|
||||||
node->point.drawTo(node->next->point, scene);
|
|
||||||
node = node->next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Tour::size() const {
|
void Tour::draw(QGraphicsScene *scene)
|
||||||
if (m_startNode == nullptr)
|
{
|
||||||
return 0;
|
// TODO: write this member
|
||||||
|
|
||||||
int count = 0;
|
|
||||||
Node *node = m_startNode;
|
|
||||||
do {
|
|
||||||
count++;
|
|
||||||
node = node->next;
|
|
||||||
} while (node != m_startNode);
|
|
||||||
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double Tour::distance() const {
|
int Tour::size()
|
||||||
if (m_startNode == nullptr)
|
{
|
||||||
return 0;
|
// TODO: write this member
|
||||||
|
|
||||||
double totalDistance = 0;
|
|
||||||
Node *node = m_startNode;
|
|
||||||
do {
|
|
||||||
totalDistance += node->point.distanceTo(node->next->point);
|
|
||||||
node = node->next;
|
|
||||||
} while (node != m_startNode);
|
|
||||||
|
|
||||||
return totalDistance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tour::insertNearest(Point p) {
|
double Tour::distance()
|
||||||
if (m_startNode == nullptr) {
|
{
|
||||||
m_startNode = new Node(p);
|
// TODO: write this member
|
||||||
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;
|
|
||||||
|
|
||||||
Node *node = m_startNode;
|
|
||||||
do {
|
|
||||||
double currentDistance = node->point.distanceTo(p);
|
|
||||||
if (currentDistance < minDistance) {
|
|
||||||
minDistance = currentDistance;
|
|
||||||
nearest = node;
|
|
||||||
}
|
|
||||||
node = node->next;
|
|
||||||
} while (node != m_startNode);
|
|
||||||
|
|
||||||
auto *newNode = new Node(p, nearest->next);
|
|
||||||
nearest->next = newNode;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tour::insertSmallest(Point p) {
|
void Tour::insertNearest(Point p)
|
||||||
if (m_startNode == nullptr) {
|
{
|
||||||
m_startNode = new Node(p);
|
// TODO: write this member
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
if (m_startNode->next == nullptr) {
|
void Tour::insertSmallest(Point p)
|
||||||
auto newNode = new Node(p, m_startNode);
|
{
|
||||||
m_startNode->next = newNode;
|
// TODO: write this member
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Node *bestNode = m_startNode;
|
|
||||||
double minIncrease = INFINITY;
|
|
||||||
|
|
||||||
Node *node = m_startNode;
|
|
||||||
do {
|
|
||||||
double increase = node->point.distanceTo(p) +
|
|
||||||
p.distanceTo(node->next->point) -
|
|
||||||
node->point.distanceTo(node->next->point);
|
|
||||||
if (increase < minIncrease) {
|
|
||||||
minIncrease = increase;
|
|
||||||
bestNode = node;
|
|
||||||
}
|
|
||||||
node = node->next;
|
|
||||||
} while (node != m_startNode);
|
|
||||||
|
|
||||||
auto *newNode = new Node(p, bestNode->next);
|
|
||||||
bestNode->next = newNode;
|
|
||||||
}
|
}
|
||||||
|
10
src/Tour.h
10
src/Tour.h
@ -15,15 +15,15 @@ public:
|
|||||||
|
|
||||||
Tour();
|
Tour();
|
||||||
~Tour();
|
~Tour();
|
||||||
void show() const;
|
void show();
|
||||||
void draw(QGraphicsScene* scene) const;
|
void draw(QGraphicsScene* scene);
|
||||||
int size() const;
|
int size();
|
||||||
double distance() const;
|
double distance();
|
||||||
void insertNearest(Point p);
|
void insertNearest(Point p);
|
||||||
void insertSmallest(Point p);
|
void insertSmallest(Point p);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Node* m_startNode;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // TOUR_H
|
#endif // TOUR_H
|
||||||
|
@ -19,10 +19,6 @@ 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);
|
||||||
|
|
||||||
@ -49,7 +45,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(150);
|
//std::chrono::milliseconds dura(50);
|
||||||
//std::this_thread::sleep_for(dura);
|
//std::this_thread::sleep_for(dura);
|
||||||
//a.processEvents();
|
//a.processEvents();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user