Skip to content

Instantly share code, notes, and snippets.

@yuhao-git-star
Created April 18, 2017 06:19
Show Gist options
  • Save yuhao-git-star/26a0619914d47333dbe9d35c8c4ce228 to your computer and use it in GitHub Desktop.
Save yuhao-git-star/26a0619914d47333dbe9d35c8c4ce228 to your computer and use it in GitHub Desktop.
class CircleView : UIView {
override func draw(_ rect: CGRect) {
let 畫筆 = UIBezierPath()
畫筆.move(to: .zero)
// 畫筆.addLine(to: CGPoint(x: 100, y: 100))
// x^2 + y^2 = r^2
// cos(θ) = x/r ==> x = r * cos(θ)
// sin(θ) = y/r == y = r * sin(θ)
let radius:Double = 100 //直徑
let 圓點 = CGPoint(x: rect.width / 2, y: rect.height / 2)
畫筆.move(to: CGPoint(x: 圓點.x + CGFloat(radius), y: 圓點.y))
for i in stride(from: 0, to: 180, by: 10) {
// 弧度 = 度 * PI / 180
let 弧度 = Double(i) * Double.pi / 180
let x = Double(圓點.x) + radius * cos(弧度)
let y = Double(圓點.y) + radius * sin(弧度)
畫筆.addLine(to: CGPoint(x: x, y: y))
}
畫筆.stroke()
}
}
let view = CircleView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
view.backgroundColor = UIColor.white
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment