-
Notifications
You must be signed in to change notification settings - Fork 5
/
ViewController.swift
150 lines (115 loc) · 4.73 KB
/
ViewController.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// ViewController.swift
// BezierPathSample
//
// Created by Volodymyr Boichentsov on 03/07/2017.
// Copyright © 2017 Volodymyr Boichentsov. All rights reserved.
//
#if os(OSX)
import Cocoa
#elseif os(iOS)
import UIKit
#endif
import Delaunay
func delayWithSeconds(_ seconds: Double, completion: @escaping () -> ()) {
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
completion()
}
}
class ViewController: OSViewController {
var flattness = 0.7
var baseLayer:CAGradientLayer = CAGradientLayer.init()
override func viewDidLoad() {
super.viewDidLoad()
let layer:CALayer? = self.view.layer
baseLayer.frame = CGRect.init(origin: CGPoint.zero, size: baseLayer.frame.size)
// baseLayer.colors = [OSColor.randomColor().cgColor, OSColor.randomColor().cgColor, OSColor.randomColor().cgColor]
layer?.addSublayer(baseLayer)
baseLayer.borderWidth = 0
#if os(OSX)
baseLayer.autoresizingMask = [.layerWidthSizable, .layerHeightSizable]
#endif
testTextTriangle()
}
#if os(OSX)
@IBAction func flatness(_ sender: NSSlider) {
flattness = sender.doubleValue
testTextTriangle()
}
#endif
func testTextTriangle() {
if let subs = baseLayer.sublayers {
for s in subs {
s.removeFromSuperlayer()
}
}
// Do any additional setup after loading the view.
let test = "Super"
let path = test.bezierPath(font: OSFont.init(name: "Helvetica", size: 200)!)
path.transform(using: OSAffineTransform.init(scaleByX: 1, byY: -1))
path.transform(using: OSAffineTransform.init(translationByX: 0, byY: path.bounds.height + 100))
// let path:OSBezierPath = OSBezierPath.init(roundedRect: CGRect.init(x: 10, y: 10, width: 150, height: 130), cornerRadius: 50)
//
// path.appendOval(in: CGRect.init(x: 100, y: 100, width: 150, height: 130))
// path.appendOval(in: CGRect.init(x: 50, y: 50, width: 150, height: 130))
// path.appendOval(in: CGRect.init(x: 150, y: 150, width: 150, height: 130))
// path.appendOval(in: CGRect.init(x: 250, y: 50, width: 150, height: 130))
// path.appendOval(in: CGRect.init(x: 150, y: 50, width: 150, height: 130))
// path.appendOval(in: CGRect.init(x: 200, y: 50, width: 150, height: 130))
// path.appendOval(in: CGRect.init(x: 200, y: 50, width: 150, height: 130))
let start = Date().timeIntervalSince1970
let triangles = path.triangles(flatness: flattness)
// let triangles = [Triangle]()
let end = Date().timeIntervalSince1970
print("time: \(end - start)")
print("triangles \(triangles.count) ")
for triangle in triangles {
let triangleLayer = CAShapeLayer()
triangleLayer.frame = baseLayer.frame
triangleLayer.path = triangle.toPath()
// triangleLayer.borderWidth = 0.5
// triangleLayer.strokeColor = OSColor.black.cgColor
triangleLayer.fillColor = OSColor.gray.cgColor
triangleLayer.fillColor = OSColor.randomColor().cgColor
triangleLayer.backgroundColor = OSColor.clear.cgColor
baseLayer.addSublayer(triangleLayer)
}
// Style Square
let a = CAShapeLayer()
a.path = path.cgPath
a.strokeColor = OSColor.black.cgColor
a.fillColor = nil
a.opacity = 1.0
a.lineWidth = 1
baseLayer.addSublayer(a)
// for segment in path.polygons() {
// let path2:CGMutablePath = CGMutablePath.init()
// for point in segment.points {
// path2.addRect(CGRect.init(x: point.x, y: point.y, width: 0.5, height: 0.5))
// }
// // Style Square
// let a = CAShapeLayer()
// a.path = path2
// a.strokeColor = OSColor.randomColor().cgColor
// a.fillColor = nil
// a.opacity = 1.0
// a.lineWidth = 1
// baseLayer.addSublayer(a)
// }
}
#if os(OSX)
override func viewDidLayout() {
self.view.layer?.frame = self.view.bounds
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
#elseif os(iOS)
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#endif
}