/
BezierFunctions.m
205 lines (173 loc) · 5.72 KB
/
BezierFunctions.m
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
/*
Erica Sadun, http://ericasadun.com
*/
#import "Bezier.h"
#pragma mark - Bezier functions
float CubicBezier(float t, float start, float c1, float c2, float end)
{
CGFloat t_ = (1.0 - t);
CGFloat tt_ = t_ * t_;
CGFloat ttt_ = t_ * t_ * t_;
CGFloat tt = t * t;
CGFloat ttt = t * t * t;
return start * ttt_
+ 3.0 * c1 * tt_ * t
+ 3.0 * c2 * t_ * tt
+ end * ttt;
}
float QuadBezier(float t, float start, float c1, float end)
{
CGFloat t_ = (1.0 - t);
CGFloat tt_ = t_ * t_;
CGFloat tt = t * t;
return start * tt_
+ 2.0 * c1 * t_ * t
+ end * tt;
}
CGPoint CubicBezierPoint(CGFloat t, CGPoint start, CGPoint c1, CGPoint c2, CGPoint end)
{
CGPoint result;
result.x = CubicBezier(t, start.x, c1.x, c2.x, end.x);
result.y = CubicBezier(t, start.y, c1.y, c2.y, end.y);
return result;
}
CGPoint QuadBezierPoint(CGFloat t, CGPoint start, CGPoint c1, CGPoint end)
{
CGPoint result;
result.x = QuadBezier(t, start.x, c1.x, end.x);
result.y = QuadBezier(t, start.y, c1.y, end.y);
return result;
}
float CubicBezierLength(CGPoint start, CGPoint c1, CGPoint c2, CGPoint end)
{
int steps = NUMBER_OF_BEZIER_SAMPLES;
CGPoint current;
CGPoint previous;
float length = 0.0;
for (int i = 0; i <= steps; i++)
{
float t = (float) i / (float) steps;
current = CubicBezierPoint(t, start, c1, c2, end);
if (i > 0)
length += PointDistanceFromPoint(current, previous);
previous = current;
}
return length;
}
float QuadBezierLength(CGPoint start, CGPoint c1, CGPoint end)
{
int steps = NUMBER_OF_BEZIER_SAMPLES;
CGPoint current;
CGPoint previous;
float length = 0.0;
for (int i = 0; i <= steps; i++)
{
float t = (float) i / (float) steps;
current = QuadBezierPoint(t, start, c1, end);
if (i > 0)
length += PointDistanceFromPoint(current, previous);
previous = current;
}
return length;
}
#pragma mark - Point Percents
#define USE_CURVE_TWEAK 0
#if USE_CURVE_TWEAK
#define CURVETWEAK 0.8
#else
#define CURVETWEAK 1.0
#endif
CGFloat ElementDistanceFromPoint(BezierElement *element, CGPoint point, CGPoint startPoint)
{
CGFloat distance = 0.0f;
switch (element.elementType)
{
case kCGPathElementMoveToPoint:
return 0.0f;
case kCGPathElementCloseSubpath:
return PointDistanceFromPoint(point, startPoint);
case kCGPathElementAddLineToPoint:
return PointDistanceFromPoint(point, element.point);
case kCGPathElementAddCurveToPoint:
return CubicBezierLength(point, element.controlPoint1, element.controlPoint2, element.point) * CURVETWEAK;
case kCGPathElementAddQuadCurveToPoint:
return QuadBezierLength(point, element.controlPoint1, element.point) * CURVETWEAK;
}
return distance;
}
// Centralize for both close subpath and add line to point
CGPoint InterpolateLineSegment(CGPoint p1, CGPoint p2, CGFloat percent, CGPoint *slope)
{
CGFloat dx = p2.x - p1.x;
CGFloat dy = p2.y - p1.y;
if (slope)
*slope = CGPointMake(dx, dy);
CGFloat px = p1.x + dx * percent;
CGFloat py = p1.y + dy * percent;
return CGPointMake(px, py);
}
// Interpolate along element
CGPoint InterpolatePointFromElement(BezierElement *element, CGPoint point, CGPoint startPoint, CGFloat percent, CGPoint *slope)
{
switch (element.elementType)
{
case kCGPathElementMoveToPoint:
{
// No distance
if (slope)
*slope = CGPointMake(INFINITY, INFINITY);
return point;
}
case kCGPathElementCloseSubpath:
{
// from self.point to firstPoint
CGPoint p = InterpolateLineSegment(point, startPoint, percent, slope);
return p;
}
case kCGPathElementAddLineToPoint:
{
// from point to self.point
CGPoint p = InterpolateLineSegment(point, element.point, percent, slope);
return p;
}
case kCGPathElementAddQuadCurveToPoint:
{
// from point to self.point
CGPoint p = QuadBezierPoint(percent, point, element.controlPoint1, element.point);
CGFloat dx = p.x - QuadBezier(percent * 0.9, point.x, element.controlPoint1.x, element.point.x);
CGFloat dy = p.y - QuadBezier(percent * 0.9, point.y, element.controlPoint1.y, element.point.y);
if (slope)
*slope = CGPointMake(dx, dy);
return p;
}
case kCGPathElementAddCurveToPoint:
{
// from point to self.point
CGPoint p = CubicBezierPoint(percent, point, element.controlPoint1, element.controlPoint2, element.point);
CGFloat dx = p.x - CubicBezier(percent * 0.9, point.x, element.controlPoint1.x, element.controlPoint2.x, element.point.x);
CGFloat dy = p.y - CubicBezier(percent * 0.9, point.y, element.controlPoint1.y, element.controlPoint2.y, element.point.y);
if (slope)
*slope = CGPointMake(dx, dy);
return p;
}
}
return NULLPOINT;
}
CGFloat EaseIn(CGFloat currentTime, int factor)
{
return powf(currentTime, factor);
}
CGFloat EaseOut(CGFloat currentTime, int factor)
{
return 1 - powf((1 - currentTime), factor);
}
CGFloat EaseInOut(CGFloat currentTime, int factor)
{
currentTime = currentTime * 2.0;
if (currentTime < 1)
return (0.5 * pow(currentTime, factor));
currentTime -= 2.0;
if (factor % 2)
return 0.5 * (pow(currentTime, factor) + 2.0);
return 0.5 * (2.0 - pow(currentTime, factor));
}