Mercurial > public > algo-animator
annotate main.c @ 10:f4e0c266321a
bubble sort
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Fri, 23 Jun 2023 20:24:37 +0100 |
parents | b7da0083b706 |
children | aaecc9b7ca9c |
rev | line source |
---|---|
8 | 1 #include <stdio.h> |
9 | 2 #include <stdlib.h> |
7 | 3 #include <GL/glut.h> |
9 | 4 #include <math.h> |
10 | 5 #include <time.h> |
0 | 6 |
7 | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
8 #define WINDOW_HEIGHT 1080 |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
9 #define WINDOW_WIDTH 1920 |
10 | 10 #define OFFSET 150 |
7 | 11 #define RECT_WIDTH 5 |
10 | 12 #define SPACE 1 |
7 | 13 |
14 | |
9 | 15 // Globals |
16 char* algos[] = {"Bubble sort", "Selection sort", "Insertion sort", "Quick sort"}; | |
17 int selected_algo = 0; | |
18 int n_algos = sizeof(algos) / sizeof(algos[0]); | |
19 | |
10 | 20 int* arr; |
21 int arr_size; | |
0 | 22 |
23 | |
9 | 24 // Algos |
25 void bubble_sort() { | |
10 | 26 for (int step = 0; step < arr_size - 1; step++) { |
9 | 27 |
10 | 28 for (int i = 0; i < arr_size - step - 1; i++) { |
29 int current = arr[i]; | |
30 int next = arr[i + 1]; | |
9 | 31 |
10 | 32 if (current > next) { |
33 arr[i + 1] = current; | |
34 arr[i] = next; | |
35 } | |
9 | 36 } |
37 } | |
38 } | |
39 | |
40 | |
10 | 41 void create_array() { |
42 arr_size = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1; | |
43 arr = (int*)malloc(arr_size * sizeof(int)); | |
44 | |
45 // srand(time(NULL)); | |
46 | |
47 int min = OFFSET; | |
48 int max = WINDOW_HEIGHT - OFFSET; | |
49 | |
50 for (int i = 0; i < arr_size; i++) { | |
51 arr[i] = rand() % ((max - min) + 1) + min; | |
52 } | |
53 } | |
54 | |
55 | |
56 void print_array() { | |
57 for (int i = 0; i < arr_size; i++) { | |
58 printf("%d ", arr[i]); | |
59 } | |
9 | 60 } |
61 | |
62 | |
63 void algo_selector(int direction) { | |
64 int selection = selected_algo + direction; | |
65 int lower = 0; | |
66 int upper = (sizeof(algos) / sizeof(algos[0])) - 1; | |
67 | |
68 if (selection >= lower && selection <= upper) { | |
69 selected_algo = selection; | |
70 } | |
71 } | |
72 | |
73 // GL and GLUT | |
0 | 74 void setup() { |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
75 |
0 | 76 // Set background dark |
77 glClearColor(0.0, 0.0, 0.0, 1.0); | |
78 | |
79 // Set point color and size to 1 pixel | |
80 glColor3f(0.0, 1.0, 0.0); | |
81 glPointSize(5.0); | |
82 | |
83 // Matrix projection and reset with identity | |
84 glMatrixMode(GL_PROJECTION); | |
85 glLoadIdentity(); | |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
86 |
10 | 87 /* |
88 * Creates projection matrix | |
89 * x increases from left to right (0 to WINDOW_WIDTH) | |
90 * y increases from bottom to top (0 to WINDOW_HEIGHT) | |
91 */ | |
92 gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT); | |
8 | 93 |
94 } | |
95 | |
96 | |
97 void render_text(int x, int y, char* text) { | |
98 glRasterPos2f(x, y); | |
99 | |
100 for (const char *c = text; *c; ++c) { | |
101 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c); | |
102 } | |
0 | 103 } |
104 | |
105 | |
106 void display() { | |
107 glClear(GL_COLOR_BUFFER_BIT); | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
108 glBegin(GL_QUADS); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
109 |
10 | 110 int x = 0; |
111 for (int i = 0; i < arr_size; i++) { | |
112 | |
113 // Bottom left | |
114 glVertex2f(x, OFFSET); | |
115 | |
116 // Top left | |
117 glVertex2f(x, arr[i]); | |
118 | |
119 // Top right | |
120 glVertex2f(x + RECT_WIDTH, arr[i]); | |
121 | |
122 // Bottom right | |
123 glVertex2f(x + RECT_WIDTH, OFFSET); | |
124 | |
125 x += RECT_WIDTH + SPACE; | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
126 } |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
127 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
128 glEnd(); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
129 |
7 | 130 // Render text |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
131 char text[256]; |
9 | 132 |
133 sprintf(text, "Algorithm: %s", algos[selected_algo]); | |
10 | 134 render_text(20, OFFSET - 50, text); |
8 | 135 |
10 | 136 sprintf(text, "Number of elements: %i", arr_size); |
137 render_text(20, OFFSET - 75, text); | |
9 | 138 |
10 | 139 render_text(WINDOW_WIDTH - 500, OFFSET - 50, "Press 'a' or 's' to select an algorithm"); |
140 render_text(WINDOW_WIDTH - 500, OFFSET - 75, "Press 'enter' to run the algorithm"); | |
141 render_text(WINDOW_WIDTH - 500, OFFSET - 100, "Press 'r' to reset array"); | |
8 | 142 |
143 glutSwapBuffers(); | |
144 glFlush(); | |
145 } | |
146 | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
147 |
8 | 148 void idle() { |
149 glutPostRedisplay(); | |
150 } | |
151 | |
152 | |
153 void keyboard(unsigned char key, int x, int y) { | |
154 | |
10 | 155 // s |
9 | 156 if (key == 115) { |
157 algo_selector(1); | |
158 } | |
159 | |
10 | 160 // r |
161 if (key == 114) { | |
162 create_array(); | |
163 } | |
164 | |
165 // a | |
9 | 166 if (key == 97) { |
167 algo_selector(-1); | |
168 } | |
169 | |
10 | 170 // enter |
8 | 171 if (key == 13) { |
10 | 172 printf("Before sorting: "); |
173 print_array(); | |
174 printf("\n\n"); | |
175 | |
9 | 176 bubble_sort(); |
10 | 177 |
178 printf("After sorting: "); | |
179 print_array(); | |
7 | 180 } |
0 | 181 } |
182 | |
183 | |
184 int main(int argc, char** argv) { | |
10 | 185 create_array(); |
7 | 186 |
0 | 187 glutInit(&argc, argv); |
8 | 188 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
189 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); |
0 | 190 glutCreateWindow("OpenGL Window"); |
191 setup(); | |
192 glutDisplayFunc(display); | |
8 | 193 glutKeyboardFunc(keyboard); |
194 glutIdleFunc(idle); | |
0 | 195 glutMainLoop(); |
196 | |
10 | 197 free(arr); |
7 | 198 |
0 | 199 return 0; |
200 } | |
7 | 201 |
202 |