Skip to content

Instantly share code, notes, and snippets.

@ole
Last active June 25, 2018 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ole/686fc8ef61fc559923fdcb71785a1b9b to your computer and use it in GitHub Desktop.
Save ole/686fc8ef61fc559923fdcb71785a1b9b to your computer and use it in GitHub Desktop.
import UIKit
let containingRect = CGRect(x: 0, y: 0, width: 100, height: 100)
let path1 = UIBezierPath(rect: containingRect)
let circleRect = CGRect(x: 50, y: 10, width: 80, height: 80)
let circleCenter = CGPoint(x: circleRect.midX, y: circleRect.midY)
let radius = circleRect.height / 2
/*
+------------------------------------------------------------+
| |
| |
| |
| .-----------. |
| _.--' `--X.
| ,' /| `.
| ,' / | `.
| ,' / | `.
| / / | \
| / radius/ |b \
| ; / | :
| ; / | :
| ; / | :
| | /alpha | |
| | -X---------+ |
| : a | ;
| : | ;
| : | ;
| \ | /
| \ | /
| \ | /
| `. | ,'
| `. | ,'
| `. |,'
| `---. _.--'
| `---------' |
| |
| |
| |
+------------------------------------------------------------+
*/
// Compute angle alpha
let a = containingRect.maxX - circleCenter.x
let b = (radius * radius - a * a).squareRoot()
let alpha = Measurement(value: Double(atan(b/a)), unit: UnitAngle.radians)
alpha.converted(to: .degrees)
let path2 = UIBezierPath()
let startAngle = alpha
let endAngle = Measurement(value: 360, unit: .degrees) - alpha
path2.addArc(withCenter: circleCenter,
radius: radius,
startAngle: CGFloat(startAngle.converted(to: .radians).value),
endAngle: CGFloat(endAngle.converted(to: .radians).value),
clockwise: true)
path2
path1.append(path2.reversing())
path1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment