-
Notifications
You must be signed in to change notification settings - Fork 20
/
UIBezierPath+LxThroughPointsBezier.swift
137 lines (95 loc) · 4.44 KB
/
UIBezierPath+LxThroughPointsBezier.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
//
// UIBezierPath+LxThroughPointsBezier.swift
// LxThroughPointsBezier-Swift
//
import Foundation
import UIKit
private var _contractionFactor: CGFloat = 0.7
extension UIBezierPath {
/// The curve‘s bend level. The good value is about 0.6 ~ 0.8. The default and recommended value is 0.7.
var contractionFactor: CGFloat {
get {
return _contractionFactor
}
set {
_contractionFactor = max(0, newValue)
}
}
/**
@param: points Points your bezier wants to through. You must give at least 1 point (different from current point) for drawing curve.
*/
func addBezierThrough(#points: [CGPoint]) {
assert(points.count > 0, "You must give at least 1 point for drawing the bezier.");
if points.count < 3 {
switch points.count {
case 1:
addLineToPoint(points[0])
case 2:
addLineToPoint(points[1])
default:
break
}
return
}
var previousPoint = CGPointZero
var previousCenterPoint = CGPointZero
var centerPoint = CGPointZero
var centerPointDistance = CGFloat()
var obliqueAngle = CGFloat()
var previousControlPoint1 = CGPointZero
var previousControlPoint2 = CGPointZero
var controlPoint1 = CGPointZero
for var i = 0; i < points.count; i++ {
let pointI = points[i]
if i > 0 {
previousCenterPoint = CenterPointOf(currentPoint, previousPoint)
centerPoint = CenterPointOf(previousPoint, pointI)
centerPointDistance = DistanceBetween(previousCenterPoint, centerPoint)
obliqueAngle = ObliqueAngleOfStraightThrough(point1:centerPoint, point2:previousCenterPoint)
previousControlPoint2 = CGPoint(x: previousPoint.x - 0.5 * contractionFactor * centerPointDistance * cos(obliqueAngle), y: previousPoint.y - 0.5 * contractionFactor * centerPointDistance * sin(obliqueAngle))
controlPoint1 = CGPoint(x: previousPoint.x + 0.5 * contractionFactor * centerPointDistance * cos(obliqueAngle), y: previousPoint.y + 0.5 * contractionFactor * centerPointDistance * sin(obliqueAngle))
}
switch i {
case 1 :
addQuadCurveToPoint(previousPoint, controlPoint: previousControlPoint2)
case 2 ..< points.count - 1 :
addCurveToPoint(previousPoint, controlPoint1: previousControlPoint1, controlPoint2: previousControlPoint2)
case points.count - 1 :
addCurveToPoint(previousPoint, controlPoint1: previousControlPoint1, controlPoint2: previousControlPoint2)
addQuadCurveToPoint(pointI, controlPoint: controlPoint1)
default:
break
}
previousControlPoint1 = controlPoint1
previousPoint = pointI
}
}
}
func ObliqueAngleOfStraightThrough(#point1: CGPoint, #point2: CGPoint) -> CGFloat { // [-π/2, 3π/2)
var obliqueRatio: CGFloat = 0
var obliqueAngle: CGFloat = 0
if (point1.x > point2.x) {
obliqueRatio = (point2.y - point1.y) / (point2.x - point1.x)
obliqueAngle = atan(obliqueRatio)
}
else if (point1.x < point2.x) {
obliqueRatio = (point2.y - point1.y) / (point2.x - point1.x)
obliqueAngle = CGFloat(M_PI) + atan(obliqueRatio)
}
else if (point2.y - point1.y >= 0) {
obliqueAngle = CGFloat(M_PI)/2
}
else {
obliqueAngle = -CGFloat(M_PI)/2
}
return obliqueAngle
}
func ControlPointForTheBezierCanThrough(#point1: CGPoint, #point2: CGPoint, #point3: CGPoint) -> CGPoint {
return CGPoint(x: (2 * point2.x - (point1.x + point3.x) / 2), y: (2 * point2.y - (point1.y + point3.y) / 2));
}
func DistanceBetween(point1: CGPoint, point2: CGPoint) -> CGFloat {
return sqrt((point1.x - point2.x) * (point1.x - point2.x) + (point1.y - point2.y) * (point1.y - point2.y))
}
func CenterPointOf(point1: CGPoint, point2: CGPoint) -> CGPoint {
return CGPoint(x: (point1.x + point2.x) / 2, y: (point1.y + point2.y) / 2)
}