comparison main.c @ 9:b7da0083b706

removing struct?
author Dennis C. M. <dennis@denniscm.com>
date Fri, 23 Jun 2023 18:26:31 +0100
parents f7af7255705e
children f4e0c266321a
comparison
equal deleted inserted replaced
8:f7af7255705e 9:b7da0083b706
1 #include "utils.h"
2 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <GL/glut.h> 3 #include <GL/glut.h>
4 #include <math.h>
4 5
5 6
6 #define WINDOW_HEIGHT 1080 7 #define WINDOW_HEIGHT 1080
7 #define WINDOW_WIDTH 1920 8 #define WINDOW_WIDTH 1920
8 #define RECT_WIDTH 5 9 #define RECT_WIDTH 5
9 #define SPACE 5 10 #define SPACE 5
10 11
11 12
13 // Globals
14 char* algos[] = {"Bubble sort", "Selection sort", "Insertion sort", "Quick sort"};
15
16 int selected_algo = 0;
17 int n_algos = sizeof(algos) / sizeof(algos[0]);
18 int n_rectangles;
19
12 struct Rectangle { 20 struct Rectangle {
13 int width; 21 int width;
14 int height; 22 int height;
15 int x; 23 int x;
16 }; 24 };
17 25
18 // Globals
19 struct Rectangle* rectangles; 26 struct Rectangle* rectangles;
20 int n_rectangles;
21 int test_counter = 0;
22 27
23 28
29 // Algos
30 void bubble_sort() {
31 for (int i = 0; i < n_rectangles; i++) {
32
33 struct Rectangle current = rectangles[i];
34 struct Rectangle next = rectangles[i + 1];
35
36 printf("Current pos: %i\n", current.x);
37 printf("Next pos: %i\n", next.x);
38
39 // Place largest element at the end of the array
40 if (current.height > next.height) {
41 rectangles[i + 1].x = current.x;
42 rectangles[i].x = next.x;
43 }
44 }
45 }
46
47
48 // Utils
49 int random_int(int min, int max) {
50 return rand() % ((max - min) + 1) + min;
51 }
52
53
54 void algo_selector(int direction) {
55 int selection = selected_algo + direction;
56 int lower = 0;
57 int upper = (sizeof(algos) / sizeof(algos[0])) - 1;
58
59 if (selection >= lower && selection <= upper) {
60 selected_algo = selection;
61 }
62 }
63
64 // GL and GLUT
24 void setup() { 65 void setup() {
25 66
26 // Set background dark 67 // Set background dark
27 glClearColor(0.0, 0.0, 0.0, 1.0); 68 glClearColor(0.0, 0.0, 0.0, 1.0);
28 69
52 void display() { 93 void display() {
53 glClear(GL_COLOR_BUFFER_BIT); 94 glClear(GL_COLOR_BUFFER_BIT);
54 glBegin(GL_QUADS); 95 glBegin(GL_QUADS);
55 96
56 for (int i = 0; i < n_rectangles; i++) { 97 for (int i = 0; i < n_rectangles; i++) {
57 glVertex2f(rectangles[i].x, WINDOW_HEIGHT - 100); 98 glVertex2f(rectangles[i].x, WINDOW_HEIGHT - 200);
58 glVertex2f(rectangles[i].x + rectangles[i].width, WINDOW_HEIGHT - 100); 99 glVertex2f(rectangles[i].x + rectangles[i].width, WINDOW_HEIGHT - 200);
59 glVertex2f(rectangles[i].x + rectangles[i].width, rectangles[i].height); 100 glVertex2f(rectangles[i].x + rectangles[i].width, rectangles[i].height);
60 glVertex2d(rectangles[i].x, rectangles[i].height); 101 glVertex2d(rectangles[i].x, rectangles[i].height);
61 } 102 }
62 103
63 glEnd(); 104 glEnd();
64 105
65 // Render text 106 // Render text
66 char text[256]; 107 char text[256];
108
109 sprintf(text, "Algorithm: %s", algos[selected_algo]);
110 render_text(20.0, WINDOW_HEIGHT - 130, text);
111
67 sprintf(text, "Number of elements: %i", n_rectangles); 112 sprintf(text, "Number of elements: %i", n_rectangles);
68 render_text(40.0, WINDOW_HEIGHT - 50, text); 113 render_text(20.0, WINDOW_HEIGHT - 100, text);
69 114
70 sprintf(text, "Test counter: %i", test_counter); 115 render_text(WINDOW_WIDTH - 500, WINDOW_HEIGHT - 130, "Press 'a' or 's' to select an algorithm");
71 render_text(540.0, WINDOW_HEIGHT - 50, text); 116 render_text(WINDOW_WIDTH - 500, WINDOW_HEIGHT - 100, "Press 'enter' to run the algorithm");
72 117
73 glutSwapBuffers(); 118 glutSwapBuffers();
74 glFlush(); 119 glFlush();
75 } 120 }
76 121
80 } 125 }
81 126
82 127
83 void keyboard(unsigned char key, int x, int y) { 128 void keyboard(unsigned char key, int x, int y) {
84 129
85 // 13 is the ASCII value of ENTER key 130 // S
131 if (key == 115) {
132 algo_selector(1);
133 }
134
135 // A
136 if (key == 97) {
137 algo_selector(-1);
138 }
139
140 // Enter
86 if (key == 13) { 141 if (key == 13) {
87 test_counter++; 142 bubble_sort();
88 } 143 }
89 } 144 }
90 145
91 146
92 int main(int argc, char** argv) { 147 int main(int argc, char** argv) {
96 int x_pos = 1; 151 int x_pos = 1;
97 152
98 int i = 0; 153 int i = 0;
99 while (i < n_rectangles) { 154 while (i < n_rectangles) {
100 rectangles[i].width = RECT_WIDTH; 155 rectangles[i].width = RECT_WIDTH;
101 rectangles[i].height = random_int(100, WINDOW_HEIGHT - 100); 156 rectangles[i].height = random_int(100, WINDOW_HEIGHT - 200);
102 rectangles[i].x = x_pos; 157 rectangles[i].x = x_pos;
103 158
104 x_pos += RECT_WIDTH + SPACE; 159 x_pos += RECT_WIDTH + SPACE;
105 i++; 160 i++;
106 } 161 }