-
Notifications
You must be signed in to change notification settings - Fork 1
/
circle.c
217 lines (185 loc) · 4.78 KB
/
circle.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
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
211
212
213
214
215
216
217
#define GL_GLEXT_PROTOTYPES
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/glext.h>
#include <GL/glcorearb.h>
#include "circle.h"
#include "image.h"
#define COMPARE_VAL(a, b) \
do { \
if(a < b) \
return -1; \
if(a > b) \
return 1; \
} while(0)
static int _random_compare(const void *d1, const void *d2)
{
int ret = (int)get_rand() % 3;
ret--;
return ret;
}
static int _circle_compare(const struct ia_circle *c1,
const struct ia_circle *c2)
{
COMPARE_VAL(c1->radius, c2->radius);
// Color
COMPARE_VAL(c1->color.r, c2->color.r);
COMPARE_VAL(c1->color.g, c2->color.g);
COMPARE_VAL(c1->color.b, c2->color.b);
// Position
COMPARE_VAL(c1->x, c2->x);
COMPARE_VAL(c1->y, c2->y);
return 0;
}
static int _id_compare(const struct ia_circle *c1,
const struct ia_circle *c2)
{
COMPARE_VAL(c1->id, c2->id);
}
static void _draw_circles(struct ia_circles *circles)
{
int idx, jdx;
float x1,y1,x2,y2;
double radius;
for(idx = 0; idx < circles->num_circles; idx++) {
glBegin(GL_POLYGON);
x1 = (float)circles->circles[idx].x;
y1 = (float)circles->circles[idx].y;
glColor3ub(circles->circles[idx].color.r,
circles->circles[idx].color.g,
circles->circles[idx].color.b);
radius = (double)circles->circles[idx].radius;
float theta = 0;
float theta2 = 6 * 3.14159265359 / 180;
for(jdx = 0; jdx < 60; jdx++) {
x2 = x1 + radius*sin(theta + theta2);
y2 = y1 + radius*cos(theta + theta2);
theta += theta2;
glVertex2f(x2,y2);
}
#if 0
for(angle = 1.0f; angle < 361.0f; angle += 1.71) {
x2 = x1 + sin(angle) * radius;
y2 = y1 + cos(angle) * radius;
glVertex2f(x2,y2);
}
#endif
glEnd();
}
}
static void _render(struct ia_circles *l_circles)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ONE, GL_ZERO);
_draw_circles(l_circles);
glDisable(GL_BLEND);
glFlush();
}
void refresh_circles(struct ia_circles *l_circles)
{
if(ia_cfg.quit) {
return;
}
img_free(l_circles->img);
_render(l_circles);
l_circles->img = img_from_GL();
img_assign_score(l_circles->img, ia_cfg.reference_image);
}
#define INIT_CIRCLE_SIZE 10
void init_circles(struct ia_circles *circles,
uint64_t number_circles)
{
//num_circles = screen_width * screen_height / 10 * 5;
struct ia_circle *circle;
circle = calloc(number_circles, sizeof(struct ia_circle));
if(!circle) {
printfd("memory allocation error");
memset(circles, 0, sizeof(struct ia_circles));
return;
}
circles->circles = circle;
circles->num_circles = number_circles;
for(int ix = 0; ix < circles->num_circles; ix++) {
circles->circles[ix].id = ix;
circles->circles[ix].x = get_rand() % ia_cfg.screen_width;
circles->circles[ix].y = get_rand() % ia_cfg.screen_height;
circles->circles[ix].color.r = get_rand() % INIT_CIRCLE_SIZE;
circles->circles[ix].color.g = get_rand() % INIT_CIRCLE_SIZE;
circles->circles[ix].color.b = get_rand() % INIT_CIRCLE_SIZE;
circles->circles[ix].radius = get_rand() % INIT_CIRCLE_SIZE + 5;
}
circles->my_index = -1;
circles->father_index = -1;
circles->father2_index = -1;
circles->mother_index = -1;
_render(circles);
circles->img = img_from_GL();
return;
}
static void _sort_circles(struct ia_circles *circles, bool random)
{
if(ia_cfg.quit) {
return;
}
if(!circles) {
printfd("circles is null");
return;
}
if(circles->num_circles == 0) {
return;
}
if(!circles->circles) {
printfd("circles->circles is null");
return;
}
if(random) {
qsort(circles->circles, circles->num_circles, sizeof(struct ia_circle),
(__compar_fn_t)_random_compare);
} else {
qsort(circles->circles, circles->num_circles, sizeof(struct ia_circle),
(__compar_fn_t)_id_compare);
// (__compar_fn_t)_circle_compare);
}
}
void sort_circles(struct ia_circles *circles)
{
_sort_circles(circles, false);
}
void random_sort_circles(struct ia_circles *circles)
{
_sort_circles(circles, true);
}
struct ia_circles *clone_circles(struct ia_circles *c)
{
struct ia_circles *ret_circles = NULL;
if(!c) {
return NULL;
}
ret_circles = calloc(1, sizeof(struct ia_circles));
ret_circles->num_circles = c->num_circles;
ret_circles->img = img_clone(c->img);
ret_circles->circles = calloc(c->num_circles,
sizeof(struct ia_circle));
memcpy(ret_circles->circles, c->circles,
ret_circles->num_circles * sizeof(struct ia_circle));
ret_circles->my_index = c->my_index;
ret_circles->father_index = c->father_index;
ret_circles->father2_index = c->father2_index;
ret_circles->mother_index = c->mother_index;
return ret_circles;
}
void free_circles(struct ia_circles *c)
{
if(!c) {
return;
}
free(c->circles);
img_free(c->img);
}
#if 0
const char *circles_to_svg(struct ia_circles *circles);
#endif