-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm
More file actions
326 lines (266 loc) · 7.78 KB
/
Copy pathalgorithm
File metadata and controls
326 lines (266 loc) · 7.78 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
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<GL/glut.h>
#include<string.h>
#define MAX 50
int k=0;
typedef struct circle
{
GLfloat x; //x axis of center
GLfloat y; //y axis of center
GLfloat r; // radius
}circle;
circle c[MAX];
int a[MAX]; // int array for sorting algorithm
int initial[MAX];
int n; //array of circles to store the center and radius of each circle
GLfloat initial_x1,initial_x2; //to set the destination for swapping
int global_i = 0, global_j = 0; //i and j values for bubble-sort
int swapping = 0; //flag to check if circle are being swapped
int sorting = 0; //start sorting only after user input
void initialise()
{
global_i = global_j = swapping = 0; //reset all globals
for (int i=0;i<n;i++)
{
a[i] = initial[i]; //if a[] is sorted restore from initial[]
c[i].r = a[i]*2.0; //4 because to fit 10 circles in screen
c[i].y = 300.0; //vertical center of window
if (i == 0)
c[i].x = 15.0; // first circle starts from 45 offset
else
c[i].x = c[i-1].x + 87.0;//(c[i-1].r+c[i].r+10.0); //distance between circles = sum of 2 max readii
//printf("%f - %f - %f\n",c[i].x,c[i].y,c[i].r); //for testing purpose don worry
}
}
//func to display text on screen char by char
void bitmap_output(int x, int y, char *string, void *font)
{
int len, i;
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
}
//function to integer to string
void int_str(int rad,char r[])
{
switch(rad)
{
case 1 : strcpy(r, "1"); break;
case 2 : strcpy(r, "2"); break;
case 3 : strcpy(r, "3"); break;
case 4 : strcpy(r, "4"); break;
case 5 : strcpy(r, "5"); break;
case 6 : strcpy(r, "6"); break;
case 7 : strcpy(r, "7"); break;
case 8 : strcpy(r, "8"); break;
case 9 : strcpy(r, "9"); break;
case 10 : strcpy(r, "10"); break;
case 11 : strcpy(r, "11"); break;
case 12 : strcpy(r, "12"); break;
case 13 : strcpy(r, "13"); break;
case 14 : strcpy(r, "14"); break;
case 15 : strcpy(r, "15"); break;
case 16 : strcpy(r, "16"); break;
case 17 : strcpy(r, "17"); break;
case 18 : strcpy(r, "18"); break;
case 19 : strcpy(r, "19"); break;
case 20 : strcpy(r, "20"); break;
}
}
//draw circle by drawing consecutive triangle fans
void circle_draw(circle c)
{
float i;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(c.x, c.y); //center of circle
for (i=0;i<360;i+=1)
glVertex2f(c.x + sin(i) * c.r, c.y + cos(i) * c.r);
glEnd();
//display the value of circle below
int x = c.x-2;
int y = c.y-(c.r+20);
int rad = c.r / 2;
char r[2] = "";
int_str(rad,r);
glColor3f(0.0,0.0,0.0);
bitmap_output(x, y, r, GLUT_BITMAP_TIMES_ROMAN_10);
}
// swaps circles cc1 and cc2 by changing their centers
void swap_circles(circle *cc1,circle *cc2)//cc->center of circle
{
if (swapping == 0) //if circles are not being swapped set destination for each circles
{
initial_x1 = cc1->x; //center of circle in left
initial_x2 = cc2->x; //center of circle in right
swapping = 1; //means cicle are being swapped
}
if (initial_x1 <= cc2->x) //decrease the center of circle in right till its > center of left circle
cc2->x -= 3.0; //speed of animation
if (initial_x2 >= cc1->x)//increase the center of circle in left till its < center of right circle
cc1->x += 3.0;
// if difference between destination and center < 0.3 then cicles are swapped
if (abs(initial_x1-cc2->x) < 0.7 && abs(initial_x2-cc1->x) < 0.7) //set this to speed of animation
{
swapping = 0;
int temp = cc1->x;
cc1->x = cc2->x;
cc2->x = temp;
temp = cc1->y;
cc1->y = cc2->y;
cc2->y = temp;
temp = cc1->r;
cc1->r = cc2->r;
cc2->r = temp;
}
}
void sort() //bubble sort algo
{
if (!swapping) //if not in process of swappin 2 circles only then get 2 new circles to swap
{
while (global_i < MAX)
{
global_j = global_i;
while (global_j < n)
{
if (a[global_j] > a[global_j+1])
{
//printf("%d %d\n",a[global_j],a[global_j+1]);
int temp = a[global_j];
a[global_j] = a[global_j+1];
a[global_j+1] = temp;
goto SWAP;
}
global_j ++;
}
global_i ++;
}
}
SWAP:
bitmap_output(10, 375, "Swapping ->",GLUT_BITMAP_9_BY_15);
char r[2]="";
int_str(a[global_j],r);
bitmap_output(150, 375, r,GLUT_BITMAP_9_BY_15);
int_str(a[global_j+1],r);
bitmap_output(175, 375, r,GLUT_BITMAP_9_BY_15);
swap_circles(&c[global_j],&c[global_j+1]);
}
void display_text()
{
bitmap_output(320, 565, "BUBBLE SORTING ALGORITHM!",GLUT_BITMAP_TIMES_ROMAN_24);//title larger font
bitmap_output(10, 525, "This program sorts a set of numbers in ascending order.",GLUT_BITMAP_9_BY_15);
if (sorting == 0)//if not sorting display menu
{
bitmap_output(10, 455, "MENU",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 435, "Press s to SORT",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 415, "Press r to RANDOMISE",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 395, "Esc to QUIT",GLUT_BITMAP_9_BY_15);
}
else if (sorting == 1)
{
glColor3f(0.5,0.5,0.5);
bitmap_output(10, 455, "Sorting in progress...",GLUT_BITMAP_9_BY_15);
bitmap_output(10, 435, "Please do not quit",GLUT_BITMAP_9_BY_15);
glColor3f(0.0,0.0,0.0);
}
}
void front()
{
glColor3f(0.0,0.80,0.91);
bitmap_output(320, 565, "Vivekananda College of Engineering",GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(1.0,0.54,0.0);
bitmap_output(350, 535, "Computer Graphics Project",GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(0.0,0.80,0.91);
bitmap_output(280, 485, "BUBBLE SORTING ALGORITHM SIMULATOR",GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(0.0,0.80,0.91);
bitmap_output(100, 295, "Under Guidance of:",GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(1.0,0.54,0.0);
bitmap_output(100, 265, "Prof.Krishna Mohana A J",GLUT_BITMAP_TIMES_ROMAN_24);
bitmap_output(100, 230, "Prof.Ajay Shastry C G",GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(0.0,0.80,0.91);
bitmap_output(100, 195, "Students:",GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(1.0,0.54,0.0);
bitmap_output(100, 165, "Aadesh Uday Shenvi-4VP19CS001",GLUT_BITMAP_TIMES_ROMAN_24);
bitmap_output(100, 130, "Adarsh-4VP19CS003",GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(0.0,0.0,1.0);
bitmap_output(600, 125, "Press Enter to continue......",GLUT_BITMAP_TIMES_ROMAN_24);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
if (k==0)
front();
else
{
display_text();
glPointSize(4.0);
for (int i=0;i<MAX;i++)
{
glColor3f(1.0,0.0,0.0);
circle_draw(c[i]);
}
if (global_j+1 < MAX && sorting == 1) // call sort only on key press
sort();
else
sorting = 0;
}
glFlush();
glutSwapBuffers();
}
void keyboard (unsigned char key, int x, int y)
{
if(key==13)
k=1;
if (k==1)
{
switch (key)
{
case 27 : exit (0); //27 is the ascii code for the ESC key
case 's' : sorting = 1; break;
case 'S' : sorting = 1; break;
case 'r' : initialise(); break;
}
}
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w, 2.0* (GLfloat) h / (GLfloat) w, -10.0, 10.0);
else
glOrtho(-2.0 * (GLfloat) w/ (GLfloat) h, 2.0* (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,900.0,0.0,600.0);
}
int main(int argc, char ** argv)
{
printf("Enter the no of elements(Max Elements upto 10)\n");
scanf("%d",&n);
printf("Enter the unsorted array(NUMBER RANGING BETWEEN 1-20)\n");
for(int i=0;i<n;i++)
{
scanf("%d",&initial[i]);
}
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,500);
glutCreateWindow("Bubble Sorting Algorithm");
init();
initialise();
glutDisplayFunc(display);
glutIdleFunc (display);
glutKeyboardFunc (keyboard);
glutMainLoop();
}