Repository navigation
Expand file tree
/
Copy pathNodeAbstraction.swift
More file actions
241 lines (180 loc) · 6.05 KB
/
Copy pathNodeAbstraction.swift
File metadata and controls
241 lines (180 loc) · 6.05 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// NodeAbstraction.swift
// LSystem01
//
// Created by Jeff Holtzkener on 4/5/16.
// Copyright © 2016 Jeff Holtzkener. All rights reserved.
//
import UIKit
class ViewForNode: UIView {
var myNode: Node!
var lineLocations = [(CGPoint, CGPoint)]()
var root: CGPoint?
var fillColor: UIColor!
override func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, fillColor.CGColor)
for line in lineLocations {
CGContextMoveToPoint(context, line.0.x, line.0.y)
CGContextAddLineToPoint(context, line.1.x, line.1.y)
CGContextStrokePath(context)
}
if let root = root {
let path = UIBezierPath(arcCenter: root, radius: 3, startAngle: 0, endAngle: 2 * CGFloat(M_PI), clockwise: true)
fillColor.setFill()
path.fill()
}
}
func clearPaths() {
lineLocations = [(CGPoint, CGPoint)]()
}
}
protocol Node {
static var nodeType: NodeType { get }
static var angle: CGFloat { get set }
static var view: ViewForNode! { get set }
var parent: Node? { get set }
var children: [Node] { get set }
var rootLocation: CGPoint? { get set }
var orientation: CGFloat { get set }
var location: CGPoint { get set }
var segmentLength: CGFloat { get set }
init()
}
extension Node {
var reductionFactor: CGFloat {
return Constants.reductionFactor
}
func calcOrientation() -> CGFloat {
if let parent = parent {
return Self.angle + parent.orientation
} else {
return 0
}
}
func calcLocation() -> CGPoint {
if let parent = parent {
let location = CGPoint(x: parent.location.x + self.segmentLength * cos(self.orientation), y: parent.location.y + self.segmentLength * sin(self.orientation))
return location
} else {
return rootLocation!
}
}
init(segmentLength: CGFloat, parent: Node?, rootLocation: CGPoint?) {
self.init()
self.segmentLength = segmentLength
if let parent = parent {
self.parent = parent
orientation = calcOrientation()
location = calcLocation()
} else {
self.rootLocation = rootLocation
location = rootLocation!
orientation = CGFloat( M_PI)
}
makePaths()
}
mutating func makePaths() {
if let parent = parent {
if Self.view.frame.contains(parent.location) || Self.view.frame.contains(location) {
Self.view.lineLocations.append((parent.location, location))
}
} else {
Self.view.root = self.rootLocation
}
}
mutating func recursiveReposition() {
if let rootLocation = rootLocation {
location = rootLocation
} else {
orientation = calcOrientation()
location = calcLocation()
}
makePaths()
for (i, _) in children.enumerate() {
children[i].recursiveReposition()
}
}
mutating func recursiveChangeLength() {
for (i, _) in children.enumerate() {
children[i].segmentLength = segmentLength * reductionFactor
if let rootLocation = rootLocation {
location = rootLocation
} else {
orientation = calcOrientation()
location = calcLocation()
}
makePaths()
children[i].recursiveChangeLength()
}
}
mutating func spawn() -> [Node] {
// called by regenerate function in VC
for node in Settings.sharedInstance.nodeSubstitutions[Self.nodeType]! {
if let nodeInit = Settings.initDict[node] {
children.append(nodeInit(segmentLength: segmentLength * reductionFactor, parent: self, rootLocation: nil))
}
}
return children
}
}
class TypeANode: Node {
static let nodeType: NodeType = .typeA
static var angle: CGFloat = CGFloat(7*(M_PI)/8)
static var view: ViewForNode! // set in VC
var parent: Node?
var children = [Node]()
var rootLocation: CGPoint?
var orientation: CGFloat = 0
var location: CGPoint = CGPoint.zero
var segmentLength: CGFloat = 0
required init() {}
}
class TypeBNode: Node {
static let nodeType: NodeType = .typeB
static var angle: CGFloat = CGFloat(7*(M_PI)/8)
static var view: ViewForNode! // set in VC
var parent: Node?
var children = [Node]()
var rootLocation: CGPoint?
var orientation: CGFloat = 0
var location: CGPoint = CGPoint.zero
var segmentLength: CGFloat = 0
required init() {}
}
class TypeCNode: Node {
static let nodeType: NodeType = .typeC
static var angle: CGFloat = CGFloat(7*(M_PI)/8)
static var view: ViewForNode! // set in VC
var parent: Node?
var children = [Node]()
var rootLocation: CGPoint?
var orientation: CGFloat = 0
var location: CGPoint = CGPoint.zero
var segmentLength: CGFloat = 0
required init() {}
}
class TypeDNode: Node {
static let nodeType: NodeType = .typeD
static var angle: CGFloat = CGFloat(7*(M_PI)/8)
static var view: ViewForNode! // set in VC
var parent: Node?
var children = [Node]()
var rootLocation: CGPoint?
var orientation: CGFloat = 0
var location: CGPoint = CGPoint.zero
var segmentLength: CGFloat = 0
required init() {}
}
class TypeENode: Node {
static let nodeType: NodeType = .typeE
static var angle: CGFloat = CGFloat(7*(M_PI)/8)
static var view: ViewForNode! // set in VC
var parent: Node?
var children = [Node]()
var rootLocation: CGPoint?
var orientation: CGFloat = 0
var location: CGPoint = CGPoint.zero
var segmentLength: CGFloat = 0
required init() {}
}