有一种简单的方法可以在Swift中绘制形状(最好使用Sprite-Kit),然后检测它们是否相交以及它们相交的位置?就像这里的交叉形状一样:
答案 0 :(得分:12)
如果这包含一系列线段,可以使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。