我通過給出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);
通過使用這些代碼如何找到交叉點。
我想你知道數學,你將不得不做它在客觀的C你自己沒有什麼大不了的功能吧 – amar