-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrafik.cpp
301 lines (294 loc) · 9.43 KB
/
grafik.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <iostream>
#include <SDL2\SDL.h>
#include <vector>
#include <time.h>
#include <thread>
#include <algorithm>
#include <queue>
//Measurements of the window
const int WIDTH = 600, HEIGHT = 600;
//Radius of drawn vertexes
const int VRAD= 10;
//Indexes of chosen points
int first = -1, second = -1;
//Bools for multithreading
bool dijkstraMode = false, dijkstraWorking = false;
//Window title
const char *mainTitle = "Grafik";
//Window
SDL_Window *mainWindow = nullptr;
//Renderer
SDL_Renderer *renderer = nullptr;
//Checking if point is closer than VRAD to another point
bool CheckCircle(int x1, int y1, int x2, int y2){
if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<=VRAD*VRAD){
return true;
}
return false;
}
//Vertex structure needed for visualising
struct _vertex{
int x, y, index;
};
//Drawing circle around vertex
void DrawCircle(_vertex v){
for(int i= v.x-VRAD+1; i<=v.x+VRAD; i++){
for(int j= v.y-VRAD+1; j<=v.y+VRAD; j++){
if(CheckCircle(v.x, v.y, i, j)){
SDL_RenderDrawPoint(renderer, i,j);
}
}
}
}
//List of vertexes
std::vector< _vertex > vertexes;
//Adjacency matrix
std::vector< std::vector<float> > edges;
//List of edges to color
std::vector< std::pair<int ,int> > coloredEdges;
//List of vertexes to color
std::vector<int> coloredVertexes;
//Function for setting up the window and renderer
void WindowSetup(){
SDL_SetMainReady();
SDL_Init(SDL_INIT_EVERYTHING);
SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, SDL_WINDOW_RESIZABLE, &mainWindow, &renderer);
SDL_RenderSetScale(renderer,1 ,1);
SDL_SetWindowTitle(mainWindow, mainTitle);
}
//Function for drawing vertexes with adequate color
void DrawVertexes(){
for(_vertex v: vertexes){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
for(int in : coloredVertexes){
if(in==v.index){
SDL_SetRenderDrawColor(renderer, 100, 200, 200, 255);
}
}
if(v.index == first||v.index == second){
SDL_SetRenderDrawColor(renderer, 100, 240, 50, 255);
if(dijkstraMode){
SDL_SetRenderDrawColor(renderer, 206, 233, 30, 255);
}
}
DrawCircle(v);
}
}
//Function for drawing edges with adequate color
void DrawEdges(){
for(int i=0; i<edges.size();i++){
for(int j=0; j<edges[i].size(); j++){
if(edges[i][j]!=-1.0f){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
for(int k=0; k<coloredEdges.size(); k++){
if((coloredEdges[k].first ==i &&coloredEdges[k].second == j)||(coloredEdges[k].first == j&&coloredEdges[k].second ==i)){
SDL_SetRenderDrawColor(renderer, 100, 150, 230, 255);
}
}
SDL_RenderDrawLine(renderer, vertexes[i].x, vertexes[i].y, vertexes[j].x, vertexes[j].y);
}
}
}
}
//Function for calculating distance between two vertexes
float EdgeLen(_vertex a, _vertex b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
//Function to add edges between two vertexes(deletes the edge if it already exists)
void AddEdge(_vertex a, _vertex b){
if(a.index==b.index){
return;
}
if(edges[a.index][b.index]!=-1.0f){
edges[a.index][b.index] = -1.0f;
edges[b.index][a.index] = -1.0f;
return;
}
float len = EdgeLen(a,b);
edges[a.index][b.index] = len;
edges[b.index][a.index] = len;
}
//Evens out adjacency matrix after adding a vertex
void EvenAdjMatrix(){
for(int i=0; i<edges.size(); i++){
while(edges[i].size()<edges.size()){
edges[i].push_back(-1.0f);
}
}
}
//Function for adding a vertex
void AddVertex(int x, int y){
if(vertexes.size()>0){
for(_vertex ver: vertexes){
if(ver.x == x&&ver.y==y){
return;
}
}
}
_vertex v;
v.x = x;
v.y = y;
v.index = vertexes.size();
vertexes.push_back(v);
std::vector<float> s;
edges.push_back(s);
EvenAdjMatrix();
}
//Function for deleting vertexes
void DeleteVertex(int n){
edges.erase(edges.begin()+n);
for(int i=0; i<edges.size(); i++){
edges[i].erase(edges[i].begin()+n);
}
vertexes.erase(vertexes.begin()+n);
for(int i=0; i<vertexes.size(); i++){
vertexes[i].index = i;
}
first =-1;
}
//Dijkstras algorithm loop for finding the shortest path between two vertexes and visualising it
void Dijkstra(){
while(true){
if(dijkstraMode&&first!=-1&&second!=-1){
dijkstraWorking = true;
int n, start, end;
start = first;
end = second;
n = vertexes.size();
std::vector<float> distances(n+1, FLT_MAX);
std::vector<int> predecessors(n+1, -1);
distances[start]=0.0;
std::priority_queue< std::pair<float , int> > Q;
Q.push({0.0, start});
while(!Q.empty()){
int v = Q.top().second;
Q.pop();
coloredVertexes.push_back(v);
for(int i=0; i<edges[v].size(); i++){
if(edges[v][i]!=-1.0f){
SDL_Delay(100);
coloredEdges.push_back({v,i});
float w = edges[v][i];
if(distances[v]+w<distances[i]){
distances[i]=distances[v]+w;
predecessors[i]=v;
Q.push({-w, i});
}
}
}
}
std::vector<int> path;
int c=end;
while(predecessors[c]!=-1){
c=predecessors[c];
path.push_back(c);
}
reverse(path.begin(), path.end());
SDL_Delay(10);
std::vector< std::pair<int , int> > edg;
std::vector<int> ver;
coloredEdges = edg;
coloredVertexes = ver;
int prev = -1;
for(int in: path){
SDL_Delay(100);
if(in ==-1){
dijkstraMode = false;
coloredEdges = edg;
coloredVertexes = ver;
break;
}
coloredEdges.push_back({prev, in});
coloredVertexes.push_back(in);
prev = in;
}
coloredEdges.push_back({prev, end});
coloredVertexes.push_back(end);
first = -1;
second = -1;
while(dijkstraMode){
}
coloredEdges = edg;
coloredVertexes = ver;
dijkstraWorking = false;
}
}
}
//Main loop to project the graph and change it
void Loop(){
bool var = true;
while(var){
SDL_SetRenderDrawColor(renderer, 0,0,0,255);
SDL_RenderClear(renderer);
SDL_Event event;
if(SDL_PollEvent(&event)){
if(event.type == SDL_KEYDOWN){
switch(event.key.keysym.sym){
case SDLK_ESCAPE:{
var = false;
break;
}
case SDLK_d:{
dijkstraMode = !dijkstraMode;
break;
}
}
}
if(event.type == SDL_QUIT){
var = false;
return;
}
if(event.type == SDL_MOUSEBUTTONDOWN){
int x, y;
bool clickedVertex = false;
SDL_GetMouseState(&x, &y);
for(_vertex v: vertexes){
clickedVertex = CheckCircle(x, y, v.x, v.y);
if(clickedVertex&&!dijkstraWorking){
switch(event.button.button){
case SDL_BUTTON_LMASK:{
if(first == -1){
first = v.index;
}
else{
second = v.index;
if(!dijkstraMode){
AddEdge(vertexes[first], vertexes[second]);
first = -1;
second = -1;
}
}
break;
}
case SDL_BUTTON_RIGHT:{
DeleteVertex(v.index);
break;
}
}
break;
}
}
if(!clickedVertex){
switch(event.button.button){
case SDL_BUTTON_LMASK:{
AddVertex(x,y);
break;
}
}
}
}
}
DrawEdges();
DrawVertexes();
SDL_SetRenderDrawColor(renderer, 0,0,0,255);
SDL_RenderPresent(renderer);
}
}
int main(){
WindowSetup();
std::thread dijkstra(Dijkstra);
Loop();
SDL_DestroyWindow(mainWindow);
SDL_Quit();
return EXIT_SUCCESS;
}