Skip to content

Instantly share code, notes, and snippets.

@mishimay
Last active May 26, 2016 07:42
Show Gist options
  • Save mishimay/6fc84c746ad6eff2c1cfbf8f8a2c3e37 to your computer and use it in GitHub Desktop.
Save mishimay/6fc84c746ad6eff2c1cfbf8f8a2c3e37 to your computer and use it in GitHub Desktop.
func circle(p1: CGPoint, _ p2: CGPoint, _ p3: CGPoint) -> (center: CGPoint, radius: CGFloat) {
let p = ((p1.y - p3.y) * (p1.y * p1.y - p2.y * p2.y + p1.x * p1.x - p2.x * p2.x) - (p1.y - p2.y) * (p1.y * p1.y - p3.y * p3.y + p1.x * p1.x - p3.x * p3.x)) / (2 * (p1.y - p3.y) * (p1.x - p2.x) - 2 * (p1.y - p2.y) * (p1.x - p3.x))
let q = ((p1.x - p3.x) * (p1.x * p1.x - p2.x * p2.x + p1.y * p1.y - p2.y * p2.y) - (p1.x - p2.x) * (p1.x * p1.x - p3.x * p3.x + p1.y * p1.y - p3.y * p3.y)) / (2 * (p1.x - p3.x) * (p1.y - p2.y) - 2 * (p1.x - p2.x) * (p1.y - p3.y))
let center = CGPoint(x: p, y: q)
let radius = hypot(p1.x - center.x, p1.y - center.y)
return (center: center, radius: radius)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment