Objective-C中的贝塞尔曲线算法 [英] Bezier curve algorithm in objective-c

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

问题描述

比我聪明的人可以看看这个吗?我正在尝试实施Bezier曲线算法,我在目标-此处 c.

Can someone smarter than me take a look at this. I'm trying to implement a Bezier curve algorithm I found here in objective-c.

输出错误.我想我正确地转换了代码,所以原来的代码是错误的或不是这样使用的……如果我使用内置的NSBezierPath,则曲线看起来不错,但我不能使用内置的NSBezierPath

The output is way wrong. I think I converted the code correctly so either the original was wrong or was not ment to be used like this... If I use the built in NSBezierPath the curve looks great but I can't use the built in NSBezierPath.

NSBezierPath *bezierPath = [[NSBezierPath alloc] init];
[bezierPath setLineWidth:1.0f];
[bezierPath moveToPoint:NSMakePoint(x1, y1)];
[bezierPath curveToPoint:NSMakePoint(x4, y4) controlPoint1:NSMakePoint(x2, y2) controlPoint2:NSMakePoint(x3, y3)];

我的代码试图绘制贝塞尔曲线

- (void)drawBezierFrom:(NSPoint)from to:(NSPoint)to controlA:(NSPoint)a controlB:(NSPoint)b color:(NSUInteger)color
{
    float qx, qy;
    float q1, q2, q3, q4;
    int plotx, ploty;
    float t = 0.0;
    
    while (t <= 1)
    {
        q1 = t*t*t*-1 + t*t*3 + t*-3 + 1;
        q2 = t*t*t*3 + t*t*-6 + t*3;
        q3 = t*t*t*-3 + t*t*3;
        q4 = t*t*t;
    
        qx = q1*from.x + q2*to.x * q3*a.x + q4*b.x;
        qy = q1*from.y + q2*to.y * q3*a.y + q4*b.y;
    
        plotx = round(qx);
        ploty = round(qy);

        [self drawPixelColor:color atX:plotx y:ploty];
    
        t = t + 0.003;
    }
}

编辑

请参见 objective-c中的贝塞尔曲线算法需要调整获取完整的功能贝塞尔曲线方法.

Edit

See Bezier curve algorithm in objective-c needs a tweak for a completed functional Bezier Curve method.

推荐答案

我在我的xy绘图仪上使用了这个Bezier函数,发现"to"有一个小错误. 需要切换to.x to.yb.x b.y,以便笔在from处开始并在to处结束.

I used this Bezier function with my xy plotter and found a small error with the 'to'. The to.x to.y and b.x b.y need to be switched so that the pen starts at from and ends at to.

qx = q1*from.x + q2*a.x + q3*b.x + q4*to.x;
qy = q1*from.y + q2*a.y + q3*b.y + q4*to.y;

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

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