Total circles area: Difference between revisions

Content added Content deleted
m (→‎{{header|C}}: note to self: don't monkey around with code while submitting)
m (→‎{{header|C}}: reduce unnecessary comparisons)
Line 203: Line 203:
typedef struct { F x0, x1; } sect_t;
typedef struct { F x0, x1; } sect_t;
sect_t sect[sizeof(circles) / sizeof(circle)];
sect_t sect[sizeof(circles) / sizeof(circle)];
inline void swap_s(int i, int j) {
sect_t ss;
ss = sect[i], sect[i] = sect[j], sect[j] = ss;
}


F spans(circle *c, int n_circs, F ymin, F step, F ymax)
F spans(circle *c, int n_circs, F ymin, F step, F ymax)
Line 208: Line 212:
F y, total = 0;
F y, total = 0;
int i, j, n, row = 1 + ceil((ymax - ymin) / step);
int i, j, n, row = 1 + ceil((ymax - ymin) / step);

circle cc;
inline void swap_c(int i, int j) {
sect_t ss;
circle cc;
cc = c[i], c[i] = c[j], c[j] = cc;
}


while (row--) {
while (row--) {
Line 215: Line 222:


for (n = n_circs, i = 0; i < n; ) {
for (n = n_circs, i = 0; i < n; ) {
if (y <= c[i].y0 || y >= c[i].y1)
if (y >= c[i].y1) ++i;
cc = c[i], c[i] = c[--n], c[n] = cc;
else if (y <= c[i].y0) {
swap_c(i, --n_circs);
else {
--n;
} else {
F dx = sqrt(c[i].r2 - sq(y - c[i].y));
F dx = sqrt(c[i].r2 - sq(y - c[i].y));
sect[i].x0 = c[i].x - dx;
sect[i].x0 = c[i].x - dx;
Line 231: Line 240:
for (j = 0; j < i; j++)
for (j = 0; j < i; j++)
if (sect[j].x0 > sect[j+1].x0) {
if (sect[j].x0 > sect[j+1].x0) {
ss = sect[j], sect[j] = sect[j+1], sect[j+1] = ss;
swap_s(j, j + 1);
cc = c[j], c[j] = c[j+1], c[j+1] = cc;
swap_c(j, j + 1);
swapped = 1;
swapped = 1;
}
}