/
SortingAlgorithmHandler.cs
210 lines (180 loc) · 5.8 KB
/
SortingAlgorithmHandler.cs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SortingAlgorithmHandler : MonoBehaviour
{
[Header("Settings")]
[Range(15,50)]
public int numberOfLines = 5;
List<Transform> objects;
List<float> objectsValues;
int index = 0;
int numberOfSteps;
int numberOfSwaps;
//Visualizer variables
float time = 0;
bool toggleAutomatic;
bool toggleSound = true;
//Audio
AudioSource source;
//Inicialize the scene
private void Start()
{
objects = new List<Transform>();
objectsValues = new List<float>();
source = GetComponent<AudioSource>();
GenerateLines();
}
//Create next step button
void OnGUI()
{
if (GUI.Button(new Rect(10, 40, 75, 30), "Next step"))
{
toggleAutomatic = false;
NextStep();
}
if (GUI.Button(new Rect(10, 90, 75, 30), "Generate"))
{
toggleAutomatic = false;
numberOfSteps = 0;
numberOfSwaps = 0;
if (numberOfLines != transform.childCount)
ResetScene();
else
HandleChangeLineValues();
}
toggleAutomatic = GUI.Toggle(new Rect(100, 40, 75, 30), toggleAutomatic, "Automatic");
toggleSound = GUI.Toggle(new Rect(100, 90, 75, 30), toggleSound, "Sound");
GUI.Label(new Rect(10, 10, 75, 30), "Steps:" + numberOfSteps);
GUI.Label(new Rect(100, 10, 75, 30), "Swaps:" + numberOfSwaps);
numberOfLines = (int)GUI.HorizontalSlider(new Rect(175, 10, 100, 30), numberOfLines, 15, 50);
}
//Automatic mode
private void Update()
{
if (!toggleAutomatic)
return;
if (time <= 0)
{
time = 0.01f;
NextStep();
}
else
time -= Time.deltaTime;
}
//Generate starting lines
void GenerateLines()
{
for (int i = 0; i < numberOfLines; i++)
{
CreateNewObject();
}
}
//Create objects in scene
void CreateNewObject()
{
//Create new gameObject in scene
GameObject obj = new GameObject();
obj.transform.parent = transform;
//Change the position y to screen bottom, change the x position to screen edge
obj.transform.position = new Vector3(
-SortingAlgorithm.ScreenWorldSize().x + ((objects.Count + 0.5f) * ((SortingAlgorithm.ScreenWorldSize().x * 2) / numberOfLines)), //Align to center
-SortingAlgorithm.ScreenWorldSize().y,
-10);
obj.name = objects.Count.ToString();
CreateNewLine(obj);
objects.Add(obj.transform);
}
//Add LineRenderer component to generated objects (called in GenerateLines())
void CreateNewLine(GameObject obj)
{
//Add line renderer and set random
LineRenderer line = obj.AddComponent<LineRenderer>();
line.material = SortingAlgorithm.lineMaterial(Color.white);
//Set size to .5f
line.SetWidth(.5f, .5f);
ChangeLinesValues(obj);
}
//Created for regenerate lines (Called in OnGUI generate button)
public void HandleChangeLineValues()
{
objectsValues.Clear();
for (int i = 0; i < numberOfLines; i++)
{
ChangeLinesValues(objects[i].gameObject);
}
}
void ResetScene()
{
objects.Clear();
objectsValues.Clear();
for (int i = 0; i < transform.childCount; i++)
{
Destroy(transform.GetChild(i).gameObject);
}
GenerateLines();
}
//Change line values not visuals
void ChangeLinesValues(GameObject obj)
{
//Get random number
float value = Random.Range(1f, SortingAlgorithm.ScreenWorldSize().y * 2);
//Change values to lists
objectsValues.Add(value);
//Update line positions values
UpdateLineValues(obj, value);
}
//Update visuals
void UpdateLineValues(GameObject obj,float value)
{
LineRenderer line = obj.GetComponent<LineRenderer>();
//Set Y size and position
line.SetPosition(0, (Vector2)line.transform.position);
line.SetPosition(1, new Vector2(line.transform.position.x, value - SortingAlgorithm.ScreenWorldSize().y));
}
//Visuals
void HighlightLines(int i)
{
objects[i].GetComponent<LineRenderer>().material = SortingAlgorithm.lineMaterial(Color.green);
}
//Audio
void PlaySound()
{
source.pitch = ((float)index / (float)objects.Count) + 1;
source.PlayOneShot(source.clip);
}
public void NextStep()
{
BubbleSort();
}
//Swap a object to b object
void SwapObjects(int aIndex, int bIndex)
{
Transform aObj = objects[aIndex];
Transform bObj = objects[bIndex];
float ia = objectsValues[aIndex];
float ib = objectsValues[bIndex];
//Cambia los valores (no mueve el objeto)
objectsValues[bIndex] = ia;
objectsValues[aIndex] = ib;
UpdateLineValues(aObj.gameObject, objectsValues[aIndex]);
UpdateLineValues(bObj.gameObject, objectsValues[bIndex]);
if(toggleSound) PlaySound();
numberOfSwaps++;
}
//Sort Algorithm
void BubbleSort()
{
if (numberOfSteps > Mathf.Pow(objects.Count, 2))
return;
if (index < objects.Count - 1)
{
if (objectsValues[index] > objectsValues[index + 1])
SwapObjects(index,index + 1);
index++;
numberOfSteps++;
}
else
index = 0;
}
}