Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The flag of Vietnam
import UIKit
import PlaygroundSupport
class StarView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .red
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
//create path
let path = UIBezierPath()
let xCenter: CGFloat = rect.size.width/2
let yCenter: CGFloat = rect.size.height/2
let w = CGFloat(rect.height/2)
let r = w / 2.0
let flip: CGFloat = -1.0
let polySide = CGFloat(5)
let theta = 2.0*Double.pi*Double(2.0/polySide)
path.move(to: CGPoint(x: xCenter, y: r*flip+yCenter))
for i in 1..<Int(polySide) {
let x: CGFloat = r*CGFloat(sin(Double(i)*theta))
let y: CGFloat = r*CGFloat(cos(Double(i)*theta))
path.addLine(to: CGPoint(x: x+xCenter, y: y*flip+yCenter))
}
path.close()
// stroke
UIColor.yellow.set()
path.lineWidth = 2.0
path.stroke()
// fill
let fillColor = UIColor.yellow
fillColor.setFill()
path.fill()
}
}
let view = StarView(frame: CGRect(x: 0, y: 0, width: 600, height: 300))
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment