forked from hakosaj/KandiTSP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuxilaryClasses.h
153 lines (109 loc) · 3.1 KB
/
AuxilaryClasses.h
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
#ifndef AUXCLASSES_H
#define AUXCLASSES_H
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Constants.h"
#include <cmath>
//Class for drawing sf lines
class sfLine : public sf::Drawable
{
public:
sfLine(const sf::Vector2f& point1, const sf::Vector2f& point2) :
color(sf::Color::Black), thickness(2.f)
{
sf::Vector2f direction = point2 - point1;
sf::Vector2f unitDirection = direction / std::sqrt(direction.x*direction.x + direction.y*direction.y);
sf::Vector2f unitPerpendicular(-unitDirection.y, unitDirection.x);
sf::Vector2f offset = (thickness / 2.f)*unitPerpendicular;
vertices[0].position = point1 + offset;
vertices[1].position = point2 + offset;
vertices[2].position = point2 - offset;
vertices[3].position = point1 - offset;
for (int i = 0; i<4; ++i)
vertices[i].color = color;
}
void draw(sf::RenderTarget &target, sf::RenderStates states) const
{
target.draw(vertices, 4, sf::Quads);
}
private:
sf::Vertex vertices[4];
float thickness;
sf::Color color;
};
class City {
public: float x, y;
int id;
friend bool operator== (const City &c1, const City &c2);
friend bool operator!= (const City &c1, const City &c2);
public: City(int num, float xval, float yval) {
id = num;
x = xval;
y = yval;
}
};
class Tour {
public: float dist;
std::vector<City> cintour;
Tour(std::vector<City> cts) {
cintour = cts;
dist = this->distance();
}
bool operator < (const Tour& tour2) const
{
return dist < tour2.dist;
}
int size()
{
return this->cintour.size();
}
float euclideanDistance(float x1, float x2, float y1, float y2)
{
float dist = std::sqrt(std::pow(x1 - x2, 2) + std::pow(y1 - y2, 2));
return dist/scalefactor;
}
float distance()
{
float currentDist = 0;
for (int i = 0; i < this->cintour.size()-1 ; i++)
{
currentDist += euclideanDistance(this->cintour[i].x, this->cintour[i + 1].x, this->cintour[i].y, this->cintour[i + 1].y);
}
currentDist += euclideanDistance(this->cintour[0].x, this->cintour[cintour.size() - 1].x, this->cintour[0].y, this->cintour[cintour.size() - 1].y);
return currentDist;
}
/*
float distance(std::vector<City> cits)
{
float currentDist = 0;
for (int i = 0; i < cintour.size() - 1; i++)
{
currentDist += euclideanDistance(cits[i].x, cits[i + 1].x, cits[i].y, cits[i + 1].y);
}
currentDist += euclideanDistance(cits[0].x, cits[cintour.size() - 1].x, cits[0].y, cits[cintour.size() - 1].y);
return currentDist;
}
*/
std::vector<int> ids()
{
std::vector<int> idt;
for (int r = 0; r <= cintour.size() - 1; r++)
{
idt.push_back(cintour[r].id);
}
return idt;
}
std::string representation()
{
std::string citys = "";
for (int r = 0; r <= cintour.size() - 1; r++)
{
citys += std::to_string(cintour[r].id);
citys += "-";
}
citys.pop_back();
return citys;
}
void printTour() { std::cout << this->representation() << std::endl; }
};
#endif