Skip to content

Instantly share code, notes, and snippets.

@fx-studio
Created September 2, 2020 01:46
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 fx-studio/5530d36ca1794d35abf732c5e3e085f5 to your computer and use it in GitHub Desktop.
Save fx-studio/5530d36ca1794d35abf732c5e3e085f5 to your computer and use it in GitHub Desktop.
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