Skip to content

Commit 9dbd14e

Browse files
author
Oleksandr Glagoliev
committed
Add Useful methods to the diagram.
Implement Delaunay triangulation i the SK Demo
1 parent 71ff81a commit 9dbd14e

File tree

3 files changed

+92
-54
lines changed

3 files changed

+92
-54
lines changed

Demo/Demo/VoronoiView.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ class VoronoiView: UIView {
5555

5656
@objc func tap(_ gesture: UITapGestureRecognizer) {
5757

58-
// let seedPoint = gesture.location(in: self)
59-
// drawNewPortionOfAxipoints(
60-
// generateCircularPoints(
61-
// center: Site(
62-
// x: Double(bounds.width / 2),
63-
// y: Double(bounds.height / 2)
64-
// ).cgPoint,
65-
// seedPoint: seedPoint,
66-
// radius: center.distance(to: seedPoint),
67-
// numberOfAxis: 1
68-
// )
69-
// )
58+
let seedPoint = gesture.location(in: self)
59+
drawNewPortionOfAxipoints(
60+
generateCircularPoints(
61+
center: Site(
62+
x: Double(bounds.width / 2),
63+
y: Double(bounds.height / 2)
64+
).cgPoint,
65+
seedPoint: seedPoint,
66+
radius: center.distance(to: seedPoint),
67+
numberOfAxis: 10
68+
)
69+
)
7070

7171

7272
// if axipoints.count > 4 {
@@ -76,7 +76,7 @@ class VoronoiView: UIView {
7676

7777
// drawRandomSites(200)
7878

79-
drawNextEdgeCase()
79+
// drawNextEdgeCase()
8080
}
8181

8282
private func generateCircularPoints(
@@ -179,7 +179,7 @@ class VoronoiView: UIView {
179179
context.drawLine(
180180
from: o.cgPoint,
181181
to: d.cgPoint,
182-
color: UIColor.black.withAlphaComponent(0.05), lineWidth: 2.0
182+
color: UIColor.black.withAlphaComponent(1), lineWidth: 2.0
183183
)
184184

185185
// Site
@@ -191,7 +191,7 @@ class VoronoiView: UIView {
191191

192192
// context.drawPolygonFromCCWPoints(points, color: UIColor.random().withAlphaComponent(0.2))
193193

194-
let hullVertices = points.map { $0.cgPoint }
194+
// let hullVertices = points.map { $0.cgPoint }
195195

196196
// context.addPath(
197197
// UIBezierPath.roundedCornersPath(hullVertices, 10).cgPath
@@ -201,11 +201,11 @@ class VoronoiView: UIView {
201201
// context.addPath(
202202
// UIBezierPath.roundedCornersPath(scaledHull, 10).cgPath
203203
// )
204-
//
205-
let paddedHull = paddedPolygon(hullVertices, padding: -15.0)
206-
context.addPath(
207-
UIBezierPath.roundedCornersPath(paddedHull, 10)!.cgPath
208-
)
204+
205+
// let paddedHull = paddedPolygon(hullVertices, padding: -15.0)
206+
// context.addPath(
207+
// UIBezierPath.roundedCornersPath(paddedHull, 10)!.cgPath
208+
// )
209209

210210
// for i in 0..<1 {
211211
// let paddedHull = paddedPolygon(hullVertices, padding: CGFloat(-i) * 8)

Sources/FortunesAlgorithm/Diagram.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,47 @@ public class Cell {
6161
}
6262
}
6363

64+
public extension Cell {
65+
66+
67+
/// Returns hell vertices of the cell
68+
func hullVerticesCCW() -> [Vertex] {
69+
var vertices: [Vertex] = []
70+
guard var he = outerComponent else {
71+
return []
72+
}
73+
74+
var finish = false
75+
while !finish {
76+
vertices.append(he.origin!)
77+
he = he.next!
78+
finish = he === outerComponent
79+
}
80+
81+
return vertices
82+
}
83+
84+
85+
/// Returns all the neighbours of specific cell
86+
func neighbours() -> [Cell] {
87+
var neighbours = [Cell]()
88+
guard var he = outerComponent else {
89+
return []
90+
}
91+
92+
var finish = false
93+
while !finish {
94+
if let neighbour = he.twin?.incidentFace {
95+
neighbours.append(neighbour)
96+
}
97+
he = he.next!
98+
finish = he === outerComponent
99+
}
100+
return neighbours
101+
}
102+
103+
}
104+
64105
/// The half‐edge record of a half‐edge e stores pointer to:
65106
/// • Origin(e)
66107
/// • Twin of e, e.twin or twin(e)

SpriteKitDemo/SpriteKitDemo/GameScene.swift

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,30 @@ class GameScene: SKScene {
3333
clippingRect: self.clippingRect
3434
)
3535

36-
self.diagram.cells.forEach { cell in
37-
var points: [Site] = []
38-
var he = cell.outerComponent
36+
diagram.cells.forEach { cell in
37+
// let hullVertices = cell
38+
// .hullVerticesCCW()
39+
// .map { $0.cgPoint }
3940

40-
var finish = false
41-
while !finish {
42-
43-
let o = he!.origin!
44-
points.append(o)
45-
46-
47-
he = he?.next
48-
finish = he === cell.outerComponent
49-
}
50-
51-
// let hullVertices = points.map { $0.cgPoint }
52-
// for i in 0..<hullVertices.count {
53-
// if i == 0 {
54-
// totalPath.move(to: hullVertices[i])
55-
// } else { totalPath.addLine(to: hullVertices[i])}
41+
// let numRepeats = 1
42+
// let radius: CGFloat = 0
43+
// for i in 0..<numRepeats {
44+
// let paddedHull = paddedPolygon(hullVertices, padding: CGFloat(-i) * 10)
45+
// if let path = UIBezierPath.roundedCornersPath(paddedHull, radius) {
46+
// totalPath.append(path)
5647
// }
57-
// totalPath.close()
48+
// }
5849

59-
let hullVertices = points.map { $0.cgPoint }
50+
// let centroid = polygonCentroid(hullVertices)
51+
//
52+
// for vtx in hullVertices {
53+
// totalPath.move(to: centroid)
54+
// totalPath.addLine(to: vtx)
55+
// }
6056

61-
for i in 0..<1 {
62-
let paddedHull = paddedPolygon(hullVertices, padding: CGFloat(-i) * 10)
63-
if let path = UIBezierPath.roundedCornersPath(paddedHull, 10) {
64-
totalPath.append(path)
65-
}
57+
for n in cell.neighbours() {
58+
totalPath.move(to: cell.site.cgPoint)
59+
totalPath.addLine(to: n.site.cgPoint)
6660
}
6761
}
6862

@@ -88,8 +82,8 @@ class GameScene: SKScene {
8882

8983
let body = SKPhysicsBody(circleOfRadius: r)
9084
body.affectedByGravity = false
91-
body.linearDamping = 0
92-
body.mass = 0.0
85+
body.linearDamping = 0.9
86+
body.mass = 2.0
9387

9488
point.physicsBody = body
9589
point.isHidden = false
@@ -148,17 +142,20 @@ class GameScene: SKScene {
148142
func touchMoved(toPoint pos : CGPoint) {
149143
let spriteAtPoint = atPoint(pos)
150144
if spriteAtPoint != diagramNode {
151-
atPoint(pos).position = CGPoint(x: pos.x.rounded(), y: pos.y.rounded())
145+
spriteAtPoint.position = CGPoint(x: pos.x.rounded(), y: pos.y.rounded())
146+
152147
}
153-
redraw(Set<Site>(balls.map { $0.position.point }))
154148
}
155149

156150
func touchUp(atPoint pos : CGPoint) {
157151
let ball = atPoint(pos)
158152
if ball == diagramNode { return }
159-
// balls.removeAll(where: { $0 == ball})
160-
// ball.removeFromParent()
161-
// ball.physicsBody!.affectedByGravity = true
153+
154+
let force = CGVector(
155+
dx: CGFloat.random(in: -300...300),
156+
dy: CGFloat.random(in: -300...300)
157+
)
158+
ball.physicsBody?.applyImpulse(force)
162159
}
163160

164161
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
@@ -178,7 +175,7 @@ class GameScene: SKScene {
178175
}
179176

180177
override func update(_ currentTime: TimeInterval) {
181-
// redraw(Set<Site>(balls.map { $0.position.point }))
178+
redraw(Set<Site>(balls.map { $0.position.point }))
182179
}
183180
}
184181

0 commit comments

Comments
 (0)