这个问题与以下内容完全相同:>
Swift: detecting intersection from sprite kit SKShapeNode drawings1个
有没有一种简单的方法可以在Swift中绘制形状(最好使用Sprite-Kit),然后检测它们是否相交以及它们相交的位置?就像这里是一个相交的形状:
有没有一种简单的方法可以在Swift中绘制形状(最好使用Sprite-Kit),然后检测它们是否相交以及它们相交的位置?就像这里是一个相交的形状:
解决方法
如果这由一系列线段组成,可以使Martin R对
UIBezierPath intersect的回答不仅可以检测交叉点,还可以识别交叉点的位置:
- func intersectionBetweenSegments(p0: CGPoint,_ p1: CGPoint,_ p2: CGPoint,_ p3: CGPoint) -> CGPoint? {
- var denominator = (p3.y - p2.y) * (p1.x - p0.x) - (p3.x - p2.x) * (p1.y - p0.y)
- var ua = (p3.x - p2.x) * (p0.y - p2.y) - (p3.y - p2.y) * (p0.x - p2.x)
- var ub = (p1.x - p0.x) * (p0.y - p2.y) - (p1.y - p0.y) * (p0.x - p2.x)
- if (denominator < 0) {
- ua = -ua; ub = -ub; denominator = -denominator
- }
- if ua >= 0.0 && ua <= denominator && ub >= 0.0 && ub <= denominator && denominator != 0 {
- return CGPoint(x: p0.x + ua / denominator * (p1.x - p0.x),y: p0.y + ua / denominator * (p1.y - p0.y))
- }
- return nil
- }
因此,如果您有一个CGPoint值数组并且想要识别所有交叉点,则可以执行以下操作:
- let n = points!.count - 1
- for i in 1 ..< n {
- for j in 0 ..< i-1 {
- if let intersection = intersectionBetweenSegments(points![i],points![i+1],points![j],points![j+1]) {
- // do whatever you want with `intersection`
- }
- }
- }
例如,您可以在屏幕相交的屏幕上添加一个点:
但是,如果曲线由三次贝塞尔曲线组成,则更复杂.不过,您可以考虑一下Checking if two cubic Bézier curves intersect.