简体   繁体   中英

Calculate Bezier curve and draw

I want to Calculate Bezier Curve which should return me x,y where I can plug and draw the line. I have to use this formula

在此处输入图像描述

where t is in [0,1]

   struct myPoint {
        int x;
        int y;
    };

void drawBezier(myPoint p0, myPoint p1, myPoint p2, myPoint p3) {
    myPoint result;
    
    for(double t = 0.0; t<=1.0; t+=0.001){
        result = pow((1-t),3) * p0 + 3 * pow((1-t),2) * t * p1 + 3 * (1-t) * pow(t,2) * p2 + pow(t,3) * p3;
    }
    
}

How can I actually receive x,y out of this? I am not sure how to calculate this correctly. Can someone give me advice

In the above code, you don't need to return anything, since the loop will give you a result for each iteration, so you can draw the result after each iteration directly.

In case you need to return something from the function, you need to make the loop outside the function itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM