www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Beauty of D

This is a great example of how beautiful D really is.

/// Returns the t-th point on the bezier curve, defined by non-empty
set p of d-dimensional points, where t : [0, 1] and d > 1.
real[d] bezier(size_t d)(real[d][] p, real t)
    if(d > 1)
in
{
    assert(p.length > 0);
    assert(t >= 0.0L && t <= 1.0L);
}
body
{
    return p.length > 1 ? (1 - t) * p[0..$-1].bezier(t) + t *
p[1..$].bezier(t) : p[0];
}

/// Returns k unidistant points on the bezier curve, defined by
non-empty set p of d-dimensional points, where k > 0 and d > 1.
real[d][] bezier(size_t d)(real[d][] p, size_t k)
    if(d > 1)
in
{
    assert(p.length > 0);
    assert(k > 0);
}
body
{
    real[d][] result = new real[k];
    foreach(i; 0..k)
        result[k] = p.bezier(i * (1.0L / k));
    return result;
}

-- 
Bye,
Gor Gyolchanyan.
Apr 18 2012