-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.cpp
190 lines (165 loc) · 4.72 KB
/
App.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
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
#include "App.h"
#include "shuffle.h"
#include "sorts.h"
#include <iostream>
#include <string>
#include <sstream>
// For srand and time
#include <cstdlib>
#include <ctime>
App::App(const char* label, int x, int y, int w, int h): GlutApp(label, x, y, w, h),quick(Replay(state, "Quick Sort")),heap(Replay(state, "Heap Sort")),insert(Replay(state, "Insertion Sort")),
shell(Replay(state, "Shell Sort")),bubble(Replay(state, "Bubble Sort")),comb(Replay(state, "Comb Sort")),slow(Replay(state, "Slow Sort")),speed(0),counter(0){
// Initialize state variables
std::srand(std::time(0));
for(int i = 0; i < 50; i++){
state.data.push_back(i+1);
}
shuffle(state.data);
quick.getSteps().push_back(new Step());
quickSort(Recorder(quick.getSteps(),state),0,state.data.size()-1);
quick.gotoEnd();
quick.reset();
slow.getSteps().push_back(new Step());
slowSort(Recorder(slow.getSteps(),state),0,state.data.size()-1);
slow.gotoEnd();
slow.reset();
insert.getSteps().push_back(new Step());
insertionSort(Recorder(insert.getSteps(),state),state.data.size());
insert.gotoEnd();
insert.reset();
bubble.getSteps().push_back(new Step());
bubbleSort(Recorder(bubble.getSteps(),state),state.data.size());
bubble.gotoEnd();
bubble.reset();
comb.getSteps().push_back(new Step());
combSort(Recorder(comb.getSteps(),state),state.data.size());
comb.gotoEnd();
comb.reset();
shell.getSteps().push_back(new Step());
shellSort(Recorder(shell.getSteps(),state),state.data.size());
shell.gotoEnd();
shell.reset();
heap.getSteps().push_back(new Step());
heapSort(Recorder(heap.getSteps(),state),state.data.size());
heap.gotoEnd();
heap.reset();
rep = &quick;
}
void App::draw() {
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set background color to black
glClearColor(0.0, 0.0, 0.0, 1.0);
// Set up the transformations stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//draw number
glColor3f(1.0,1.0,1.0);
glRasterPos2f( -.95, .9);
//std::cout << state.step << std::endl;
std::stringstream s;
s << "step " << state.step << "/" << rep->getSteps().size() << " " << rep->name << " Speed " << speed;
std::string pos = s.str();
for(int i = 0; i < pos.length(); i++){
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, pos[i]);
}
float width = 2.0 / state.data.size();
float spacing = 0.2*width;
// Draw histogram
for(int i = 0; i < state.data.size(); i++){
switch(rep->step()->getColor(i)){
case WHITE:
glColor3f(1.0, 1.0, 1.0);
break;
case GREEN:
glColor3f(0.0, 1.0, 0.0);
break;
case RED:
glColor3f(1.0,0.0,0.0);
break;
case BLUE:
glColor3f(0.0, 0.0, 1.0);
break;
}
glRectf(i*width-1+spacing/2, -1, (i*width-1+spacing/2) + width-spacing, (float)state.data[i]/(float)state.data.size()*1.8-1);
}
// We have been drawing everything to the back buffer
// Swap the buffers to see the result of what we drew
glFlush();
glutSwapBuffers();
}
void App::keyPress(unsigned char key) {
if(key == 'h'){
rep->reset();
rep = &heap;
}else if(key == 's'){
rep->reset();
rep = &slow;
}else if(key == 'q'){
rep->reset();
rep = &quick;
}else if(key == 'i'){
rep->reset();
rep = &insert;
}else if(key == 'l'){
rep->reset();
rep = &shell;
}else if(key == 'b'){
rep->reset();
rep = &bubble;
}else if(key == 'c'){
rep->reset();
rep = &comb;
}else if(key == '['){
speed--;
}else if(key == ']'){
speed++;
}else if(key == '-'){
speed-=10;
}else if(key == '='){
speed+=10;
}else if(key == 'r'){
speed = 0;
}else if(key == 27){
// Exit the app when Esc key is pressed
exit(0);
}else{
return;
}
redraw();
}
void App::specialKeyPress(int key) {
if (speed != 0) return;
if(key == 103){
++(*rep);++(*rep);
++(*rep);++(*rep);
++(*rep);++(*rep);
++(*rep);
}else if(key == 102){
++(*rep);
}else if(key == 101){
--(*rep);--(*rep);
--(*rep);--(*rep);
--(*rep);--(*rep);
--(*rep);
}else if(key == 100){
--(*rep);
}
redraw();
}
void App::idle(){
/*if(play && speed > 0 && (counter = (counter + 1) % speed) == 0) ++(*rep);
if(play && speed < 0 && (counter = counter + 1) % -speed == 0) --(*rep);*/
if(speed != 0){
counter+= speed;
while(counter > 10){
++(*rep);
counter -= 10;
}
while(counter < -10){
--(*rep);
counter += 10;
}
redraw();
}
}