如何使用Bezier绘制曲线?

时间:2011-12-07 06:14:28

标签: cocos2d-iphone line bezier

我画了一行 - (void)draw方法:

-(void)draw // code for line draw
  {
glEnable(GL_LINE_SMOOTH);   
CGPoint start;
start.x = 50;
start.y = 50;
CGPoint end;
end.x = 50;
end.y = 200;

if (pointOne.x>300){
    pointOne.x = 300;
}
if (pointOne.y>200){
    pointOne.y = 200;
}
ccDrawLine(start, pointOne);//get a line

[self Bezier:location.x:location.y:pointOne.x:pointOne.y];
  }

现在我想通过cocos2d中的Bezier曲线。当我移动手指时,时间线应绘制曲线。

Bezier Code是:

    - (void) Bezier:(NSInteger) CP_x:(NSInteger) CP_y:(NSInteger) end_x:(NSInteger) end_y
{

CGPoint start;
start.x = 50;
start.y = 50;

ccBezierConfig bezier;
bezier.controlPoint_1 = ccp(CP_x, CP_y);
bezier.controlPoint_2 = ccp(CP_x,CP_y);
bezier.endPosition = ccp(end_x,end_y);
  }

如何在bezier中实现此行?

2 个答案:

答案 0 :(得分:0)

试试这个。 CCDrawBezier在你的最后一个 - (void)Bezier methord

ccDrawCubicBezier(StartPoint, controlPoint_1, controlPoint_2, EndPoint,NSInteger);

答案 1 :(得分:0)

这是一种写入我游戏的方法。

//Bezier calculation on multiple points
//Can be set to any number of points

//The t parameter is the time 0 = start point -> 1 = end point.

//Made by Sebastian Winbladh

-(CGPoint)getBezerAtTime:(float)t array:(NSArray*)a{

    int count = [a count];
    float xPoints[count],yPoints[count];

    int c=0;

    //Makeup an array for our points
    for(int i=0;i<count;i++){

        CGPoint p = CGPointMake([[[a objectAtIndex:i] objectAtIndex:0] floatValue], [[[a objectAtIndex:i] objectAtIndex:1] floatValue]);
        xPoints[i] = p.x;
        yPoints[i] = p.y;

        c++;

    }

    //Calculate our bezier curve here
    while(c != 0){
        for(int i=0;i<c-1;i++){

            CGPoint p0 = CGPointMake(xPoints[i], yPoints[i]);
            CGPoint p1 = CGPointMake(xPoints[i+1], yPoints[i+1]);

            float dx = p1.x - p0.x;
            float dy = p1.y - p0.y;

            dx = p0.x + (dx * t);
            dy = p0.y + (dy * t);

            xPoints[i] = dx;
            yPoints[i] = dy;

        }c--;
    }

    return CGPointMake(xPoints[0], yPoints[0]);

}