[ create a new paste ] login | about

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

D, pasted on Sep 6:
import std.stdio, std.math, std.algorithm, std.typecons,
       std.string, std.conv;

void main() {
    immutable rawData =
    "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 real Fp;
    alias Tuple!(Fp,"x", Fp,"y", Fp,"r") Circle;

    Circle[] circles;
    foreach (immutable string line; rawData.splitLines()) {
        auto parts = line.split().map!(to!Fp)();
        circles ~= Circle(parts[0], parts[1], parts[2]);
    }

    immutable Fp infx = reduce!((acc, c) => min(acc, c.x - c.r))(Fp.max, circles);
    immutable Fp supx = reduce!((acc, c) => max(acc, c.x + c.r))(cast(Fp)0, circles);

    immutable Fp infy = reduce!((acc, c) => min(acc, c.y - c.r))(Fp.max, circles);
    immutable Fp supy = reduce!((acc, c) => max(acc, c.y + c.r))(cast(Fp)0, circles);

    enum bins = 900;

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

    auto mat = new bool[][](bins, bins);

    foreach (r, row; mat) {
        immutable Fp y = infy + r * dy;
        foreach (c, ref b; row) {
            immutable Fp x = infx + c * dx;
            foreach (circle; circles) {
                if ((x - circle.x) ^^ 2 + (y - circle.y) ^^ 2 <= (circle.r ^^ 2)) {
                    b = true;
                    break;
                }
            }
        }
    }

    immutable total = reduce!((acc, row) => acc + count!q{a}(row))(0, mat);
    writeln(total * dx * dy); // 9.731
}


Create a new paste based on this one


Comments: