How do I detect intersections between a circle and any other circle in the same plane?

前端 未结 7 2247
走了就别回头了
走了就别回头了 2020-11-28 05:54

I\'m looking for an algorithm to detect if a circle intersects with any other circle in the same plane (given that there can be more than one circle in a plane).

One

7条回答
  •  隐瞒了意图╮
    2020-11-28 06:02

    Swift 4 solution:

    struct Circle {
        let radius: CGFloat
        let position: CGPoint
    }
    
    func circlesIntersect(circleA: Circle, circleB: Circle) -> Bool {
        let Δr² = pow(circleA.radius - circleB.radius, 2)
        let Δx² = pow(circleA.position.x - circleB.position.x, 2)
        let Δy² = pow(circleA.position.y - circleB.position.y, 2)
        let ΣΔx²Δy² = Δx² + Δy²
        let Σr² = pow(circleA.radius + circleB.radius, 2)
        return Δr² <= ΣΔx²Δy² && ΣΔx²Δy² <= Σr²
    }
    

提交回复
热议问题