[ create a new paste ] login | about

Link: http://codepad.org/2aNy3Umc    [ raw code | fork ]

D, pasted on Sep 10:
import std.stdio, std.math, std.algorithm, std.typecons;

void main() {
    alias real Fp;
    static struct Circle { Fp x, y, r; }

    immutable Circle[] circles = [
        { 0.479477, -0.634017, 0.137317},
        {-0.568894, -0.450312, 0.211238},
        {-0.907263, -0.434144, 0.668432},
        { 0.279875,  0.309700, 0.242502},
        {-0.999968, -0.910107, 0.455271},
        { 0.889064, -0.864342, 1.292949},
        {-0.701553,  0.285499, 0.321359},
        {-0.947186,  0.261604, 0.028034},
        { 0.805749, -0.175108, 0.688808},
        { 0.813269, -0.117034, 0.340474},
        {-0.630897, -0.659249, 0.298656},
        {-0.054129, -0.661273, 0.270216},
        { 0.042748,  0.469534, 0.759090},
        { 0.079393, -0.803786, 0.635903},
        {-0.987166,  0.561186, 0.740386},
        {-0.246960, -0.774309, 1.035616},
        {-0.189155, -0.244443, 0.187699},
        { 0.683683, -0.569687, 0.275045},
        {-0.249028, -0.452500, 0.713051},
        {-0.070789, -0.898363, 0.135069}];

    alias Tuple!(Fp,"infx", Fp,"supx", Fp,"infy", Fp,"supy") BBox;

    immutable BBox bb = reduce!((acc, c) => min(acc, c.x - c.r),
                                (acc, c) => max(acc, c.x + c.r),
                                (acc, c) => min(acc, c.y - c.r),
                                (acc, c) => max(acc, c.y + c.r))
                               (BBox(Fp.max, cast(Fp)0, Fp.max, cast(Fp)0), circles[]);

    enum bins = 900;

    immutable Fp dx = (bb.supx - bb.infx) / bins;
    immutable Fp dy = (bb.supy - bb.infy) / bins;

    uint count = 0;
    foreach (r; 0 .. bins) {
        immutable Fp y = bb.infy + r * dy;
        foreach (c; 0 .. bins) {
            immutable Fp x = bb.infx + c * dx;
            foreach (immutable Circle circle; circles) {
                if ((x - circle.x) ^^ 2 + (y - circle.y) ^^ 2 <= (circle.r ^^ 2)) {
                    count++;
                    break;
                }
            }
        }
    }

    writeln(count * dx * dy); // 9.731
}


Create a new paste based on this one


Comments: