您的位置:首页 > 移动开发 > IOS开发

使用一组点画出平滑的曲线

2017-04-17 20:34 435 查看
今天在项目中需要人脸上的点来勾勒出人脸的轮廓,我的想法是将要画的点存入一个数组,使用了UIBezierPath来连接每一个点。但是这样画出来的图是折线,显得过于生硬。查了若干资料,后来终于在stackoverflow上找到了一个很好的解决办法

这个做法的原理其实是在每两个点之间加入一些点,来使得两个点可以平滑的过度。这其实就是Centripetal Catmull–Rom
spline的思想。

看效果:



于是便照葫芦画瓢,写出了我的代码
#define POINT(_INDEX_) [(NSValue *)[points objectAtIndex:_INDEX_] CGPointValue]
- (void)smoothedPathWithPoints:(NSArray *) pointsArray andGranularity:(NSInteger)granularity {

NSMutableArray *points = [pointsArray mutableCopy];

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
CGContextSetLineWidth(context, 0.6);

UIBezierPath *smoothedPath = [UIBezierPath bezierPath];

// Add control points to make the math make sense
[points insertObject:[points objectAtIndex:0] atIndex:0];
[points addObject:[points lastObject]];
[smoothedPath moveToPoint:POINT(0)];

for (NSUInteger index = 1; index < points.count - 2; index++) {
CGPoint p0 = POINT(index - 1);
CGPoint p1 = POINT(index);
CGPoint p2 = POINT(index + 1);
CGPoint p3 = POINT(index + 2);

// now add n points starting at p1 + dx/dy up until p2 using Catmull-Rom splines
for (int i = 1; i < granularity; i++) {

float t = (float) i * (1.0f / (float) granularity);
float tt = t * t;
float ttt = tt * t;

CGPoint pi; // intermediate point
pi.x = 0.5 * (2*p1.x+(p2.x-p0.x)*t + (2*p0.x-5*p1.x+4*p2.x-p3.x)*tt + (3*p1.x-p0.x-3*p2.x+p3.x)*ttt);
pi.y = 0.5 * (2*p1.y+(p2.y-p0.y)*t + (2*p0.y-5*p1.y+4*p2.y-p3.y)*tt + (3*p1.y-p0.y-3*p2.y+p3.y)*ttt);
[smoothedPath addLineToPoint:pi];
}

// Now add p2
[smoothedPath addLineToPoint:p2];
}

// finish by adding the last point
[smoothedPath addLineToPoint:POINT(points.count - 1)];

CGContextAddPath(context, smoothedPath.CGPath);
CGContextDrawPath(context, kCGPathStroke);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息