-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualizer.cpp
152 lines (122 loc) · 2.92 KB
/
Visualizer.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
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <unistd.h>
#include "GL/freeglut.h"
#include "GL/gl.h"
int length;
int delay;
int* arr;
void (*sort)(int*, int);
static int window, returnsubmenucolor1, value=0, returnmenu;
void randomizeArray(int* arr, int length)
{
for(int i = length - 1; i > 0; --i)
{
std::swap(arr[i], arr[rand() % (i+1)]);
}
}
//used for debugging
void printArray(int* arr, int length)
{
for(int i = 0; i < length; ++i)
{
printf("%i", arr[i]);
if(i < length - 1)
{
printf(",");
}
}
printf("\n");
}
void renderFunction()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
float l = (float) length;
float widthAdder = 1/l;
for(int i = 0; i < length; ++i)
{
glBegin(GL_POLYGON);
// + 1 so value of 0 has height of 1
float arrayIndexHeightRation = 2*(arr[i] + 1)/l;
float widthIndexAdder = 2*i/l;
float leftX = -1 + widthIndexAdder;
float rightX = leftX + widthAdder;
float bottomY = -1;
float topY = bottomY + arrayIndexHeightRation;
// bottom left
glColor4f(1,0,0,0);
glVertex2f(leftX, bottomY);
// bottom right
glColor4f(0,1,0,0);
glVertex2f(rightX, bottomY);
// top right
glColor4f(0,0,1,0);
glVertex2f(rightX, topY);
// top left
glColor4f(0,0,0,1);
glVertex2f(leftX, topY);
glEnd();
}
glFlush();
}
void swap(int index1, int index2)
{
std::swap(arr[index1], arr[index2]);
renderFunction();
usleep(delay);
}
void keyboardEvent(unsigned char c, int x, int y)
{
if(c == 27)
{
// exit on escape key pressed
exit(0);
free(arr);
}
else if(c == 115)
{
// start on `s` key pressed
sort(arr, length);
}
}
void menu(int n){
if (n == 0)
{
glutDestroyWindow(window);
exit(0);
}
else if(n==1)
{
sort(arr, length);
}
glutPostRedisplay();
}
void createMenu(void) {
returnmenu = glutCreateMenu(menu); //function to call menu function and return value
glutAddMenuEntry("Start", 1);
glutAddMenuEntry("Quit", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void setUpGlutAndArray(int argc, char** argv, void (*sortingAlgorithm)(int*, int))
{
sort = sortingAlgorithm;
arr = (int*) malloc(sizeof(int) * length);
for(int i = 0; i < length; ++i)
{
arr[i] = i;
}
randomizeArray(arr, length);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(length,length);
glutInitWindowPosition(100,100);
window=glutCreateWindow("Sort Visualization");
createMenu();
glutDisplayFunc(renderFunction);
glutKeyboardFunc(keyboardEvent);
glutMainLoop();
}