iOS 三次B样条曲线插值算法

points 为要插值的样条曲线型值点数组
fitPoints 为插值后的样条曲线型值点数组

- (void)fitSplineWith:(NSMutableArray *)points fitPoint:(NSMutableArray *)fitPoints {
    
    // 将坐标点数组CGPoint值转化为x,y形式
    const int len = (int)[points count];
    float x[len];
    float y[len];
    for (int i = 0; i < len; i++)
    {
        CGPoint p = [[points objectAtIndex:i] CGPointValue];
        // 检测出的点坐标是y,x形式,需要转换下
        x[i] = ceilf(p.y);
        y[i] = p.x;
        NSLog(@"%f,%f",x[i],y[i]);
    }
    
    
    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]);
    }
    
    // 追逐法求解方程组得到【系数】
    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];
    }
    
    float *ph = h;
    float *px = x;
    float *psx = sx;
    float *py = y;
    
    /**
     曲线函数方程组 传入节点X和自变量vx值计算Y值

     @k 第几曲线段
     @vX 几点X坐标值
     返回几点Y坐标值
     */
    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;
    };
    
    // 根据步长插入节点,计算y值
    for (int i = 0; i < [points count]; i++)
    {
        int count = 0;
        for (NSValue *val in points) {
            count++;
            CGPoint p = [val CGPointValue];
            
            NSLog(@"插值点:%f,%f  === Count:%d",p.x,p.y,count);
        }
        // 起点
        CGPoint p0 = [[points objectAtIndex:i] CGPointValue];  
        CGPoint pt = CGPointMake(ceilf(p0.y), p0.x);
        if (i == 0)
        {
//            [fitPoints addObject:[NSValue valueWithCGPoint:pt]];
        }
        else
        {
            CGPoint temP = [[points objectAtIndex:i-1] CGPointValue];
            CGPoint curP = CGPointMake(ceilf(temP.y), temP.x);
            
            // 步长
            float delta = 1.0f;
            
            for (float pointX = curP.x; fabs(pointX - pt.x) > 1e-4f; pointX += delta)
            {
                // x点对应的y值
                float pointY = func(i-1, pointX);
                
                // 存入插点数组中需要转换x,y
                CGPoint fitPoint = CGPointMake(pointX, pointY);
                NSLog(@"算法内最后插入点的值:%f,%f",pointY,pointX);
                [fitPoints addObject:[NSValue valueWithCGPoint:fitPoint]];
            }
            // 终点
            if (i == [points count] - 1) {
//                [fitPoints addObject:[NSValue valueWithCGPoint:pt]];
            }
        }
    }

}

你可能感兴趣的:(iOS 三次B样条曲线插值算法)