-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortingVisualizer.c
97 lines (80 loc) · 2.4 KB
/
sortingVisualizer.c
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
#include <stdlib.h> // for exit(0) in android and srand() seed
#include <time.h> // for srand
#include "raylib.h"
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#ifdef PLATFORM_ANDROID
#define SCREENWIDTH 1600
#define SCREENHEIGHT 720
#define TOTALBARS 200
#else
#define SCREENWIDTH 700
#define SCREENHEIGHT 512
#endif // PLATFORM_ANDROID
#ifndef TOTALBARS
#define TOTALBARS 200
#endif
#define BARCLIPLENGTH 200
int bars[TOTALBARS];
int main()
{
int screenWidth = SCREENWIDTH, screenHeight = SCREENHEIGHT;
InitWindow(screenWidth, screenHeight, "Sorting Visualizer");
#ifdef PLATFORM_ANDROID
GuiSetStyle(DEFAULT, TEXT_SIZE, 56);
#endif
bool start = false;
bool firsttime = true;
bool sort = false;
int sortingindex = 0;
int BarSpacing = 3;
int shifter = 12;
srand(time(NULL));
for(int i = 0; i < TOTALBARS; i++)
{
bars[i] = rand() % BARCLIPLENGTH;
bars[i] = bars[i] > 1 ? bars[i] : rand() % BARCLIPLENGTH;
}
while (!WindowShouldClose())
{
if(start || IsKeyPressed(KEY_R))
{
firsttime = false;
srand(time(NULL));
for(int i = 0; i < TOTALBARS; i++)
{
bars[i] = rand() % BARCLIPLENGTH;
bars[i] = bars[i] > 1 ? bars[i] : rand() % BARCLIPLENGTH;
}
sort = true;
sortingindex = 0;
}
if(sort)
{
if(bars[sortingindex] > bars[sortingindex + 1])
{
int temp = bars[sortingindex];
bars[sortingindex] = bars[sortingindex + 1];
bars[sortingindex + 1] = temp;
}
sortingindex++;
if(sortingindex > TOTALBARS - 2)
sortingindex = 0;
}
BeginDrawing();
ClearBackground(BLACK);
#ifdef PLATFORM_ANDROID
start = GuiButton((Rectangle) {220, 100, 255, 105}, firsttime ? "START" : "RESTART");
#else
start = GuiButton((Rectangle) {120, 10, 65, 25}, firsttime ? "START" : "RESTART");
#endif
for(int i = 0; i < TOTALBARS; i++)
DrawLine( i * BarSpacing + shifter, screenHeight - 50, i * BarSpacing + shifter, screenHeight - 50 - bars[i] , RED);
if(sort) DrawText("SORTING", 520, 120, 21, RED);
EndDrawing();
if(IsKeyPressed(KEY_BACK)) break;
}
CloseWindow();
exit(0);
return 0;
}