[ create a new paste ] login | about

Link: http://codepad.org/NFvyfNR1    [ 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 inf = reduce!((acc, c) => min(acc, c.x - c.r))(Fp.max, circles);
    immutable Fp sup = reduce!((acc, c) => max(acc, c.x + c.r))(cast(Fp)0, circles);

    enum Fp mult = 100_000;
    Fp t = 0;

    foreach (p; cast(int)(inf * mult) .. cast(int)(sup * mult) + 1) {
        immutable Fp x = p / mult;
        alias Tuple!(Fp,"y0", Fp,"y1") Pair;
        Pair[] ranges;
        foreach (c; circles) {
            immutable Fp d = c.r ^^ 2 - (c.x - x) ^^ 2;
            immutable Fp s = sqrt(d);
            if (d > 0)
                ranges ~= Pair(c.y - s, c.y + s);
        }
        ranges.sort();
        Fp y = -Fp.max;
        foreach (r; ranges)
            if (y < r.y1) {
                t += r.y1 - max(y, r.y0);
                y = r.y1;
            }
    }

    writefln("%.17f", t / mult); // 9.73178438409874325
}                                // 9.73178175169        (Python)


Create a new paste based on this one


Comments: