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
|
#include "../AnimationRender/AnimationRender.h"
#include "../AnimationRender/F_Draw.h"
#include "../AnimationRender/F_FrameRange.h"
#include "../AnimationRender/F_Interpolate.h"
#include "../AnimationRender/FrameCallback.h"
#include "../FrameRender/P_Circle.h"
#include "../FrameRender/P_Square.h"
#include "../FrameRender/P_TransformCenter.h"
#include "../FrameRender/PixelCallback.h"
#include <stdlib.h>
struct FrameCallbackArray {
FrameCallback** arr;
u32 count;
u32 allocated;
const u32* elements;
const TransformCenter* positions;
const PixelCallback** callbacksPositions;
};
void
FrameCallbackArray_push(struct FrameCallbackArray* fca, u32 from, u32 to, u32 start, u32 end) {
// resize
if (fca->count + 5 >= fca->allocated) {
fca->allocated = fca->allocated == 0 ? 8 : fca->allocated << 1;
FrameCallback** arrLarger = calloc(sizeof(FrameCallback*), fca->allocated);
for (unsigned i = 0; i < fca->count; ++i)
arrLarger[i] = fca->arr[i];
free(fca->arr);
fca->arr = arrLarger;
}
fca->arr[fca->count++] = NULL;
fca->arr[fca->count++] = FrameRange_setup((FrameRange){
.startFrame = start, .endFrame = end,
});
fca->arr[fca->count++] = Interpolate_setup((Interpolate){
.startValue = fca->positions + from, .endValue = fca->positions + to,
.value = fca->callbacksPositions[fca->elements[from]]->callback_self,
.interpolate = (f_interpolate)Interpolate_TransformCenter,
});
}
struct SwapCallbackArgs {
struct FrameCallbackArray* fca;
u32* frames;
u32 duration;
u32 rest;
};
void
swap_callback(struct SwapCallbackArgs* sca, u32 from, u32 to) {
FrameCallbackArray_push(sca->fca,
from, to, *sca->frames, *sca->frames + sca->duration);
FrameCallbackArray_push(sca->fca,
to, from, *sca->frames, *sca->frames + sca->duration);
*sca->frames += sca->duration + sca->rest;
}
void
swap_u32(struct SwapCallbackArgs* sca, u32* a, u32 i, u32 j) {
swap_callback(sca, i, j);
u32 temp = a[i];
a[i] = a[j];
a[j] = temp;
}
void
bubble_sort(u32* a, u32 length, struct SwapCallbackArgs* sca) {
for (u32 i = 0; i < length - 1; ++i) {
for (u32 j = 0; j < length - 1 - i; ++j) {
// Sorts in ascending order
if (a[j] > a[j+1])
swap_u32(sca, a, j, j+1);
}
}
}
void
insertion_sort(u32* a, u32 length, struct SwapCallbackArgs* sca) {
for (u32 i = 1; i < length; ++i) {
u32 j = i;
// Sorts in descending order
while (j > 0 && a[j-1] < a[j]) {
swap_u32(sca, a, j, j-1);
--j;
}
}
}
void
selection_sort(u32* a, u32 length, struct SwapCallbackArgs* sca) {
for (u32 i = 0; i < length - 1; ++i) {
int minind = i;
for (int j = i + 1; j < length; ++j) {
// Sorts in ascending order
if (a[j] < a[minind])
minind = j;
}
if (minind != i)
swap_u32(sca, a, i, minind);
}
}
int
main() {
/* Sesttings */
// Animation settings
u32 width = 960;
u32 height = 320;
const u32 FPS = 60;
// Elements settings
u32 elements[] = { 5, 4, 3, 2, 1, 0 };
#define ELEMENTS_COUNT (sizeof(elements) / sizeof(*elements))
u32 elementSpacing = 40;
/* Implementation */
// Spaced out elements
u32 minRadius = (width / ELEMENTS_COUNT) / 4 - elementSpacing / 2;
u32 maxRadius = (width / ELEMENTS_COUNT) / 2 - elementSpacing / 2;
TransformCenter positions[ELEMENTS_COUNT];
for (unsigned i = 0; i < ELEMENTS_COUNT; ++i) {
positions[i].centerX = i * width / ELEMENTS_COUNT + maxRadius + elementSpacing / 2;
positions[i].centerY = height / 2;
}
PixelCallback* callbacksPositions[ELEMENTS_COUNT];
PixelCallback* callbacksElements[1 + ELEMENTS_COUNT * 3] = {
/* Background */
Square_setup((Square){
.size = width, .color = 0xFFF0F0F0,
}),
};
for (unsigned i = 0; i < ELEMENTS_COUNT; ++i) {
double p = elements[i] / (ELEMENTS_COUNT - 1.0);
callbacksElements[1+i*3] = NULL;
callbacksElements[1+i*3+1] = callbacksPositions[elements[i]] = TransformCenter_setup(positions[i]);
callbacksElements[1+i*3+2] = Circle_setup((Circle){
.radius = minRadius * (1-p) + maxRadius * p,
.color = 0xFF000000 + 0xFFFF * p,
});
}
u32 callbacksElementsSize = 1 + ELEMENTS_COUNT * 3;
// Elements movement
u32 frames = FPS;
struct FrameCallbackArray fca = {
.arr = NULL,
.elements = elements,
.positions = positions,
.callbacksPositions = (const PixelCallback**)callbacksPositions,
};
struct SwapCallbackArgs sca = {
.fca = &fca,
.frames = &frames,
.duration = FPS * 1,
.rest = FPS * 0.5,
};
// Sort
bubble_sort(elements, ELEMENTS_COUNT, &sca);
insertion_sort(elements, ELEMENTS_COUNT, &sca);
selection_sort(elements, ELEMENTS_COUNT, &sca);
frames += FPS;
// Render
fca.arr[fca.count++] = NULL;
fca.arr[fca.count++] = Draw_setup((Draw){
.callbacks = (const PixelCallback**)callbacksElements, .sizeCallbacks = callbacksElementsSize,
});
FrameCallback** callbacksMove = fca.arr;
u32 callbacksMoveSize = fca.count;
RenderAnimation(width, height, frames, (const FrameCallback**)callbacksMove, callbacksMoveSize);
// Free
for (u32 i = 0; i < callbacksElementsSize; ++i) {
if (callbacksElements[i] == NULL) continue;
free(callbacksElements[i]->callback_self);
free(callbacksElements[i]);
}
for (u32 i = 0; i < callbacksMoveSize; ++i) {
if (callbacksMove[i] == NULL) continue;
free(callbacksMove[i]->callback_self);
free(callbacksMove[i]);
}
free(callbacksMove);
return 0;
}
|