我通过给出11个点绘制曲线和线。是否可以检测两条线(或)两条曲线(或)一条线和一条曲线的交点。
我使用
绘图线CGMutablePathRef path = CGPathCreateMutable();
for (int i = 0; i < [_points count]; i++)
{
CGPoint pt = [[_points objectAtIndex:i] CGPointValue];
if (i == 0)
{
CGPathMoveToPoint(path, NULL, pt.x+1, pt.y+1);
}
else
{
CGPathAddLineToPoint(path, NULL, pt.x+1, pt.y+1);
}
}
CGContextSetLineWidth(context, 1.0f);
CGContextSetStrokeColorWithColor(context, curveColor.CGColor);
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGPathRelease(path);
曲线绘制
CGMutablePathRef path = CGPathCreateMutable();
for (int i = 0; i < [_points count]; i++)
{
CGPoint pt = [[_points objectAtIndex:i] CGPointValue];
NSLog(@"%@",NSStringFromCGPoint(pt));
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);
}
}
CGContextSetLineWidth(context, 1.0f);
CGContextSetStrokeColorWithColor(context, curveColor.CGColor);
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGPathRelease(path);
通过使用这些代码,如何找到交叉点。
答案 0 :(得分:3)
有多种方法可以优化,比如首先碰撞曲线或直线的边界矩形。