blob: 5c63ad27de9ab7754ef8686c03e3c26743f312f6 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#include "Tour.h"
#include <iostream>
#include "Node.h"
#include "Point.h"
Tour::Tour()
: first(nullptr)
{}
Tour::~Tour()
{
if (!first) {
return;
}
Node *node = first;
Node *next;
while (node->next != first) {
next = node->next;
delete node;
node = next;
}
delete node;
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); // do-while since `node == first` for the first node
}
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); // do-while since `node == first` for the first node
}
int Tour::size() const
{
if (!first) {
cout << "Empty tour" << endl;
return 0;
}
int amount = 0;
Node *node = first;
do {
amount++;
node = node->next;
} while (node != first); // do-while since `node == first` for the first node
return amount;
}
double Tour::distance() const
{
if (!first) {
cout << "Empty tour" << endl;
return 0.0;
}
double sum = 0.0;
Node *node = first;
do {
sum += node->point.distanceTo(node->next->point);
node = node->next;
} while (node != first); // do-while since `node == first` for the first node
return sum;
}
void Tour::insertNearest(Point p)
{
if (!first) {
// no current nodes
Node *node = new Node(p);
first = node;
node->next = node;
return;
}
if (first == first->next) {
// only one current node
Node *node = new Node(p, first);
first->next = node;
return;
}
/*
* For each node, inserting a new node at p makes a triangle like below.
* We insert p in order to minimize d1.
*
* p ----*
* / \----*
* d1 / \----* d2
* / \----*
* / \----*
* / \----*
* node ------------------------------------ next
* d0
*/
double smallestDistance = first->point.distanceTo(p); // d1
Node *node = first->next;
Node *smallestDistanceNode = first; // the node to insert this node after
while (node != first) {
double distance = node->point.distanceTo(p); // d1
if (distance < smallestDistance) {
smallestDistance = distance;
smallestDistanceNode = node;
}
node = node->next;
}
Node *newNode = new Node(p, smallestDistanceNode->next);
smallestDistanceNode->next = newNode;
}
void Tour::insertSmallest(Point p)
{
if (!first) {
// no current nodes
Node *node = new Node(p);
first = node;
node->next = node;
return;
}
if (first == first->next) {
// only one current node
Node *node = new Node(p, first);
first->next = node;
return;
}
/*
* For each node, inserting a new node at p makes a triangle like below.
* We insert p in order to minimize d1 + d2 - d0.
*
* p ----*
* / \----*
* d1 / \----* d2
* / \----*
* / \----*
* / \----*
* node ------------------------------------ next
* d0
*/
double smallestDiff = first->point.distanceTo(p) // d1
+ p.distanceTo(first->next->point) // d2
- first->point.distanceTo(first->next->point); // d0
Node *node = first->next;
Node *smallestDiffNode = first; // the node to insert this node after
while (node != first) {
double diff = node->point.distanceTo(p) // d1
+ p.distanceTo(node->next->point) // d2
- node->point.distanceTo(node->next->point); // d0
if (diff < smallestDiff) {
smallestDiff = diff;
smallestDiffNode = node;
}
node = node->next;
}
Node *newNode = new Node(p, smallestDiffNode->next);
smallestDiffNode->next = newNode;
}
|