Last active
June 25, 2018 14:14
-
-
Save ole/686fc8ef61fc559923fdcb71785a1b9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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