-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgridwidget.cpp
More file actions
143 lines (118 loc) · 4.09 KB
/
gridwidget.cpp
File metadata and controls
143 lines (118 loc) · 4.09 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
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
#include "gridwidget.h"
#include <Eigen/Dense>
#include "antsimutils.h"
GridWidget::GridWidget(World &world, QWidget *parent): antWorld{world},QWidget{parent}
{
this->place_colony();
}
void GridWidget::DrawWindowBackground(QPainter &antPainter, QPaintEvent *event)
{
QBrush background = QBrush(QColor(0, 0, 0));
antPainter.fillRect(event->rect(), background);
}
void GridWidget::draw_food(QPainter &antPainter, const std::vector<Food> &food)
{
antPainter.save();
for (int i{0}; i < food.size(); i++)
{
QRectF foodBox{food[i].get_x_coordinate() - foodSize, food[i].get_y_coordinate() - foodSize, foodSize, foodSize};
antPainter.drawImage(foodBox, foodImage);
antPainter.restore();
}
antPainter.restore();
}
void GridWidget::draw_ants(QPainter& antPainter, const std::vector<Ant *> &ants)
{
antPainter.save();
double antScaleValue{15};
double halfAntScaleValue{antScaleValue * 0.5};
for (int i{0}; i < antWorld.get_amount_ants(); i++)
{
QPointF antLocation{ants[i]->get_position()[0] ,ants[i]->get_position()[1]};
Eigen::Vector2d orginalOrientation{{0.0,-1.0}};
double rotationAngle{calculate_clockwise_alignment_angle_degrees(orginalOrientation, ants[i]->get_orientation())};
antPainter.save();
antPainter.translate(antLocation);
antPainter.rotate(rotationAngle);
antPainter.translate(-antLocation.x() + halfAntScaleValue, -antLocation.y() + halfAntScaleValue);
QRectF antBox{antLocation.x()-antScaleValue, antLocation.y()-antScaleValue, antScaleValue, (antScaleValue)};
antPainter.drawImage(antBox, antImage);
antPainter.restore();
}
draw_ant_food(antPainter,ants);
antPainter.restore();
}
void GridWidget::draw_ant_food(QPainter &antPainter, const std::vector<Ant *> &ants)
{
double cargoShift{8.0};
for (int i{0}; i < antWorld.get_amount_ants(); i++)
{
if (ants[i]->has_food())
{
QPointF cargoLocation{ants[i]->get_position()[0] + cargoShift, ants[i]->get_position()[1] - cargoShift};
QRectF foodBox{cargoLocation.x()-foodSize, cargoLocation.y()-foodSize, foodSize, foodSize};
antPainter.drawImage(foodBox, foodImage);
}
}
}
void GridWidget::draw_colony(QPainter& antPainter)
{
antPainter.drawPixmap(colonyLocation, colony);
}
void GridWidget::draw_pheromones(QPainter &antPainter, const std::vector<Pheromone> &pheromones, char color)
{
antPainter.save();
antPainter.setPen(set_up_pheromone_pen(color));
for (int i{0}; i < pheromones.size(); i++)
{
antPainter.drawPoint(pheromones[i].get_x_position(),pheromones[i].get_y_position());
}
}
void GridWidget::mouseReleaseEvent(QMouseEvent *event)
{
QPoint position = event->pos();
antWorld.add_sushi_roll(position.x(), position.y());
}
void GridWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
this->reset_world();
}
void GridWidget::paintEvent(QPaintEvent *event)
{
QPainter antPainter(this);
DrawWindowBackground(antPainter, event);
draw_colony(antPainter);
antWorld.update();
draw_ants(antPainter,antWorld.get_ants());
draw_food(antPainter, antWorld.get_food());
draw_pheromones(antPainter,antWorld.get_home_pheromones(),'g');
draw_pheromones(antPainter, antWorld.get_food_pheromones(),'b');
}
QPen GridWidget::set_up_pheromone_pen(char color)
{
QPen pheromonePen;
pheromonePen.setWidth(2);
if (color == 'g')
{
pheromonePen.setColor(Qt::green);
}
else if (color == 'b')
{
pheromonePen.setColor(Qt::blue);
}
else
{
pheromonePen.setColor(Qt::red);
}
return pheromonePen;
}
void GridWidget::place_colony()
{
colonyLocation.setX(antWorld.get_world_size()/2 - colonyScaleValue/2);
colonyLocation.setY(antWorld.get_world_size()/2 - colonyScaleValue/2);
colony = colony.scaled(colonyScaleValue,colonyScaleValue);
}
void GridWidget::reset_world()
{
antWorld.reset();
}