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) { |
|||
⚫ | |||
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); |
||
⚫ | |||
inline void swap_c(int i, int j) { |
|||
⚫ | |||
⚫ | |||
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 ( |
if (y >= c[i].y1) ++i; |
||
else if (y <= c[i].y0) { |
|||
swap_c(i, --n_circs); |
|||
⚫ | |||
--n; |
|||
⚫ | |||
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) { |
||
swap_s(j, j + 1); |
|||
swap_c(j, j + 1); |
|||
swapped = 1; |
swapped = 1; |
||
} |
} |