Skip to content

Commit 93338ca

Browse files
author
Oleksandr Glagoliev
committed
Improve SK Demo
1 parent 9dbd14e commit 93338ca

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

SpriteKitDemo/SpriteKitDemo/GameScene.swift

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class GameScene: SKScene {
1414
var diagram = Diagram()
1515

1616
var balls = [SKShapeNode]()
17-
var diagramNode: SKShapeNode = SKShapeNode()
17+
var voronoiDiagramNode = SKShapeNode()
18+
var delaunayDiagramNode = SKShapeNode()
1819

1920
private var clippingRect: Rectangle {
2021
Rectangle(
@@ -24,7 +25,8 @@ class GameScene: SKScene {
2425
}
2526

2627
private func redraw(_ sites: Set<Site>) {
27-
let totalPath = UIBezierPath()
28+
let voronoiPath = UIBezierPath()
29+
let delaunayPath = UIBezierPath()
2830

2931
diagram.clear()
3032
fs.compute(
@@ -34,39 +36,46 @@ class GameScene: SKScene {
3436
)
3537

3638
diagram.cells.forEach { cell in
37-
// let hullVertices = cell
38-
// .hullVerticesCCW()
39-
// .map { $0.cgPoint }
39+
let hullVertices = cell
40+
.hullVerticesCCW()
41+
.map { $0.cgPoint }
4042

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)
47-
// }
48-
// }
43+
let numRepeats = 1
44+
let radius: CGFloat = 0
45+
for i in 0..<numRepeats {
46+
let paddedHull = paddedPolygon(hullVertices, padding: CGFloat(-i) * 10)
47+
if let path = UIBezierPath.roundedCornersPath(paddedHull, radius) {
48+
voronoiPath.append(path)
49+
}
50+
}
4951

5052
// let centroid = polygonCentroid(hullVertices)
5153
//
5254
// for vtx in hullVertices {
53-
// totalPath.move(to: centroid)
54-
// totalPath.addLine(to: vtx)
55+
// voronoiPath.move(to: centroid)
56+
// voronoiPath.addLine(to: vtx)
5557
// }
5658

59+
5760
for n in cell.neighbours() {
58-
totalPath.move(to: cell.site.cgPoint)
59-
totalPath.addLine(to: n.site.cgPoint)
61+
delaunayPath.move(to: cell.site.cgPoint)
62+
delaunayPath.addLine(to: n.site.cgPoint)
6063
}
6164
}
65+
voronoiDiagramNode.path = voronoiPath.cgPath
66+
voronoiDiagramNode.strokeColor = UIColor.black
67+
voronoiDiagramNode.lineWidth = 3.0
68+
voronoiDiagramNode.fillColor = .clear
6269

63-
diagramNode.path = totalPath.cgPath
64-
diagramNode.strokeColor = UIColor.black
65-
diagramNode.fillColor = .clear
70+
delaunayDiagramNode.path = delaunayPath.cgPath
71+
delaunayDiagramNode.strokeColor = UIColor.green
72+
delaunayDiagramNode.lineWidth = 1.0
73+
delaunayDiagramNode.fillColor = .clear
6674
}
6775

6876
override func didMove(to view: SKView) {
69-
addChild(diagramNode)
77+
addChild(voronoiDiagramNode)
78+
addChild(delaunayDiagramNode)
7079

7180
let offset: Double = 50
7281
let lbx = offset
@@ -141,15 +150,15 @@ class GameScene: SKScene {
141150

142151
func touchMoved(toPoint pos : CGPoint) {
143152
let spriteAtPoint = atPoint(pos)
144-
if spriteAtPoint != diagramNode {
153+
if spriteAtPoint != voronoiDiagramNode {
145154
spriteAtPoint.position = CGPoint(x: pos.x.rounded(), y: pos.y.rounded())
146155

147156
}
148157
}
149158

150159
func touchUp(atPoint pos : CGPoint) {
151160
let ball = atPoint(pos)
152-
if ball == diagramNode { return }
161+
if ball == voronoiDiagramNode { return }
153162

154163
let force = CGVector(
155164
dx: CGFloat.random(in: -300...300),

0 commit comments

Comments
 (0)