-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
493 lines (400 loc) · 14.1 KB
/
main.cpp
File metadata and controls
493 lines (400 loc) · 14.1 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#include <GL/glut.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <fstream>
#include <queue>
#include <stack>
#include <algorithm>
#include <vector>
#include <limits.h>
#include <math.h>
#include <sstream>
#include <map>
#include "graphx.h"
using namespace std;
static graph *prosesGraph;
static int mst, mstMenu, mstBobot, mstBobotMenu, findPath, findPathMenu, coloring, graphManipulate, mainx, mstSub, statAlgo = 0;
float xMidle, yMidle, jarak;
bool bobot = false, noBobot = false, pasangEdege = false;
vector< pair<float, float> > vertex; pair<bool, bool> findPathProp(false, false);
vector< vector<float> > adjacencyMatrix(100, vector <float>(100, -1));
vector< vector<float> > adjacencyTransversal; vector<bool> visitedVertex; vector <int> visitedVertexLog;
stringstream convert; string label; pair <bool, int> selectEdge(false, NULL);
void init(); void MidPoint(float xFrom, float yFrom, float xTujuan, float yTujuan);
void drawGraph(bool printAlgo); void display(); void djikstra(int source, int dest);
enum statusAlgo {mstAlgo = 1, mstBobotAlgo, findPathAlgo}; statusAlgo algoStatus;
enum colorEnum {hijau, merah, kuning, orange, hitam, putih};
enum algoritmaEnum {bfsEnum, dfsEnum, primEnum, djikstraEnum, backtrackingEnum};
int xMAx = 900, yMax = 700, kotaLabel = 1, corDiv = 15;
void warna(colorEnum color){
switch(color){
case hijau:
glColor3f(0.196, 0.804, 0.196);
break;
case merah:
glColor3f(1, 0, 0);
break;
case kuning:
glColor3f(1.000, 0.843, 0.000);
break;
case orange:
glColor3f(1.000, 0.549, 0.000);
break;
case hitam:
glColor3f(0, 0, 0);
break;
case putih:
glColor3f(1, 1, 1);
break;
}
}
void drawString(float x,float y,float z, string text, int font = 18)
{
char *c;
warna(putih);
glRasterPos3f(x,y,z);
for(int i = 0; i < text.length(); i++)
{
if (font == 12)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, text.data()[i]);
else if (font == 18)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, text.data()[i]);
else if (font == 24)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, text.data()[i]);
}
}
void judulAwal(){
glClear( GL_COLOR_BUFFER_BIT);
switch (algoStatus){
case mstAlgo:
drawString(340.0,650.0,0.0, "MINIMUM SPANNING TREE");
break;
case mstBobotAlgo:
drawString(290.0,650.0,0.0, "MINIMUM SPANNING TREE - BERBOBOT");
break;
case findPathAlgo:
drawString(280.0,650.0,0.0, "FINDPATH / PENCARIAN JALUR TERPENDEK");
break;
}
}
void drawKotak(float x, float y, string jenis = "normal", bool prosesAlg = false){
if (selectEdge.first && !prosesAlg){
float xSelect = vertex.at(selectEdge.second).first, ySelect = vertex.at(selectEdge.second).second;
if (x == xSelect && y == ySelect ){
warna(kuning);
glPointSize(42.0);
glBegin(GL_POINTS);
glVertex2f( x, y );
glEnd();
}
}
if (prosesAlg){
warna(kuning);
glPointSize(42.0);
glBegin(GL_POINTS);
glVertex2f( x, y);
glEnd();
}
if (jenis == "normal")
warna(orange);
else if( jenis == "src")
warna(merah);
else if(jenis == "dest")
warna(hijau);
glPointSize(30.0);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
convert << kotaLabel;
label = convert.str();
drawString(x-7,y-5,0.0,label);
kotaLabel++;
convert.str("");
}
void drawEdge(int i, int j,bool prosesAlg = false){
if (i == -1 || j == -1)
return;
if(!prosesAlg)
glLineWidth(1), warna(putih);
else
glLineWidth(5), warna(merah);
glBegin(GL_LINES);
glVertex2f( vertex.at(i).first , vertex.at(i).second );
glVertex2f( vertex.at(j).first , vertex.at(j).second );
glEnd();
if (algoStatus != mstAlgo){
float jarak = prosesGraph->getData().adjacencyMatrix.at(i).at(j);
convert << jarak;
label = convert.str();
convert.str("");
MidPoint(vertex.at(i).first , vertex.at(i).second, vertex.at(j).first, vertex.at(j).second);
drawString(xMidle, yMidle,0, label, 12);
}
}
void MidPoint(float xFrom, float yFrom, float xTujuan, float yTujuan){
xMidle = (xFrom + xTujuan) / 2;
yMidle = (yFrom + yTujuan) / 2;
}
bool selectEdgeSame(float x, float y){
for (int i = 0; i < vertex.size() ; i++)
{
if ( (x >= vertex.at(i).first && x <= (vertex.at(i).first+15)) || (x <= vertex.at(i).first && x >= (vertex.at(i).first-15)) )
if ( (y >= vertex.at(i).second && y <= (vertex.at(i).second+15)) || (y <= vertex.at(i).second && y >= (vertex.at(i).second-15)) ){
if(!selectEdge.first){
selectEdge.second = i;
selectEdge.first = true;
drawGraph(false);
}
else
if (selectEdge.second == i){
selectEdge.second = NULL, selectEdge.first = false;
drawGraph(false);
}
else{
float bobot;
float jarak = sqrt( pow( fabs( (vertex.at(selectEdge.second).first - vertex.at(i).first) ), 2 ) + pow( fabs( (vertex.at(i).second - vertex.at(selectEdge.second).second ) ), 2) );
bobot = algoStatus == mstAlgo ? 1 : jarak, bobot = algoStatus == mstAlgo ? 1 : jarak;
prosesGraph->addEdge(selectEdge.second, i, bobot);
drawGraph(false);
}
return true;
}
}
return false;
}
void setPoint(float x, float y){
if(algoStatus){
if (selectEdgeSame(x, y))
return;
vertex.push_back(make_pair(x,y));
drawGraph(false);
}else{
cout << "PILIH METODE ALGORITMA DULU" << endl;
}
}
void drawGraph(bool printAlgo = false){
judulAwal();
float nil; kotaLabel = 1; string node = "normal";
for (int i = 0; i < vertex.size(); i++){
if (algoStatus == findPathAlgo){
if (i == 0)
node = "src";
else if (i == 1)
node = "dest";
else
node = "normal";
}
drawKotak(vertex.at(i).first, vertex.at(i).second, node);
}
for (int v = 0; v < vertex.size(); v++){
for (int j = 0; j < vertex.size(); j++){
nil = printAlgo ? prosesGraph->getData().adjacencyTransversal.at(v).at(j) : prosesGraph->getData().adjacencyMatrix.at(v).at(j);
if (nil != -1)
drawEdge(v, j);
}
}
}
void drawAlgo(){
string node = "normal";
cout << "size: " << prosesGraph->getData().visitedVertexLog.size() << endl;
for (int i = 0; i < prosesGraph->getData().visitedVertexLog.size(); i++){
int vertexLog = prosesGraph->getData().visitedVertexLog.at(i);
cout << "i+1" << i+1 << endl;
int vertexLogNext = i+1 >= prosesGraph->getData().visitedVertexLog.size() ? -1 : prosesGraph->getData().visitedVertexLog.at(i+1);
if (algoStatus == findPathAlgo){
if (vertexLog == 0)
node = "src";
else if (vertexLog == 1)
node = "dest";
else
node = "normal";
}
kotaLabel = vertexLog + 1;
for (int i = 0; i <= 100000; i++){cout << i << endl;}
drawKotak(vertex.at(vertexLog).first, vertex.at(vertexLog).second, node, true);
glFlush();
}
if (algoStatus == findPathAlgo)
for (int i = 0; i < prosesGraph->getData().shortest.size() - 1; i++ )
drawEdge(prosesGraph->getData().shortest.at(i), prosesGraph->getData().shortest.at(i+1), true );
}
void allNodeConnected(){
if (vertex.size() < 2){
cout << "Jumlah vertex minimal 2!" << endl;
return;
}
for (int i = 0; i < vertex.size() ; i++)
{
for (int j = 0; j < vertex.size() ; j++){
if(i != j){
float bobot;
float jarak = sqrt( pow( fabs( (vertex.at(i).first - vertex.at(j).first) ), 2 ) + pow( fabs( (vertex.at(i).second - vertex.at(j).second) ), 2) );
bobot = algoStatus == mstAlgo ? 1 : jarak, bobot = algoStatus == mstAlgo ? 1 : jarak;
prosesGraph->addEdge(i, j, bobot);
}
}
}
if (algoStatus == findPathAlgo){
for (int i = 0; i < 2; i++)
for (int j = 0; j < vertex.size(); j++)
prosesGraph->removeEdge(i, j);
}
drawGraph(false);
}
void allNodeNotConnected(){
if (vertex.size() < 2){
cout << "Jumlah vertex minimal 2 !" << endl;
return;
}
for (int i = 0; i < vertex.size() ; i++)
for (int j = 0; j < vertex.size() ; j++)
prosesGraph->removeEdge(i, j);
drawGraph(false);
}
void display(){
if (!algoStatus){
drawString(380.0,650.0,0.0,"GRAPH ALGORITHM", 24);
drawString(100.0,550.0,0.0,"[F1] POHON RENTANG MINIMUM", 24);
drawString(150.0,520.0,0.0,"1. Depth First Search (DFS) ", 18);
drawString(150.0,490.0,0.0,"2. Breadth First Search (BFS) ", 18);
drawString(100.0,410.0,0.0,"[F2] POHON RENTANG MINIMUM - BERBOBOT", 24);
drawString(150.0,380.0,0.0,"1. Prim ", 18);
drawString(150.0,350.0,0.0,"2. Kruskal ", 18);
drawString(100.0,270.0,0.0,"[F3] PATHFINDING / JALUR TERPENDEK", 24);
drawString(150.0,240.0,0.0,"1. Djikstra ", 18);
drawString(150.0,210.0,0.0,"2. A* (Star) ", 18);
drawString(100.0,140.0,0.0,"[UTILITY] COLORING GRAPH", 24);
drawString(150.0,110.0,0.0,"1. Backtracking", 18);
}
glFlush();
}
void mouse(int mouse, int state, int x, int y){
switch(mouse){
case GLUT_LEFT_BUTTON:
if(state == GLUT_DOWN){
float a = x/1.0;
float b = yMax - (y/1.0);
setPoint(a,b);
glFlush();
}
break;
}
}
void ProcessMenu(int value){
cout << "prosess menu";
if (value == 7)
allNodeConnected();
else if (value == 8)
allNodeNotConnected();
}
void algoritma(int algo){
switch(algo){
case bfsEnum:
prosesGraph->proses("bfs", 0, false, vertex.size());
break;
case dfsEnum:
prosesGraph->proses("dfs", 0, false, vertex.size());
break;
case primEnum:
prosesGraph->proses("prim", 0, false, vertex.size());
break;
case djikstraEnum:
prosesGraph->proses("djikstra", 0, 1, vertex.size());
break;
}
drawAlgo();
}
void createMenu(void){
mst = glutCreateMenu(algoritma);
glutAddMenuEntry("BFS", bfsEnum);
glutAddMenuEntry("DFS", dfsEnum);
mstBobot = glutCreateMenu(algoritma);
glutAddMenuEntry("Prim", primEnum);
findPath = glutCreateMenu(algoritma);
glutAddMenuEntry("Djikstra", djikstraEnum);
coloring = glutCreateMenu(algoritma);
glutAddMenuEntry("Node - (Backtracking)", backtrackingEnum);
graphManipulate = glutCreateMenu(ProcessMenu);
glutAddMenuEntry("Connect All Nodes", 7);
glutAddMenuEntry("Disconnect All Nodes", 8);
glutAddMenuEntry("Restore Graph", 9);
glutAddMenuEntry("Delete Graph", 10);
mstMenu = glutCreateMenu(ProcessMenu);
glutAddSubMenu("Proses Algoritma", mst);
glutAddMenuEntry("Skip Process", 1);
glutAddSubMenu("Edit Graph", graphManipulate);
glutAddSubMenu("Coloring Graph", coloring);
glutAddMenuEntry("Back to Home.", 11);
findPathMenu = glutCreateMenu(ProcessMenu);
glutAddSubMenu("Proses Algoritma", findPath);
glutAddMenuEntry("Skip Process", 1);
glutAddSubMenu("Edit Graph", graphManipulate);
glutAddSubMenu("Coloring Graph", coloring);
glutAddMenuEntry("Back to Home.", 11);
mstBobotMenu = glutCreateMenu(ProcessMenu);
glutAddSubMenu("Proses Algoritma", mstBobot);
glutAddMenuEntry("Skip Process", 1);
glutAddSubMenu("Edit Graph", graphManipulate);
glutAddSubMenu("Coloring Graph", coloring);
glutAddMenuEntry("Back to Home.", 11);
//glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void special(int key, int, int)
{
// handle special keys
try {
if (key == GLUT_KEY_F1 || key == GLUT_KEY_F2 || key == GLUT_KEY_F3)
vertex.clear();
switch (key)
{
case GLUT_KEY_F1:
algoStatus = mstAlgo;
glClear( GL_COLOR_BUFFER_BIT);
prosesGraph = graph::graphAlgoritma("mst");
glutSetMenu(mstMenu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
break;
case GLUT_KEY_F2:
algoStatus = mstBobotAlgo;
glClear( GL_COLOR_BUFFER_BIT);
prosesGraph = graph::graphAlgoritma("mstBobot");
glutSetMenu(mstBobotMenu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
break;
case GLUT_KEY_F3:
algoStatus = findPathAlgo;
glClear( GL_COLOR_BUFFER_BIT);
prosesGraph = graph::graphAlgoritma("findPath");
glutSetMenu(findPathMenu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
break;
}
}catch(int x){
cout << "exception: " << x << endl;
}
judulAwal();
glFlush();
//glutPostRedisplay(); // force a redraw
}
void init(){
glClearColor(0,0,0,0);
glClear( GL_COLOR_BUFFER_BIT);
gluOrtho2D(0.0, xMAx, 0.0, yMax);
}
int main(int argc, char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(xMAx,yMax);
glutInitWindowPosition(200,100);
glutCreateWindow("Graphs");
createMenu();
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
// glutPassiveMotionFunc(motionku);
glutSpecialFunc(special);
glutMainLoop();
return 0;
}