Skip to content

Instantly share code, notes, and snippets.

@azenla
Created May 24, 2021 06:55
Show Gist options
  • Save azenla/1c35aa4a62c212905fed5e24fce0a3f3 to your computer and use it in GitHub Desktop.
Save azenla/1c35aa4a62c212905fed5e24fce0a3f3 to your computer and use it in GitHub Desktop.
import Foundation
import Cocoa
public extension CGPoint {
func toPointCode() -> String {
return "CGPoint(x: \(x), y: \(y))"
}
}
public extension NSBezierPath {
var cgPath: CGPath {
let path = CGMutablePath()
var points = [CGPoint](repeating: .zero, count: 3)
for i in 0 ..< elementCount {
let type = element(at: i, associatedPoints: &points)
switch type {
case .moveTo:
path.move(to: points[0])
case .lineTo:
path.addLine(to: points[0])
case .curveTo:
path.addCurve(to: points[2], control1: points[0], control2: points[1])
case .closePath:
path.closeSubpath()
@unknown default:
continue
}
}
return path
}
func toBezierPathCode(_ variable: String) -> String {
var output = ""
var points = [CGPoint](repeating: .zero, count: 3)
for i in 0 ..< elementCount {
let type = element(at: i, associatedPoints: &points)
switch type {
case .moveTo:
output = output.appending("\(variable).move(to: \(points[0].toPointCode()))\n")
case .lineTo:
output = output.appending("\(variable).line(to: \(points[0].toPointCode()))\n")
case .curveTo:
output = output.appending("\(variable).curve(to: \(points[2].toPointCode()), controlPoint1: \(points[0].toPointCode()), controlPoint2: \(points[1].toPointCode()))\n")
case .closePath:
output = output.appending("\(variable).close()\n")
@unknown default:
continue
}
}
return output
}
}
public struct AppleLogo {
public static func buildProperFruit() -> NSBezierPath {
let fruit = NSBezierPath()
fruit.move(to: CGPoint(x: 110.89, y: 100.8))
fruit.curve(to: CGPoint(x: 105.97, y: 91.91), controlPoint1: CGPoint(x: 109.5, y: 97.59), controlPoint2: CGPoint(x: 107.87, y: 94.63))
fruit.curve(to: CGPoint(x: 99.64, y: 84.21), controlPoint1: CGPoint(x: 103.39, y: 88.2), controlPoint2: CGPoint(x: 101.27, y: 85.63))
fruit.curve(to: CGPoint(x: 91.5, y: 80.6), controlPoint1: CGPoint(x: 97.11, y: 81.87), controlPoint2: CGPoint(x: 94.4, y: 80.67))
fruit.curve(to: CGPoint(x: 83.99, y: 82.41), controlPoint1: CGPoint(x: 89.42, y: 80.6), controlPoint2: CGPoint(x: 86.91, y: 81.2))
fruit.curve(to: CGPoint(x: 75.9, y: 84.21), controlPoint1: CGPoint(x: 81.06, y: 83.61), controlPoint2: CGPoint(x: 78.36, y: 84.21))
fruit.curve(to: CGPoint(x: 67.58, y: 82.41), controlPoint1: CGPoint(x: 73.31, y: 84.21), controlPoint2: CGPoint(x: 70.54, y: 83.61))
fruit.curve(to: CGPoint(x: 60.39, y: 80.51), controlPoint1: CGPoint(x: 64.61, y: 81.2), controlPoint2: CGPoint(x: 62.21, y: 80.57))
fruit.curve(to: CGPoint(x: 52.07, y: 84.21), controlPoint1: CGPoint(x: 57.6, y: 80.39), controlPoint2: CGPoint(x: 54.83, y: 81.62))
fruit.curve(to: CGPoint(x: 45.44, y: 92.18), controlPoint1: CGPoint(x: 50.3, y: 85.76), controlPoint2: CGPoint(x: 48.09, y: 88.42))
fruit.curve(to: CGPoint(x: 38.44, y: 106.18), controlPoint1: CGPoint(x: 42.6, y: 96.2), controlPoint2: CGPoint(x: 40.27, y: 100.86))
fruit.curve(to: CGPoint(x: 35.5, y: 122.85), controlPoint1: CGPoint(x: 36.48, y: 111.91), controlPoint2: CGPoint(x: 35.5, y: 117.47))
fruit.curve(to: CGPoint(x: 39.48, y: 138.79), controlPoint1: CGPoint(x: 35.5, y: 129.01999999999998), controlPoint2: CGPoint(x: 36.82, y: 134.32999999999998))
fruit.curve(to: CGPoint(x: 47.8, y: 147.26), controlPoint1: CGPoint(x: 41.56, y: 142.37), controlPoint2: CGPoint(x: 44.33, y: 145.19))
fruit.curve(to: CGPoint(x: 59.06, y: 150.46), controlPoint1: CGPoint(x: 51.27, y: 149.32999999999998), controlPoint2: CGPoint(x: 55.02, y: 150.39))
fruit.curve(to: CGPoint(x: 67.76, y: 148.42000000000002), controlPoint1: CGPoint(x: 61.27, y: 150.46), controlPoint2: CGPoint(x: 64.16, y: 149.77))
fruit.curve(to: CGPoint(x: 74.67, y: 146.38), controlPoint1: CGPoint(x: 71.35, y: 147.06), controlPoint2: CGPoint(x: 73.66, y: 146.38))
fruit.curve(to: CGPoint(x: 82.33, y: 148.78), controlPoint1: CGPoint(x: 75.42, y: 146.38), controlPoint2: CGPoint(x: 77.98, y: 147.18))
fruit.curve(to: CGPoint(x: 92.73, y: 150.64), controlPoint1: CGPoint(x: 86.43, y: 150.27), controlPoint2: CGPoint(x: 89.9, y: 150.88))
fruit.curve(to: CGPoint(x: 110.05, y: 141.47), controlPoint1: CGPoint(x: 100.43, y: 150.02), controlPoint2: CGPoint(x: 106.2, y: 146.97))
fruit.curve(to: CGPoint(x: 99.83, y: 123.87), controlPoint1: CGPoint(x: 103.17, y: 137.28), controlPoint2: CGPoint(x: 99.77, y: 131.41))
fruit.curve(to: CGPoint(x: 106.17, y: 109.24), controlPoint1: CGPoint(x: 99.89, y: 118.0), controlPoint2: CGPoint(x: 102.01, y: 113.12))
fruit.curve(to: CGPoint(x: 112.5, y: 105.06), controlPoint1: CGPoint(x: 108.05, y: 107.44), controlPoint2: CGPoint(x: 110.16, y: 106.05))
fruit.curve(to: CGPoint(x: 110.89, y: 100.8), controlPoint1: CGPoint(x: 111.99, y: 103.58), controlPoint2: CGPoint(x: 111.46, y: 102.16))
fruit.close()
fruit.move(to: CGPoint(x: 110.89, y: 100.8))
return fruit
}
public static func buildProperLeaf() -> NSBezierPath {
let leaf = NSBezierPath()
leaf.move(to: CGPoint(x: 93.25, y: 170.64))
leaf.curve(to: CGPoint(x: 88.25, y: 157.77), controlPoint1: CGPoint(x: 93.25, y: 166.04), controlPoint2: CGPoint(x: 91.58, y: 161.74))
leaf.curve(to: CGPoint(x: 74.1, y: 150.74), controlPoint1: CGPoint(x: 84.23, y: 153.04), controlPoint2: CGPoint(x: 79.37, y: 150.31))
leaf.curve(to: CGPoint(x: 74.0, y: 152.48), controlPoint1: CGPoint(x: 74.03, y: 151.29), controlPoint2: CGPoint(x: 74.0, y: 151.87))
leaf.curve(to: CGPoint(x: 79.3, y: 165.49), controlPoint1: CGPoint(x: 74.0, y: 156.9), controlPoint2: CGPoint(x: 75.91, y: 161.62))
leaf.curve(to: CGPoint(x: 85.76, y: 170.37), controlPoint1: CGPoint(x: 80.99, y: 167.45), controlPoint2: CGPoint(x: 83.15, y: 169.07))
leaf.curve(to: CGPoint(x: 93.15, y: 172.48), controlPoint1: CGPoint(x: 88.37, y: 171.65), controlPoint2: CGPoint(x: 90.83, y: 172.35))
leaf.curve(to: CGPoint(x: 93.25, y: 170.64), controlPoint1: CGPoint(x: 93.22, y: 171.86), controlPoint2: CGPoint(x: 93.25, y: 171.25))
leaf.line(to: CGPoint(x: 93.25, y: 170.64))
leaf.close()
leaf.move(to: CGPoint(x: 93.25, y: 170.64))
return leaf
}
public static func buildFruit() -> NSBezierPath {
let path = NSBezierPath()
path.move(to: CGPoint(x: 110.89, y: 99.2))
path.curve(to: CGPoint(x: 105.97, y: 108.09), controlPoint1: CGPoint(x: 109.5, y: 102.41), controlPoint2: CGPoint(x: 107.87, y: 105.37))
path.curve(to: CGPoint(x: 99.64, y: 115.79), controlPoint1: CGPoint(x: 103.39, y: 111.8), controlPoint2: CGPoint(x: 101.27, y: 114.37))
path.curve(to: CGPoint(x: 91.5, y: 119.4), controlPoint1: CGPoint(x: 97.11, y: 118.13), controlPoint2: CGPoint(x: 94.4, y: 119.33))
path.curve(to: CGPoint(x: 83.99, y: 117.59), controlPoint1: CGPoint(x: 89.42, y: 119.4), controlPoint2: CGPoint(x: 86.91, y: 118.8))
path.curve(to: CGPoint(x: 75.9, y: 115.79), controlPoint1: CGPoint(x: 81.06, y: 116.39), controlPoint2: CGPoint(x: 78.36, y: 115.79))
path.curve(to: CGPoint(x: 67.58, y: 117.59), controlPoint1: CGPoint(x: 73.31, y: 115.79), controlPoint2: CGPoint(x: 70.54, y: 116.39))
path.curve(to: CGPoint(x: 60.39, y: 119.49), controlPoint1: CGPoint(x: 64.61, y: 118.8), controlPoint2: CGPoint(x: 62.21, y: 119.43))
path.curve(to: CGPoint(x: 52.07, y: 115.79), controlPoint1: CGPoint(x: 57.6, y: 119.61), controlPoint2: CGPoint(x: 54.83, y: 118.38))
path.curve(to: CGPoint(x: 45.44, y: 107.82), controlPoint1: CGPoint(x: 50.3, y: 114.24), controlPoint2: CGPoint(x: 48.09, y: 111.58))
path.curve(to: CGPoint(x: 38.44, y: 93.82), controlPoint1: CGPoint(x: 42.6, y: 103.8), controlPoint2: CGPoint(x: 40.27, y: 99.14))
path.curve(to: CGPoint(x: 35.5, y: 77.15), controlPoint1: CGPoint(x: 36.48, y: 88.09), controlPoint2: CGPoint(x: 35.5, y: 82.53))
path.curve(to: CGPoint(x: 39.48, y: 61.21), controlPoint1: CGPoint(x: 35.5, y: 70.98), controlPoint2: CGPoint(x: 36.82, y: 65.67))
path.curve(to: CGPoint(x: 47.8, y: 52.74), controlPoint1: CGPoint(x: 41.56, y: 57.63), controlPoint2: CGPoint(x: 44.33, y: 54.81))
path.curve(to: CGPoint(x: 59.06, y: 49.54), controlPoint1: CGPoint(x: 51.27, y: 50.67), controlPoint2: CGPoint(x: 55.02, y: 49.61))
path.curve(to: CGPoint(x: 67.76, y: 51.58), controlPoint1: CGPoint(x: 61.27, y: 49.54), controlPoint2: CGPoint(x: 64.16, y: 50.23))
path.curve(to: CGPoint(x: 74.67, y: 53.62), controlPoint1: CGPoint(x: 71.35, y: 52.94), controlPoint2: CGPoint(x: 73.66, y: 53.62))
path.curve(to: CGPoint(x: 82.33, y: 51.22), controlPoint1: CGPoint(x: 75.42, y: 53.62), controlPoint2: CGPoint(x: 77.98, y: 52.82))
path.curve(to: CGPoint(x: 92.73, y: 49.36), controlPoint1: CGPoint(x: 86.43, y: 49.73), controlPoint2: CGPoint(x: 89.9, y: 49.12))
path.curve(to: CGPoint(x: 110.05, y: 58.53), controlPoint1: CGPoint(x: 100.43, y: 49.98), controlPoint2: CGPoint(x: 106.2, y: 53.03))
path.curve(to: CGPoint(x: 99.83, y: 76.13), controlPoint1: CGPoint(x: 103.17, y: 62.72), controlPoint2: CGPoint(x: 99.77, y: 68.59))
path.curve(to: CGPoint(x: 106.17, y: 90.76), controlPoint1: CGPoint(x: 99.89, y: 82), controlPoint2: CGPoint(x: 102.01, y: 86.88))
path.curve(to: CGPoint(x: 112.5, y: 94.94), controlPoint1: CGPoint(x: 108.05, y: 92.56), controlPoint2: CGPoint(x: 110.16, y: 93.95))
path.curve(to: CGPoint(x: 110.89, y: 99.2), controlPoint1: CGPoint(x: 111.99, y: 96.42), controlPoint2: CGPoint(x: 111.46, y: 97.84))
path.close()
return path
}
public static func buildLeaf() -> NSBezierPath {
let path = NSBezierPath()
path.move(to: CGPoint(x: 93.25, y: 29.36))
path.curve(to: CGPoint(x: 88.25, y: 42.23), controlPoint1: CGPoint(x: 93.25, y: 33.96), controlPoint2: CGPoint(x: 91.58, y: 38.26))
path.curve(to: CGPoint(x: 74.1, y: 49.26), controlPoint1: CGPoint(x: 84.23, y: 46.96), controlPoint2: CGPoint(x: 79.37, y: 49.69))
path.curve(to: CGPoint(x: 74, y: 47.52), controlPoint1: CGPoint(x: 74.03, y: 48.71), controlPoint2: CGPoint(x: 74, y: 48.13))
path.curve(to: CGPoint(x: 79.3, y: 34.51), controlPoint1: CGPoint(x: 74, y: 43.1), controlPoint2: CGPoint(x: 75.91, y: 38.38))
path.curve(to: CGPoint(x: 85.76, y: 29.63), controlPoint1: CGPoint(x: 80.99, y: 32.55), controlPoint2: CGPoint(x: 83.15, y: 30.93))
path.curve(to: CGPoint(x: 93.15, y: 27.52), controlPoint1: CGPoint(x: 88.37, y: 28.35), controlPoint2: CGPoint(x: 90.83, y: 27.65))
path.curve(to: CGPoint(x: 93.25, y: 29.36), controlPoint1: CGPoint(x: 93.22, y: 28.14), controlPoint2: CGPoint(x: 93.25, y: 28.75))
path.line(to: CGPoint(x: 93.25, y: 29.36))
path.close()
return path
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment