ios 画线平滑_iphone – 绘制平滑曲线 – 需要的方法

这篇博客介绍了如何在iOS应用中使用Catmull-Rom样条算法来平滑地绘制一系列移动的点。通过添加控制点并计算中间点坐标,实现了UIBezierPath的平滑连接,避免了锯齿效果。博主提供了具体的代码示例,帮助新手理解平滑绘图的实现方法。
摘要由CSDN通过智能技术生成

像以前的人都问过,你如何平滑一组点在iOS绘图应用程序WHILE MOVING?我试过UIBezierpaths,但我得到的是锯齿的结束,他们相交,当我只是移动点1,2,3,4 – 2,3,4,5。我听说过样条曲线和所有其他类型。我是新的iPhone编程,不明白如何编程在我的石英绘图应用程序。一个坚实的例子将非常感谢,我已经花了几个星期的圈子,我永远似乎找不到任何iOS代码这个任务。大多数帖子只是链接到一个java模拟或wikipedia上的页面曲线拟合,这对我没有什么。我也不想切换到openGL ES。我希望有人能够终于提供代码来回答这个循环的问题。

谢谢,新年快乐!!!

…………………………………………. …………………………………………. …………………………………………. ……………

这是我的代码为UIBezierPath左边缘交叉///

更新到下面的答案

#define VALUE(_INDEX_) [NSValue valueWithCGPoint:points[_INDEX_]]

#define POINT(_INDEX_) [(NSValue *)[points objectAtIndex:_INDEX_] CGPointValue]

- (UIBezierPath*)smoothedPathWithGranularity:(NSInteger)granularity

{

NSMutableArray *points = [(NSMutableArray*)[self pointsOrdered] mutableCopy];

if (points.count < 4) return [self bezierPath];

// Add control points to make the math make sense

[points insertObject:[points objectAtIndex:0] atIndex:0];

[points addObject:[points lastObject]];

UIBezierPath *smoothedPath = [self bezierPath];

[smoothedPath removeAllPoints];

[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)];

return smoothedPath;

}

- (PVPoint *)pointAppendingCGPoint:(CGPoint)CGPoint

{

PVPoint *newPoint = [[PVPoint alloc] initInsertingIntoManagedObjectContext:[self managedObjectContext]];

[newPoint setCGPoint:CGPoint];

[newPoint setOrder:[NSNumber numberWithUnsignedInteger:[[self points] count]]];

[[self mutableSetValueForKey:@"points"] addObject:newPoint];

[(NSMutableArray *)[self pointsOrdered] addObject:newPoint];

[[self bezierPath] addLineToPoint:CGPoint];

return [newPoint autorelease];

if ([self bezierPath] && [pointsOrdered count] > 3)

{

PVPoint *control1 = [pointsOrdered objectAtIndex:[pointsOrdered count] - 2];

PVPoint *control2 = [pointsOrdered objectAtIndex:[pointsOrdered count] - 1];

[bezierPath moveToPoint:[[pointsOrdered objectAtIndex:[pointsOrdered count] - 3] CGPoint]];

[[self bezierPath] addCurveToPoint:CGPoint controlPoint1:[control1 CGPoint] controlPoint2:[control2 CGPoint]];

}

}

- (BOOL)isComplete { return [[self points] count] > 1; }

- (UIBezierPath *)bezierPath

{

if (!bezierPath)

{

bezierPath = [UIBezierPath bezierPath];

for (NSUInteger p = 0; p < [[self points] count]; p++)

{

if (!p) [bezierPath moveToPoint:[(PVPoint *)[[self pointsOrdered] objectAtIndex:p] CGPoint]];

else [bezierPath addLineToPoint:[(PVPoint *)[[self pointsOrdered] objectAtIndex:p] CGPoint]];

}

[bezierPath retain];

}

return bezierPath;

}

- (CGPathRef)CGPath

{

return [[self bezierPath] CGPath];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值