n阶贝塞尔曲线? [英] n-th order Bezier Curves?

查看:158
本文介绍了n阶贝塞尔曲线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法实现了二次和三次贝塞尔曲线。它们非常简单,因为我们有一个公式。现在我想用泛化来表示一个n阶贝塞尔曲线: b
$ b




/i.stack.imgur.com/98JCb.pngalt =在这里输入图片描述>






'I'使用位图库来渲染输出,所以这里是我的代码:

  // binomialCoef(n,k)=( factorial(n)/(factorial(k)* factorial(n-k)))
unsigned int binomialCoef(unsigned int n,const unsigned int k)
{
unsigned int r = 1 ;

if(k> n)
return 0;

(unsigned int d = 1; d <= k; d ++)
{
r * = n--;
r / = d;
}

return r;


void nBezierCurve(Bitmap * obj,const Point * p,const unsigned int nbPoint,float steps,const unsigned char red,const unsigned char green,const unsigned char blue)
{
int bx1 = p [0] .x;
int by1 = p [0] .y;
int bx2;
int by2;

steps = 1 / steps;

for(float i = 0; i <1; i + = steps)
{
bx2 = by2 = 0;
for(int j = 0;(unsigned int)j {
bx2 + =(int)(binomialCoef(nbPoint,j)* pow(1 - i ,(float)nbPoint - j)* pow(i,j)* p [j] .x);
by2 + =(int)(binomialCoef(nbPoint,j)* pow(1-i,(float)nbPoint -j)* pow(i,j)* p [j] .y);
}

bresenhamLine(obj,bx1,by1,bx2,by2,red,green,blue);

bx1 = bx2;
by1 = by2;
}

//曲线必须以最后一个定位点结束
bresenhamLine(obj,bx1,by1,p [nbPoint - 1] .x,p [nbPoint - 1] .y,红色,绿色,蓝色);
}

以下是要呈现的一组点:

  Point ncurv [] = {
20,200,
70,300,
200,400,
250,200
};

以下是输出:

< img src =https://i.stack.imgur.com/fQfv8.pngalt =在这里输入图片描述>



红色曲线是立方贝塞尔。蓝色的应该是四阶Bezier,与立方Bezier相同,但在这种情况下,他们是不一样的!!

编辑:
我忘了注意左下角的点是(0,0)

解决方案

你公式中的总和...





...从0运行到n,即对于一个n阶贝塞尔,你需要n + 1分。



你有4点,所以你正在画一个三阶bezier。



你的代码中的错误在这里:

  for(int j = 0;(unsigned int)j   

应该是:

  for(int j = 0;(unsigned int)j <= nbPoint; j ++)

否则您只是从0到n-1。





编辑:



出于兴趣,您得到的形状与缺少第五)点是(0,0),因为这是唯一的一点对你的总和没有贡献......

$ b


I've managed to implement quadratic and cubic Bezier curves.They are pretty straightforward since we have a formula. Now I want to represent an n-th order Bezier curve using the generalization:

Where

and

I'm using a bitmap library to render the output, so here is my code:

// binomialCoef(n, k) = (factorial(n) / (factorial(k) * factorial(n- k)))
unsigned int binomialCoef(unsigned int n, const unsigned int k)
{
    unsigned int r = 1;

    if(k > n)
        return 0;

    for(unsigned int d = 1; d <= k; d++)
    {
        r *= n--;
        r /= d;
    }

    return r;
}

void nBezierCurve(Bitmap* obj, const Point* p, const unsigned int nbPoint, float steps, const unsigned char red, const unsigned char green, const unsigned char blue)
{
    int bx1 = p[0].x;
    int by1 = p[0].y;
    int bx2;
    int by2;

    steps = 1 / steps;

    for(float i = 0; i < 1; i += steps)
    {
        bx2 = by2 = 0;
        for(int j = 0; (unsigned int)j < nbPoint; j++)
        {
            bx2 += (int)(binomialCoef(nbPoint, j) * pow(1 - i, (float)nbPoint - j) * pow(i, j) * p[j].x);
            by2 += (int)(binomialCoef(nbPoint, j) * pow(1 - i, (float)nbPoint - j) * pow(i, j) * p[j].y);
        }

        bresenhamLine(obj, bx1, by1, bx2, by2, red, green, blue);

        bx1 = bx2;
        by1 = by2;
    }

    // curve must end on the last anchor point
    bresenhamLine(obj, bx1, by1, p[nbPoint - 1].x, p[nbPoint - 1].y, red, green, blue);
}

Here's the set of points to render:

Point ncurv[] = {
                    20, 200,
                    70, 300,
                    200, 400,
                    250, 200
                };

and here's the output:

The red curve is a cubic Bezier. The blue one is supposed to be the 4th order Bezier, which is the same as cubic Bezier, but in this case, they are not the same ?!

EDIT : I forgot to note that the bottom left point is (0, 0)

解决方案

The sum in your formula...

...runs from 0 to n, ie for an n-th order bezier you need n+1 points.

You have 4 points, so you're drawing a 3rd-order bezier.

The error in your code is here:

for(int j = 0; (unsigned int)j < nbPoint; j++)

it should be:

for(int j = 0; (unsigned int)j <= nbPoint; j++)

otherwise you're only iterating from 0 to n-1.

EDIT:

Out of interest, the shape you were getting is the same as if the missing (5th) point was at (0,0), since that's the only point that would contribute nothing to your sum...

这篇关于n阶贝塞尔曲线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