-
Notifications
You must be signed in to change notification settings - Fork 9
/
Path+Geometry.swift
164 lines (147 loc) · 5.27 KB
/
Path+Geometry.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// Path+Geometry.swift
//
//
// Created by Luo,Huanyu on 2019/10/13.
//
import Foundation
import CoreGraphics
extension Path {
public var area: CGFloat {
let size = self.count
if size < 3 {
return 0
}
var a:CGFloat = 0.0
var i = 0
while i < self.count - 1 {
a += self[i].x * self[i+1].y
a -= self[i+1].x * self[i].y
i += 1
}
a += self[count-1].x * self[0].y
a -= self[0].x * self[count-1].y
return abs(a * 0.5)
}
public var circumference: CGFloat {
var distance:CGFloat = 0
if self.count < 2 {
return distance
}
for index in 0...self.count-1 {
let point = self[index]
let nextPoint = self[(index+1) % self.count ]
distance += point.distance(to:nextPoint)
}
return distance
}
public var centroid:CGPoint {
var center = CGPoint.zero
let polygonArea = area
for index in 0...self.count-1 {
let vertice = self[index]
let verticeNext = self[(index+1) % self.count]
center.x += (vertice.x+verticeNext.x) * (vertice.x * verticeNext.y - verticeNext.x * vertice.y)
center.y += (vertice.y+verticeNext.y) * (vertice.x * verticeNext.y - verticeNext.x * vertice.y)
}
center.x /= polygonArea*6
center.y /= polygonArea*6
return center
}
public var orientation: Bool {
return self.area >= 0
}
//See "The Point in Polygon Problem for Arbitrary Polygons" by Hormann & Agathos
//http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.88.5498&rep=rep1&type=pdf
public func contains(point pt:CGPoint) -> Int {
//returns 0 if false, +1 if true, -1 if pt ON polygon boundary
var result = 0
let count = self.count
if count < 3 {
return 0
}
var ip = self[0]
for i in 1...count {
let ipNext = (i == count ? self[0] : self[i])
if ipNext.y == pt.y {
if ipNext.x == pt.x || (ip.y == pt.y &&
((ipNext.x > pt.x) == (ip.x < pt.x))) {
return -1
}
}
if (ip.y < pt.y) != (ipNext.y < pt.y) {
if ip.x >= pt.x {
if ipNext.x > pt.x {
result = 1 - result
}else {
let d = CGFloat((ip.x - pt.x) * (ipNext.y - pt.y)) -
CGFloat((ipNext.x - pt.x) * (ip.y - pt.y))
if d == 0 {
return -1
}
if ((d > 0) == (ipNext.y > ip.y)) {
result = 1 - result
}
}
} else {
if ipNext.x > pt.x {
let d = CGFloat((ip.x - pt.x) * (ipNext.y - pt.y)) -
CGFloat((ipNext.x - pt.x) * (ip.y - pt.y))
if d == 0 {
return -1
}
if (d > 0) == (ipNext.y > ip.y) {
result = 1 - result
}
}
}
}
ip = ipNext
}
return result
}
/// Simplify the polygon with Ramer–Douglas–Peucker algorithm.
/// - Parameter epsilon: Threshold value.
func simplify(_ epsilon:CGFloat) -> Path {
var simplePolygon = Path()
var maxIndex = 0
var maxDistance = CGFloat.leastNormalMagnitude
if self.count-2 > 1 {
for index in 1...self.count-2 {
let point1 = self.first!
let point2 = self.last!
let point = self[index]
let signedS = (point.x-point1.x)*(point1.y-point2.y)-(point1.x-point2.x)*(point.y-point1.y)
let s = abs(signedS)
let l = sqrt(pow(point1.x-point2.x,2)+pow(point1.y-point2.y,2))
let distance = s/l
if distance > maxDistance {
maxDistance = distance
maxIndex = index
}
}
if maxDistance > epsilon {
var subPoints1 = Path()
var subPoints2 = Path()
for i in 0...self.count-1 {
if i < maxIndex {
subPoints1.append(self[i])
} else {
subPoints2.append(self[i])
}
}
subPoints1.append(self[maxIndex])
subPoints1 = subPoints1.simplify(epsilon)
subPoints2 = subPoints2.simplify(epsilon)
simplePolygon = subPoints1
if simplePolygon.count > 0 {
simplePolygon.removeLast()
}
simplePolygon.append(contentsOf: subPoints2)
} else {
simplePolygon = [self.first!,self.last!]
}
}
return simplePolygon
}
}