help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Improve my Bézier curve vectorisation


From: Jordi Gutiérrez Hermoso
Subject: Improve my Bézier curve vectorisation
Date: Fri, 25 Feb 2011 09:10:39 -0600

     function B = bezier(P, t)
       n = length(P)-1;
       i = 0:n;
       a = bincoeff(n, 0:n) ;
       b = bsxfun(@power, 1-t, (n-i)');
       c = bsxfun(@power,  t,  i');
       d = bsxfun(@times, a', b.*c)';

       B = permute(sum(bsxfun(@times, d, permute(P, [3,2,1])),2),[3,1,2]);
     endfunction

The above function is meant to be used with a set of points P, in any
dimensionality, arranged column by column, and t is a parameter that
goes from 0 to 1, with the desired resolution. E.g. if you want the
Bézier curve that goes through (0,0), (1,2), (3,0), then P should be

     P = [ 0 1 3;
           1 2 0]

and t could be something like

     t = linspace(0,1,100);

The output is d by n matrix where d is the dimensionality of the
points in P and n is the length of t. The Bézier curve formula, in
case you can't read it from the above, is the general form below,
where the n in this formula is the dimensionality of P:

    http://en.wikipedia.org/wiki/B%C3%A9zier_curve#Generalization

Can my implementation be improved? It seems to be plenty fast, but I
think it looks too complicated, and the use of permute so that bsxfun
does the right thing looks like overkill.

I know there are better ways to implement Bézier curves, my question
here is only about the vectorisation details. Assuming that's the
formula I want to implement, did I vectorise it properly?

Thanks,
- Jordi G. H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]