-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSBezierPath+SMSubdivision.m
231 lines (182 loc) · 7.83 KB
/
NSBezierPath+SMSubdivision.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// NSBezierPath+Subdivision.m
// SplineSubdivision
//
// Created by Stephan Michels on 05.01.12.
// Copyright (c) 2012 Stephan Michels Softwareentwicklung und Beratung. All rights reserved.
//
#import "NSBezierPath+SMSubdivision.h"
#import "SMLineGeometry.h"
#import "SMSplineGeometry.h"
@implementation NSBezierPath (SMSubdivision)
- (NSBezierPath *)subpathFromLength:(CGFloat)start toLength:(CGFloat)end {
if (start >= end) {
return [NSBezierPath bezierPath];
}
NSPoint points[3];
BOOL started = NO;
NSPoint previousPoint;
NSBezierPath *subpath = [NSBezierPath bezierPath];
for (NSUInteger elementIndex = 0; elementIndex < [self elementCount]; elementIndex++) {
// if we already reached the end
if (end < 0) {
return subpath;
}
NSBezierPathElement element = [self elementAtIndex:(NSInteger)elementIndex associatedPoints:points];
switch (element) {
case NSMoveToBezierPathElement: {
started = NO;
previousPoint = points[0];
} break;
case NSLineToBezierPathElement: {
NSPoint p1 = previousPoint;
NSPoint p2 = points[0];
CGFloat lineLength = SMLineGetLength(p1, p2);
// check if the reached the start
if (start < lineLength) {
double u1 = MAX(start, 0) / lineLength;
double u2 = MIN(lineLength, end) / lineLength;
CGPoint startPoint = SMLineGetPointAtParameter(p1, p2, u1);
CGPoint endPoint = SMLineGetPointAtParameter(p1, p2, u2);
if (!started) {
started = YES;
[subpath moveToPoint:startPoint];
}
[subpath lineToPoint:endPoint];
}
start -= lineLength;
end -= lineLength;
previousPoint = p2;
} break;
case NSCurveToBezierPathElement: {
CGPoint p1 = previousPoint;
CGPoint p2 = points[0];
CGPoint p3 = points[1];
CGPoint p4 = points[2];
CGFloat curveLength = SMSplineGetTotalLength(p1, p2, p3, p4);
// check if the reached the start
if (start < curveLength) {
double u1 = SMSplineParameterForLength(p1, p2, p3, p4, start);
double u2 = SMSplineParameterForLength(p1, p2, p3, p4, end);
CGPoint sdp1 = p1;
CGPoint sdp2 = p2;
CGPoint sdp3 = p3;
CGPoint sdp4 = p4;
// check to remove a part at the beginning of the current curve element
if (u1 > 0.0) {
SMSplineGetSubdivisionAtParameter(sdp1, sdp2, sdp3, sdp4, u1, NO,
&sdp1, &sdp2, &sdp3, &sdp4);
[subpath moveToPoint:sdp1];
started = YES;
}
if (!started) {
[subpath moveToPoint:sdp1];
started = YES;
}
// check to remove a part at the end of the current curve element
if (u2 < 1.0) {
SMSplineGetSubdivisionAtParameter(sdp1, sdp2, sdp3, sdp4, (u2 - u1) / (1 - u1), YES,
&sdp1, &sdp2, &sdp3, &sdp4);
}
[subpath curveToPoint:sdp4 controlPoint1:sdp2 controlPoint2:sdp3];
}
start -= curveLength;
end -= curveLength;
previousPoint = p4;
} break;
case NSClosePathBezierPathElement: {
[subpath closePath];
started = NO;
} break;
default:
break;
}
}
return subpath;
}
- (CGPoint)pointAtLength:(CGFloat)length {
NSPoint points[3];
NSPoint previousPoint = NSZeroPoint;
for (NSUInteger elementIndex = 0; elementIndex < [self elementCount]; elementIndex++) {
NSBezierPathElement element = [self elementAtIndex:(NSInteger)elementIndex associatedPoints:points];
switch (element) {
case NSMoveToBezierPathElement: {
NSPoint p = points[0];
if (length <= 0) {
return p;
}
previousPoint = p;
} break;
case NSLineToBezierPathElement: {
NSPoint p1 = previousPoint;
NSPoint p2 = points[0];
CGFloat lineLength = SMLineGetLength(p1, p2);
if (length <= 0) {
return p1;
} else if (length <= lineLength) {
return SMLineGetPointAtParameter(p1, p2, length / lineLength);
} else {
length -= lineLength;
}
previousPoint = p2;
} break;
case NSCurveToBezierPathElement: {
CGPoint p1 = previousPoint;
CGPoint p2 = points[0];
CGPoint p3 = points[1];
CGPoint p4 = points[2];
CGFloat curveLength = SMSplineGetTotalLength(p1, p2, p3, p4);
if (length <= 0) {
return p1;
} else if (length <= curveLength) {
double u = SMSplineParameterForLength(p1, p2, p3, p4, length);
return SMSplineGetPointAtParameter(p1, p2, p3, p4, u);
} else {
length -= curveLength;
}
previousPoint = points[2];
} break;
case NSClosePathBezierPathElement: {
} break;
default:
break;
}
}
return previousPoint;
}
- (CGFloat)length {
CGFloat length = 0;
NSPoint points[3];
NSPoint previousPoint = NSZeroPoint;
for (NSUInteger elementIndex = 0; elementIndex < [self elementCount]; elementIndex++) {
NSBezierPathElement element = [self elementAtIndex:(NSInteger)elementIndex associatedPoints:points];
switch (element) {
case NSMoveToBezierPathElement: {
NSPoint p = points[0];
previousPoint = p;
} break;
case NSLineToBezierPathElement: {
NSPoint p1 = previousPoint;
NSPoint p2 = points[0];
CGFloat lineLength = SMLineGetLength(p1, p2);
length += lineLength;
previousPoint = p2;
} break;
case NSCurveToBezierPathElement: {
CGPoint p1 = previousPoint;
CGPoint p2 = points[0];
CGPoint p3 = points[1];
CGPoint p4 = points[2];
CGFloat curveLength = SMSplineGetTotalLength(p1, p2, p3, p4);
length += curveLength;
previousPoint = points[2];
} break;
case NSClosePathBezierPathElement: {
} break;
default:
break;
}
}
return length;
}
@end