哪个是使用8个三次贝塞尔曲线创建(近似)圆的控制点

时间:2015-11-10 12:50:26

标签: cocoa math geometry uibezierpath bezier

前提

我有一个圆圈,动画成8个贝塞尔曲线的形状。为了使过渡平滑,我需要用8个立方贝塞尔曲线制作圆。 这是我到目前为止所做的:

代码

- (UIBezierPath*)pathBubbleLeft {
    UIBezierPath *path = [UIBezierPath new];
    [path moveToPoint:p(sqlx, sqlMidy)];

    CGFloat r = sqlW/2;
    CGFloat sin45 = 0.7071 * r;
    CGFloat cos45 = 0.7071 * r;

    [path addRelativeCurveToPoint:point(sqlMidx - cos45, sqlMidy - sin45) control1:vector(0, 0.4) control2:vector(0.5, 0.8)];
    [path addRelativeCurveToPoint:point(sqlMidx, sqly) control1:vector(0.2, 0.5) control2:vector(0.4, 1)];

    [path addRelativeCurveToPoint:point(sqlMidx + cos45, sqlMidy - sin45) control1:vector(0.6, 0) control2:vector(0.8, 0.5)];
    [path addRelativeCurveToPoint:point(sqlMaxx, sqlMidy) control1:vector(0.5, 0.2) control2:vector(1, 0.5)];

    [path addRelativeCurveToPoint:point(sqlMidx + cos45, sqlMidy + sin45) control1:vector(0, 0.4) control2:vector(0.5, 0.8)];
    [path addRelativeCurveToPoint:point(sqlMidx, sqlMaxy) control1:vector(0.2, 0.5) control2:vector(0.4, 1)];

    [path addRelativeCurveToPoint:point(sqlMidx - cos45, sqlMidy + sin45) control1:vector(0.6, 0) control2:vector(0.8, 0.5)];
    [path addRelativeCurveToPoint:point(sqlx, sqlMidy) control1:vector(0.5, 0.2) control2:vector(1, 0.5)];

    return path;
}

路径从左开始顺时针(从pi到pi / 2,0,3pi / 4,pi)

pointvector是CGPointMake和CGVectorMake的短片

'sql'位于sqlxsqlysqlMidxsqlMidYsqlMaxx& sqlMaxy代表'squareLeft',圆圈的边界矩形。这些都是CGFloats。

addRelativeCurveToPoint用于相对于起点/终点定义控制点。 (0,0)开始,(1,1)结束。更容易阅读。

- (void)addRelativeCurveToPoint:(CGPoint)endPoint control1:(CGVector)controlPoint1 control2:(CGVector)controlPoint2 {
    CGPoint start = self.currentPoint;
    CGPoint end = endPoint;
    CGFloat x1 = start.x + controlPoint1.dx*(end.x - start.x);
    CGFloat x2 = start.x + controlPoint2.dx*(end.x - start.x);
    CGFloat y1 = start.y + controlPoint1.dy*(end.y - start.y);
    CGFloat y2 = start.y + controlPoint2.dy*(end.y - start.y);
    [self addCurveToPoint:endPoint controlPoint1:CGPointMake(x1, y1) controlPoint2:CGPointMake(x2, y2)];
}

到目前为止的结果: static

红色圆圈有点波浪状。这就是我想解决的问题。

下面,左侧圆圈使用上面的代码,右侧圆圈由4条曲线组成,顶部插入2个零长度,底部插入2个([path addLineToPoint:path.currentPoint];)。

enter image description here

从左边到花生中间过渡是好的,但从中间到右边是奇怪的

3 个答案:

答案 0 :(得分:5)

使用四个线段,使用三次贝塞尔曲线的圆近似不能比@fang在评论中给出的0.55228值更圆:它只是数学上唯一的值,立方体贝塞尔曲线最接近圆形。在无限精度表示中,它实际上是您获得的值:

     4        angle       4                     sqrt(2) - 1
k =  - * tan(-------)  =  - * tan(pi/8)  =  4 * -----------
     3          2         3                          3

并且是0.5522847498307933984022516322796 [...]。这可以为您提供4个段的最佳近似值,因此如果您需要使用8个段,我们需要一个不同的值,这意味着我们需要使用为 k 提供pi的角度的推导/ 2(四分之一圈),看看它为pi / 4(第八个圆圈)提供了什么。所以:我们将角度pi/4插入Primer on Bezier Curveapproximating circles with cubic curves部分中列出的函数中,我们得到:

start = {
  x: 1,
  y: 0
}

c1 = {
  x: 1,
  y: 4/3 * tan(pi/16)
}

c2 = {
  x: cos(pi/4) + 4/3 * tan(pi/16) * sin(pi/4)
  y: sin(pi/4) - 4/3 * tan(pi/16) * cos(pi/4)
}

e = {
  x: cos(pi/4)
  y: sin(pi/4)
}

