c++ - 如何在 C++ 中实现贝塞尔曲线?

标签 c++ math interpolation spline

我想实现一个 Bézier curve .我以前在 C# 中做过这个,但我对 C++ 库完全不熟悉。我应该如何创建二次曲线?

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

显然我们需要使用线性插值,但这是否存在于标准数学库中?如果没有,我在哪里可以找到它?

我正在使用 Linux。

最佳答案

最近我遇到了同样的问题,想自己实现它。 这张来自维基百科的图片帮助了我:

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

以下代码是用 C++ 编写的,展示了如何计算二次贝塞尔曲线。

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 );
}

图像中的 (x1|y1)、(x2|y2) 和 (x3|y3) 分别为 P0、P1 和 P2。只是为了展示基本思想......

对于那些要求三次贝塞尔曲线的人,它只是模拟的(也来自维基百科):

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

This答案提供了代码。

关于c++ - 如何在 C++ 中实现贝塞尔曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/785097/

相关文章:

matlab - 用样条勾勒出你的手(Matlab)

javascript - 如何根据对数要求创建尺寸公式

c++ - 浮点平方根算法

video - ffmpeg 运动插值替代方案或加速

c++ - 将带有随机 char 元素的 char[] 转换为一个 int

java - 在java中测试公式中的括号是否匹配,我的算法是否正确?

graphics - 将四边形图像提取为矩形

c++ - 从一个线程中捕获 OpenCV 相机

c++ - 在 C++11 中,std::atomic 是否可以用于在两个线程之间传输非原子数据

c++ - 没有 edge_predicate 的 BOOST filtered_graph 中的 out_edges() 实现