-
Notifications
You must be signed in to change notification settings - Fork 0
/
star.swift
executable file
·47 lines (37 loc) · 1.24 KB
/
star.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env swift
import AppKit
let width: CGFloat = 128
let height: CGFloat = 128
let filePath = CommandLine.arguments[0] as NSString
let output = (filePath.deletingPathExtension as NSString).appendingPathExtension("pdf")!
var box = CGRect(x: 0.0, y: 0.0, width: width, height: height)
guard let context = CGContext(NSURL(fileURLWithPath: output, isDirectory: false), mediaBox: &box, nil) else {exit(EXIT_FAILURE)}
context.beginPDFPage(nil)
context.setFillColor(NSColor(white: 0.0, alpha: 1.0).cgColor)
context.setStrokeColor(NSColor(white: 0.0, alpha: 1.0).cgColor)
context.setLineWidth(4)
context.setLineJoin(.round)
context.setLineCap(.round)
// uncomment for flipped
context.scaleBy(x: 1, y: -1)
context.translateBy(x: 0, y: -height)
// START DRAWING
let radius = 50.0
let angle: Double = 2 * .pi / 10
let path = CGMutablePath()
for i in 0...10 {
let _radius = i % 2 == 0 ? radius : radius * 0.36
let _angle = Double(i) * angle - .pi / 2
let x = CGFloat(_radius * cos(_angle) + radius)
let y = CGFloat(_radius * sin(_angle) + radius)
if i == 0 {
path.move(to: CGPoint(x: x, y: y))
continue
}
path.addLine(to: CGPoint(x: x, y: y))
}
context.translateBy(x: 14, y: 18)
context.addPath(path)
context.strokePath()
// END
context.endPDFPage()