-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedge.cpp
More file actions
106 lines (90 loc) · 1.83 KB
/
Copy pathedge.cpp
File metadata and controls
106 lines (90 loc) · 1.83 KB
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
#include "edge.h"
#include <QPen>
#include <QBrush>
#include <QDebug>
double Edge::decayRate = 0.93;
double Edge::defaultPheromone = 0.8;
bool Edge::showPheromone = false;
double Edge::scale = 1.0;
Edge::Edge(QPointF p1, QPointF p2) :
pheromone(defaultPheromone),
onBestTour(false)
{
length = dist(p1, p2);
line = new QGraphicsLineItem(p1.x(), p1.y(), p2.x(), p2.y());
line->setZValue(PHEROMONE_Z);
updateLine(0.0);
}
Edge::~Edge()
{
delete line;
}
double Edge::addPheromone(double amount)
{
pheromone = pheromone + amount;
return pheromone;
}
void Edge::doDecay()
{
pheromone *= decayRate;
if (pheromone < 0.000001)
pheromone = 0.0;
// onBestTour = false;
}
void Edge::reset()
{
pheromone = defaultPheromone;
onBestTour = false;
updateLine(0.0);
}
void Edge::setBest(bool best)
{
onBestTour = best;
}
double Edge::getLength()
{
return length;
}
QGraphicsItem *Edge::getGraphicsItem()
{
return line;
}
double Edge::getPheromone()
{
return pheromone;
}
void Edge::setDecayRate(int rate)
{
decayRate = 1.0 - rate / 100.0;
}
void Edge::setDefaultPheromone(double amount)
{
defaultPheromone = amount;
}
void Edge::setShowPheromone(bool show)
{
showPheromone = show;
}
void Edge::setScale(double x, double y)
{
scale = (x + y) / 2.0;
}
void Edge::updateLine(double best)
{
if (!onBestTour && (!showPheromone || best <= 0.0 || best / 10.0 > pheromone)) {
line->setVisible(false);
return;
}
line->setVisible(true);
QPen pen;
if (onBestTour) {
pen.setColor(BEST_TOUR_COLOR);
pen.setWidthF(5.0 / scale);
line->setZValue(BEST_PHEROMONE_Z);
} else {
pen.setColor(EDGE_COLOR);
pen.setWidthF((5.0/scale)*pheromone/best);
line->setZValue(PHEROMONE_Z);
}
line->setPen(pen);
}