Bézier 曲线没有画直

发布于 2024-12-20 13:36:35 字数 1389 浏览 2 评论 0原文

我确实使用 Objective-C 和 Cocos2D 制作了创建贝塞尔曲线的算法。这是我的代码

-(int)factorial:(int)x{
    int sum=1;
    int i;
    if(x == 0){
        return 1;
    }else{
        for(i=1;i<x;i++){
            sum = sum*i;
        }
        return sum;
        }
}

-(int)binomialCoefficient:(int)n:(int)i{
    int sum;
    //NSLog([NSString stringWithFormat:@"fac n-i=%f\n", fach] );
    sum = [self factorial:n]/([self factorial:i]*[self factorial:(n-i)]);

    return sum;
}

-(float)convertT:(int)t{
    return t*(0.001);
}

-(float)power:(float)a:(int)b{
        int i;
    float hasil=1;
    for(i=0;i<b;i++){
        hasil = hasil*a;
    }
    return hasil;
}

-(float)bernstein:(float)t:(int)n:(int)i{
    float sum = 0;

    sum = [self binomialCoefficient:n:i]*[self power:t :i]*[self power:(1-t) :(n-i)];
    //NSLog([NSString   stringWithFormat:@"yeah"]);
    return sum;
}

,为了实现,您只需放置 x 和 y 的数组并访问它。例如,要在控制曲线中绘制一个点,我是这样做的

float myPx = px[i];
float myPy = py[i];
posx = posx+([self bernstein:theT :banyak-1 :i]*myPx);
posy = posy+([self bernstein:theT :banyak-1 :i]*myPy);

,是的,这段代码没有给出完美的漂亮线条,但我尝试逐点绘制它。 它工作得很好,但是当我尝试使用 3 个点时,问题就出现了。用于弯曲线条的中间点的表现并不像我预期的那样。例如,如果我在这些坐标中放置 3 个点: (100,200) b(250,250) c(500,200)

它没有向上弯曲,而是向下弯曲。如果我想把它说直,我就必须把它说得更高。

我在语法或数据类型上做错了吗?或者这只是我的算法?

预先感谢

最好的问候 (抱歉我的英语不好)

I did make algorithm for creating Bézier Curve with Objective-C and Cocos2D. Here is my code

-(int)factorial:(int)x{
    int sum=1;
    int i;
    if(x == 0){
        return 1;
    }else{
        for(i=1;i<x;i++){
            sum = sum*i;
        }
        return sum;
        }
}

-(int)binomialCoefficient:(int)n:(int)i{
    int sum;
    //NSLog([NSString stringWithFormat:@"fac n-i=%f\n", fach] );
    sum = [self factorial:n]/([self factorial:i]*[self factorial:(n-i)]);

    return sum;
}

-(float)convertT:(int)t{
    return t*(0.001);
}

-(float)power:(float)a:(int)b{
        int i;
    float hasil=1;
    for(i=0;i<b;i++){
        hasil = hasil*a;
    }
    return hasil;
}

-(float)bernstein:(float)t:(int)n:(int)i{
    float sum = 0;

    sum = [self binomialCoefficient:n:i]*[self power:t :i]*[self power:(1-t) :(n-i)];
    //NSLog([NSString   stringWithFormat:@"yeah"]);
    return sum;
}

and for implementation you just put an array of x and y and access it. For example to draw a single dot in control curve I did it like this

float myPx = px[i];
float myPy = py[i];
posx = posx+([self bernstein:theT :banyak-1 :i]*myPx);
posy = posy+([self bernstein:theT :banyak-1 :i]*myPy);

Yes, this code doesn't give the perfect nice line, but I try to draw it dot by dot.
It works well, but the problem arise when I try to use 3 dots. The middle dot for curving the lines didn't behave like what I expected. For example if I put 3 dots in these coordinates:
a(100,200)
b(250,250)
c(500,200)

It didn't curving up but curving down. If I want to put it straight I have to put it all the way higher.

Am I do it wrong in syntax or data types? Or is it just my algorithm?

Thanks in advance

Best Regards
(sorry for my bad english)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别再吹冷风 2024-12-27 13:36:35

阶乘循环应该

for ( i = 1 ; i <= x ; i++ )

代替

for ( i = 1 ; i < x ; i++ )

The factorial loop should be

for ( i = 1 ; i <= x ; i++ )

instead of

for ( i = 1 ; i < x ; i++ )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文