Exquisite code samples

Caligo iteronvexor at gmail.com
Tue Jul 10 10:09:17 PDT 2012


It is suppose to compile?

I get:
t4.d(16): Error: incompatible types for ((cast(real)1 - t) *
(bezier(p[0u..__dollar - 1u],t))): 'real' and 'real[3u]'
t4.d(16): Error: incompatible types for ((t) *
(bezier(p[1u..__dollar],t))): 'real' and 'real[3u]'
t4.d(43): Error: template instance t4.bezier!(3u,real) error instantiating

On Mon, Jul 9, 2012 at 6:16 AM, Gor Gyolchanyan
<gor.f.gyolchanyan at gmail.com> wrote:
> I've put together a code sample, which could demonstrate the awesome power
> of D when it comes to getting good results very quickly and safely. Perhaps
> it could end up on display for newcomers:
>
> import std.traits;
>
> /// 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, Number)(Number[d][] p, Number t)
>     if(d > 1 && isFloatingPoint!Number)
> 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, Number)(Number[d][] p, size_t k)
>     if(d > 1 && isFloatingPoint!Number)
> in
> {
>     assert(p.length > 0);
>     assert(k > 0);
> }
> body
> {
>     Number[d][] result = new Number[d][k];
>     foreach(i; 0..k)
>         result[k] = p.bezier(i * (1.0L / k));
>     return result;
> }
>
> --
> Bye,
> Gor Gyolchanyan.


More information about the Digitalmars-d mailing list