## 贝塞尔曲线拟合

```CGPoint startPt = [[_points objectAtIndex:0] CGPointValue];
CGPoint endPt = [[_points objectAtIndex:(self.pointCount - 1)] CGPointValue];
float amount = endPt.x - startPt.x;
```

rank是指总的阶数，也就是实际的点数。这个函数表示n个点的贝塞尔曲线在x处的值。

```float (^bezierSpline)(int rank, float ux) = ^(int rank, float ux) {

float p = 0.0f;

for (int i = 0; i < rank; i++)
{
CGPoint pt = [[_points objectAtIndex:i] CGPointValue];

p += pt.y * powf((1 - ux), (rank - i - 1)) * powf(ux, i) * (factorial(rank - 1)
/ (factorial(i) * factorial(rank - i - 1)));
}

return p;
};
```

```    [path moveToPoint:startPt];

for (float curX = startPt.x; (curX - endPt.x) < 1e-5; curX += 1.0f)
{
float u = (curX - startPt.x) / amount;
CGPointMake(curX, bezierSpline(self.pointCount, u))];
}

CGContextSetLineWidth(context, 1.0f);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextStrokePath(context);
}
```

## 三次样条插值和曲线拟合

```        const int len = [_points count];
float x[len];
float y[len];
for (int i = 0; i < len; i++)
{
CGPoint p = [[_points objectAtIndex:i] CGPointValue];
x[i] = p.x;
y[i] = p.y;
}
```

```        float h[len];
float u[len];
float lam[len];
for (int i = 0; i < len-1; i++)
{
h[i] = x[i+1] - x[i];
}

u[0] = 0;
lam[0] = 1;
for (int i = 1; i < (len - 1); i++)
{
u[i] = h[i-1]/(h[i] + h[i-1]);
lam[i] = h[i]/(h[i] + h[i-1]);
}

float a[len];
float b[len];
float c[len];

float m[len][len];
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
m[i][j] = 0;
}
if (i == 0)
{
m[i][0] = 2;
m[i][1] = 1;

b[0] = 2;
c[0] = 1;
}
else if (i == (len - 1))
{
m[i][len - 2] = 1;
m[i][len - 1] = 2;

a[len-1] = 1;
b[len-1] = 2;
}
else
{
m[i][i-1] = lam[i];
m[i][i] = 2;
m[i][i+1] = u[i];

a[i] = lam[i];
b[i] = 2;
c[i] = u[i];
}
}
```

```        float g[len];
g[0] = 3 * (y[1] - y[0])/h[0];
g[len-1] = 3 * (y[len - 1] - y[len - 2])/h[len - 2];
for (int i = 1; i < len - 1; i++)
{
g[i] = 3 * ((lam[i] * (y[i] - y[i-1])/h[i-1]) + u[i] * (y[i+1] - y[i])/h[i]);
}
```

```        //< Solve the Equations
float p[len];
float q[len];

p[0] = b[0];
for (int i = 0; i < len - 1; i++)
{
q[i] = c[i]/p[i];
}

for (int i = 1; i < len; i++)
{
p[i] = b[i] - a[i]*q[i-1];
}

float su[len];
float sq[len];
float sx[len];

su[0] = c[0]/b[0];
sq[0] = g[0]/b[0];
for (int i = 1; i < len - 1; i++)
{
su[i] = c[i]/(b[i] - su[i-1]*a[i]);
}

for (int i = 1; i < len; i++)
{
sq[i] = (g[i] - sq[i-1]*a[i])/(b[i] - su[i-1]*a[i]);
}

sx[len-1] = sq[len-1];
for (int i = len - 2; i >= 0; i--)
{
sx[i] = sq[i] - su[i]*sx[i+1];
}
```

```        double (^func)(int k, float vX) = ^(int k, float vX) {
double p1 =  (ph[k] + 2.0 * (vX - px[k])) * ((vX - px[k+1]) * (vX - px[k+1])) * py[k] / (ph[k] *ph[k] * ph[k]);
double p2 =  (ph[k] - 2 * (vX - px[k+1])) * powf((vX - px[k]), 2.0f) * py[k+1] / powf(ph[k], 3.0f);
double p3 =  (vX - px[k]) * powf((vX - px[k+1]), 2.0f) * psx[k] / powf(ph[k], 2.0f);
double p4 =  (vX - px[k+1]) * powf((vX - px[k]), 2.0f) * psx[k+1] / powf(ph[k], 2.0f);
return p1 + p2 + p3 + p4;
};
```

```        for (int i = 0; i < [_points count]; i++)
{
CGPoint pt = [[_points objectAtIndex:i] CGPointValue];
if (i == 0)
{
CGPathMoveToPoint(path, NULL, pt.x, pt.y);
}
else
{
CGPoint curP = [[_points objectAtIndex:i-1] CGPointValue];
float delta = 1.0f;
for (float pointX = curP.x; fabs(pointX - pt.x) > 1e-5f; pointX += delta)
{
float pointY = func(i-1, pointX);
CGPathAddLineToPoint(path, NULL, pointX, pointY);
}
}
}
```