给了我们这些(完全有用的)近似坐标:

s  = (1,           0)
c1 = (1,           0.265216...)
c2 = (0.894643..., 0.51957...)
e  = (0.7071...,   0.7071...)

那将是段1,然后其余的段只是通过对称得出,段2是:

s  = (0.7071...,   0.7071...)
c1 = (0.51957...,  0.894643...)
c2 = (0.265216..., 1)
e  = (0,           1)

这些坐标的演示覆盖了四分之一圆圈:http://jsbin.com/ridedahixu/edit?html,output

其余的是(+, - ),( - ,+)和( - , - )象限中明显的对称性。

这些是最佳可能近似值,因此:如果bezierPathWithOvalInRect(...)执行其他操作,那么不正确比我们几十年前制定的值=)< / p>

答案 1 :(得分:1)

当圆由四条贝塞尔曲线近似时,第一个控制点(在(1,0)起点之后)具有坐标(1,0.552)。对于8曲线情况,由于Bezies细分规则,它将为(1,0.276)。

所以你的控制向量是(0,0.276),(0.276,0),(0.195,0.195),具有不同的符号组合

答案 2 :(得分:0)

OP解决方案。

基于迈克的答案

- (CGRect)sql {
    return CGRectMake(0, 0, self.frameHeight, self.frameHeight);
}

CGPoint point(CGFloat x, CGFloat y) {return CGPointMake(x, y);}

#define sqlx (self.sql.origin.x)
#define sqly (self.sql.origin.y)
#define sqlMaxx (self.sql.origin.x + self.sql.size.width)
#define sqlMidx (self.sql.origin.x + self.sql.size.width/2)
#define sqlMaxy (self.sql.origin.y + self.sql.size.height)
#define sqlMidy (self.sql.origin.y + self.sql.size.height/2)
#define sqlW (self.sql.size.width)
#define sqlH (self.sql.size.height)


- (UIBezierPath*)pathBubbleLeft {
    UIBezierPath *path = [UIBezierPath new];
    [path moveToPoint:p(sqlx, sqlMidy)];

    CGFloat r = sqlW/2;
    CGFloat sin45 = sin(M_PI_4)*r;
    CGFloat cos45 = cos(M_PI_4)*r;

    CGFloat magic1 = (cos(M_PI_4) + 4/3.0 * tan(M_PI/16.0) * sin(M_PI_4))*r;
    CGFloat magic2 = (sin(M_PI_4) - 4/3.0 * tan(M_PI/16.0) * cos(M_PI_4))*r;
    CGFloat magic3 = 4/3.0 * tan(M_PI/16.0)*r;

    [path addCurveToPoint:point(sqlMidx - cos45, sqlMidy - sin45)
            controlPoint1:point(sqlx, sqlMidy - magic3)
            controlPoint2:point(sqlMidx - magic1, sqlMidy - magic2)];

    [path addCurveToPoint:point(sqlMidx, sqly)
            controlPoint1:point(sqlMidx - magic2 , sqlMidy - magic1)
            controlPoint2:point(sqlMidx - magic3, sqly)];

    [path addCurveToPoint:point(sqlMidx + cos45, sqlMidy - sin45)
            controlPoint1:point(sqlMidx + magic3, sqly)
            controlPoint2:point(sqlMidx + magic2 , sqlMidy - magic1)];

    [path addCurveToPoint:point(sqlMaxx, sqlMidy)
            controlPoint1:point(sqlMidx + magic1 , sqlMidy - magic2)
            controlPoint2:point(sqlMaxx, sqlMidy - magic3)];

    [path addCurveToPoint:point(sqlMidx + cos45, sqlMidy + sin45)
            controlPoint1:point(sqlMaxx, sqlMidy + magic3)
            controlPoint2:point(sqlMidx + magic1 , sqlMidy + magic2)];

    [path addCurveToPoint:point(sqlMidx, sqlMaxy)
            controlPoint1:point(sqlMidx + magic2 , sqlMidy + magic1)
            controlPoint2:point(sqlMidx + magic3, sqlMaxy)];

    [path addCurveToPoint:point(sqlMidx - cos45, sqlMidy + sin45)
            controlPoint1:point(sqlMidx - magic3 , sqlMaxy)
            controlPoint2:point(sqlMidx - magic2 , sqlMidy + magic1)];

    [path addCurveToPoint:point(sqlx, sqlMidy)
            controlPoint1:point(sqlMidx - magic1 , sqlMidy + magic2)
            controlPoint2:point(sqlx, sqlMidy + magic3)];

    return path;
}

结果:  浅灰色是pathBubbleLeft,红色描边是bezierPathWithOvalInRect(恕我直言,这很完美)。

enter image description here

观察:

此代码适用于iOS,其坐标(0,0)位于左上方,坐标(320,548)位于右下方。对于MacOS(0,0)位于左下方;

绘图从9点开始,并顺时针旋转;