MKPolyline 检测自相交线 OBJECTIVE C
MKPolyline detect self-intersecting line OBJECTIVE C
如何检测 MKPolyline 是否与自身相交?我尝试对此进行研究,但只发现有两行或更多行的问题。如何检测我是否只有一次 line/one 中风?我想在用户释放触摸后检测它。
我目前在 touchEnded 函数中有这段代码。
CGPoint location = [touch locationInView:self.mapView];
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
[self.coordinates addObject:[NSValue valueWithMKCoordinate:coordinate]];
NSInteger numberOfPoints = [self.coordinates count];
if(numberOfPoints > 2)
{
[self setLineLength:[self getLengthArea]];
if([self lineLength] < 401)
{
if (numberOfPoints > 2)
{
CLLocationCoordinate2D points[numberOfPoints];
for (NSInteger i = 0; i < numberOfPoints; i++) {
points[i] = [self.coordinates[i] MKCoordinateValue];
}
[self.mapView addOverlay:[MKPolyline polylineWithCoordinates:points count:numberOfPoints]];
}
PCAnnotation *ann = [[PCAnnotation alloc] init];
[ann setCoordinate:coordinate];
ann.title = @"End";
[self.mapView addAnnotation:ann];
}
else
{
NSArray *overlayItems = [self.mapView overlays];
NSArray *annotations = [self.mapView annotations];
[self.mapView removeOverlays:overlayItems];
[self.mapView removeAnnotations:annotations];
}
}
MKPolyline
继承形式 MKMultiPoint
它有一个 - (MKMapPoint *)points;
方法,
您可以尝试检查所有线段之间的交点。
"The points are connected end-to-end in the order they are provided."
所以你可以在每两个点之间制作你自己的线段,
在你有一个线段数组之后,你可以检查它们的交点。
这是一个用于检查交叉点的 C++ 代码片段:
它可以很容易地翻译成 Objective-C 和其他任何东西。
public static bool LineSegmentsCross(Vector2 a, Vector2 b, Vector2 c, Vector2 d)
{
float denominator = ((b.X - a.X) * (d.Y - c.Y)) - ((b.Y - a.Y) * (d.X - c.X));
if (denominator == 0)
{
return false;
}
float numerator1 = ((a.Y - c.Y) * (d.X - c.X)) - ((a.X - c.X) * (d.Y - c.Y));
float numerator2 = ((a.Y - c.Y) * (b.X - a.X)) - ((a.X - c.X) * (b.Y - a.Y));
if (numerator1 == 0 || numerator2 == 0)
{
return false;
}
float r = numerator1 / denominator;
float s = numerator2 / denominator;
return (r > 0 && r < 1) && (s > 0 && s < 1);
}
如何检测 MKPolyline 是否与自身相交?我尝试对此进行研究,但只发现有两行或更多行的问题。如何检测我是否只有一次 line/one 中风?我想在用户释放触摸后检测它。
我目前在 touchEnded 函数中有这段代码。
CGPoint location = [touch locationInView:self.mapView];
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
[self.coordinates addObject:[NSValue valueWithMKCoordinate:coordinate]];
NSInteger numberOfPoints = [self.coordinates count];
if(numberOfPoints > 2)
{
[self setLineLength:[self getLengthArea]];
if([self lineLength] < 401)
{
if (numberOfPoints > 2)
{
CLLocationCoordinate2D points[numberOfPoints];
for (NSInteger i = 0; i < numberOfPoints; i++) {
points[i] = [self.coordinates[i] MKCoordinateValue];
}
[self.mapView addOverlay:[MKPolyline polylineWithCoordinates:points count:numberOfPoints]];
}
PCAnnotation *ann = [[PCAnnotation alloc] init];
[ann setCoordinate:coordinate];
ann.title = @"End";
[self.mapView addAnnotation:ann];
}
else
{
NSArray *overlayItems = [self.mapView overlays];
NSArray *annotations = [self.mapView annotations];
[self.mapView removeOverlays:overlayItems];
[self.mapView removeAnnotations:annotations];
}
}
MKPolyline
继承形式 MKMultiPoint
它有一个 - (MKMapPoint *)points;
方法,
您可以尝试检查所有线段之间的交点。
"The points are connected end-to-end in the order they are provided."
所以你可以在每两个点之间制作你自己的线段, 在你有一个线段数组之后,你可以检查它们的交点。
这是一个用于检查交叉点的 C++ 代码片段: 它可以很容易地翻译成 Objective-C 和其他任何东西。
public static bool LineSegmentsCross(Vector2 a, Vector2 b, Vector2 c, Vector2 d)
{
float denominator = ((b.X - a.X) * (d.Y - c.Y)) - ((b.Y - a.Y) * (d.X - c.X));
if (denominator == 0)
{
return false;
}
float numerator1 = ((a.Y - c.Y) * (d.X - c.X)) - ((a.X - c.X) * (d.Y - c.Y));
float numerator2 = ((a.Y - c.Y) * (b.X - a.X)) - ((a.X - c.X) * (b.Y - a.Y));
if (numerator1 == 0 || numerator2 == 0)
{
return false;
}
float r = numerator1 / denominator;
float s = numerator2 / denominator;
return (r > 0 && r < 1) && (s > 0 && s < 1);
}