Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement a Bézier curve in C++?

Tags:

I'd like to implement a Bézier curve. I've done this in C# before, but I'm totally unfamiliar with the C++ libraries. How should I go about creating a quadratic curve?

void printQuadCurve(float delta, Vector2f p0, Vector2f p1, Vector2f p2);

Clearly we'd need to use linear interpolation, but does this exist in the standard math library? If not, where can I find it?

I'm using Linux.

like image 556
Nick Bolton Avatar asked Apr 24 '09 09:04

Nick Bolton


People also ask

How do you create a curve write the properties of B-spline curve?

Properties of B-spline Curve : Each basis function has 0 or +ve value for all parameters. Each basis function has one maximum value except for k=1. The degree of B-spline curve polynomial does not depend on the number of control points which makes it more reliable to use than Bezier curve.


1 Answers

Recently I ran across the same question and wanted to implemented it on my own. This image from Wikipedia helped me:

http://upload.wikimedia.org/wikipedia/commons/3/35/Bezier_quadratic_anim.gif

The following code is written in C++ and shows how to compute a quadratic bezier.

int getPt( int n1 , int n2 , float perc )
{
    int diff = n2 - n1;

    return n1 + ( diff * perc );
}    

for( float i = 0 ; i < 1 ; i += 0.01 )
{
    // The Green Line
    xa = getPt( x1 , x2 , i );
    ya = getPt( y1 , y2 , i );
    xb = getPt( x2 , x3 , i );
    yb = getPt( y2 , y3 , i );

    // The Black Dot
    x = getPt( xa , xb , i );
    y = getPt( ya , yb , i );

    drawPixel( x , y , COLOR_RED );
}

With (x1|y1), (x2|y2) and (x3|y3) being P0, P1 and P2 in the image. Just for showing the basic idea...

For the ones who ask for the cubic bezier, it just works analogue (also from Wikipedia):

http://upload.wikimedia.org/wikipedia/commons/a/a3/Bezier_cubic_anim.gif

This answer provides Code for it.

like image 141
Jakob Riedle Avatar answered Sep 23 '22 09:09

Jakob Riedle