-
Notifications
You must be signed in to change notification settings - Fork 4
/
NSBezierPath+Geometry.m
2471 lines (1856 loc) · 62.9 KB
/
NSBezierPath+Geometry.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///**********************************************************************************************************************************
/// NSBezierPath-Geometry.m
/// DrawKit ©2005-2008 Apptree.net
///
/// Created by graham on 22/10/2006.
///
/// This software is released subject to licensing conditions as detailed in DRAWKIT-LICENSING.TXT, which must accompany this source file.
///
///**********************************************************************************************************************************
#import "NSBezierPath+Geometry.h"
#import "DKDrawKitMacros.h"
#import "DKGeometryUtilities.h"
#import "DKRandom.h"
#import "NSBezierPath+Editing.h"
#include <math.h>
#if 0
static void ConvertPathApplierFunction ( void *info, const CGPathElement *element );
#endif
static float lengthOfBezier(const NSPoint bez[4], float acceptableError);
static inline float distanceBetween(NSPoint a, NSPoint b);
static void InterpolatePoints( const NSPoint* pointsIn, NSPoint* cp1, NSPoint* cp2, const float smooth_value );
static NSPoint CornerPoint( const NSPoint* pointsIn, float offset, float miterLimit );
static BOOL CornerArc( const NSPoint* pointsIn, float offset, NSBezierPath* newPath );
static BOOL CornerBevel( const NSPoint* pointsIn, float offset, NSBezierPath* newPath );
@interface NSBezierPath (Geometry_Private)
- (NSBezierPath*) paralleloidPathWithOffset3:(float) delta lineJoinStyle:(NSLineJoinStyle) js;
@end
@implementation NSBezierPath (Geometry)
- (NSBezierPath*) scaledPath:(float) scale
{
// returns a copy of the receiver scaled by <scale>, with the path's origin assumed to be at the centre of its bounds rect.
NSPoint cp = [self centreOfBounds];
return [self scaledPath:scale aboutPoint:cp];
}
- (NSBezierPath*) scaledPath:(float) scale aboutPoint:(NSPoint) cp
{
// This is like an inset or an outset operation. If scale is 1.0, self is returned.
if( scale == 1.0 )
return self;
else
{
NSBezierPath* copy = [self copy];
NSAffineTransform* xfm = [NSAffineTransform transform];
[xfm translateXBy:cp.x yBy:cp.y];
[xfm scaleXBy:scale yBy:scale];
[xfm translateXBy:-cp.x yBy:-cp.y];
[copy transformUsingAffineTransform:xfm];
return [copy autorelease];
}
}
- (NSBezierPath*) rotatedPath:(float) angle
{
// return a rotated copy of the receiver. The origin is taken as the centre of the path bounds.
// angle is a value in radians
return [self rotatedPath:angle aboutPoint:[self centreOfBounds]];
}
- (NSBezierPath*) rotatedPath:(float) angle aboutPoint:(NSPoint) cp
{
// return a rotated copy of the receiver. The origin is taken as point <cp> relative to the original path.
// angle is a value in radians
if( angle == 0.0 )
return self;
else
{
NSBezierPath* copy = [self copy];
NSAffineTransform* xfm = RotationTransform( angle, cp );
[copy transformUsingAffineTransform:xfm];
return [copy autorelease];
}
}
- (NSBezierPath*) insetPathBy:(float) amount
{
// returns a scaled copy of the receiver, calculating the scale by adding <amount> to all edges of the bounds.
// since this can scale differently in x and y directions, this doesn't call the scale function but works
// very similarly.
// note that due to the mathematics of bezier curves, this may not produce exactly perfect results for some
// curves.
// +ve values of <amount> inset (shrink) the path, -ve values outset (grow) the shape.
if( amount == 0.0 )
return self;
else
{
NSRect r = NSInsetRect([self bounds], amount, amount );
float xs, ys;
xs = r.size.width / [self bounds].size.width;
ys = r.size.height / [self bounds].size.height;
NSBezierPath* copy = [self copy];
NSPoint cp = [copy centreOfBounds];
NSAffineTransform* xfm = [NSAffineTransform transform];
[xfm translateXBy:cp.x yBy:cp.y];
[xfm scaleXBy:xs yBy:ys];
[xfm translateXBy:-cp.x yBy:-cp.y];
[copy transformUsingAffineTransform:xfm];
return [copy autorelease];
}
}
- (NSBezierPath*) horizontallyFlippedPathAboutPoint:(NSPoint) cp
{
NSBezierPath* copy = [self copy];
NSAffineTransform* xfm = [NSAffineTransform transform];
[xfm translateXBy:cp.x yBy:cp.y];
[xfm scaleXBy:-1.0 yBy:1.0];
[xfm translateXBy:-cp.x yBy:-cp.y];
[copy transformUsingAffineTransform:xfm];
return [copy autorelease];
}
- (NSBezierPath*) verticallyFlippedPathAboutPoint:(NSPoint) cp
{
NSBezierPath* copy = [self copy];
NSAffineTransform* xfm = [NSAffineTransform transform];
[xfm translateXBy:cp.x yBy:cp.y];
[xfm scaleXBy:1.0 yBy:-1.0];
[xfm translateXBy:-cp.x yBy:-cp.y];
[copy transformUsingAffineTransform:xfm];
return [copy autorelease];
}
- (NSBezierPath*) horizontallyFlippedPath
{
return [self horizontallyFlippedPathAboutPoint:[self centreOfBounds]];
}
- (NSBezierPath*) verticallyFlippedPath
{
return [self verticallyFlippedPathAboutPoint:[self centreOfBounds]];
}
- (NSPoint) centreOfBounds
{
return NSMakePoint( NSMidX([self bounds]), NSMidY([self bounds]));
}
- (float) minimumCornerAngle
{
// returns the smallest angle subtended by any segment join in the path. The largest value this can be is pi (180 degrees), the smallest is 0. The
// resultis in radians. Can be used to determine the necessary bounding rect of the path for a given stroke width and miter limit. For curve
// elements, the curvature is ignored and the element treated as a line segment.
float v, a = pi;
int i, m = [self elementCount] - 1;
NSBezierPathElement element, nextElement;
NSPoint fp, cp, pp, xp, ap[3], np[3];
fp = cp = pp = xp = NSZeroPoint;
for( i = 0; i < m; ++i )
{
element = [self elementAtIndex:i associatedPoints:ap];
nextElement = [self elementAtIndex:i + 1 associatedPoints:np];
switch( element )
{
case NSMoveToBezierPathElement:
fp = pp = ap[0];
continue;
case NSLineToBezierPathElement:
cp = ap[0];
break;
case NSCurveToBezierPathElement:
cp = ap[2];
break;
case NSClosePathBezierPathElement:
cp = fp;
break;
default:
break;
}
switch( nextElement )
{
case NSMoveToBezierPathElement:
continue;
case NSLineToBezierPathElement:
xp = np[0];
break;
case NSCurveToBezierPathElement:
xp = np[2];
break;
case NSClosePathBezierPathElement:
xp = fp;
break;
default:
break;
}
v = fabs( AngleBetween( pp, cp, xp ));
if ( v < a )
a = v;
pp = cp;
}
return a;
}
- (NSBezierPath*) bezierPathByIteratingWithDelegate:(id) delegate contextInfo:(void*) contextInfo
{
// this method allows a delegate to use the info from the receiver to build a new path element by element. This is a generic method that is intended to
// avoid the need to write these loops over and over. The delegate is passed the points of each element in an order that is easier to work with than
// the native list and also always includes the last point in a subpath.
NSAssert( delegate != nil, @"cannot operate with a nil delegate");
if(![delegate respondsToSelector:@selector(path:elementIndex:type:points:subPathIndex:subPathClosed:contextInfo:)])
return nil;
if([self isEmpty])
return nil;
NSBezierPath* newPath = [NSBezierPath bezierPath];
int i, m, spi = -1;
NSPoint ap[3];
NSPoint rp[4];
BOOL spc = NO;
NSBezierPathElement element;
m = [self elementCount];
for( i = 0; i < m; ++i )
{
element = [self elementAtIndex:i associatedPoints:ap];
if( element == NSMoveToBezierPathElement )
{
// get info about the end of this subpath
++spi;
// is it closed?
spc = [self subpathContainingElementIsClosed:i];
// what is its end point?
int spe = [self subpathEndingElementForElement:i];
NSPoint ep[3];
if ( spc )
--spe; // ignore closePath element
NSBezierPathElement ee = [self elementAtIndex:spe associatedPoints:ep];
if ( ee == NSCurveToBezierPathElement )
rp[3] = ep[2];
else
rp[3] = ep[0];
}
// set up the list of points (reordered from ap). The list passed to the delegate is always ordered thus:
//[0] = the next on-path point
//[1] = cp1 for a curve element
//[2] = cp2 for a curve element
//[3] = the end point of the current subpath
if( element == NSCurveToBezierPathElement )
{
rp[0] = ap[2];
rp[1] = ap[0];
rp[2] = ap[1];
}
else
rp[0] = rp[1] = rp[2] = ap[0];
// let the delegate do its thing with the info:
[delegate path:newPath elementIndex:i type:element points:rp subPathIndex:spi subPathClosed:spc contextInfo:contextInfo];
}
return newPath;
}
- (NSBezierPath*) paralleloidPathWithOffset:(float) delta
{
// returns a copy of the receiver modified by offsetting all of its control points by <delta> in the direction of the
// normal of the path at the location of the on-path control point. This will create a parallel-ish offset path that works
// for most non-pathological paths. Given that there is no known mathematically correct way to do this (for bezier curves), this works well enough in
// many practical situations. Positive delta moves the path below or to the right, -ve is up and left.
NSBezierPath* newPath = [NSBezierPath bezierPath];
if( ![self isEmpty])
{
int i, count = [self elementCount];
NSPoint ap[3], np[3], p0, p1;
NSBezierPathElement kind, nextKind;
float slope, dx, dy, pdx, pdy;
pdx = pdy = 0;
for( i = 0; i < count; ++i )
{
kind = [self elementAtIndex:i associatedPoints:ap];
if ( i < count - 1 )
{
nextKind = [self elementAtIndex:i + 1 associatedPoints:np];
// calculate the slope of the on-path point
if ( kind != NSCurveToBezierPathElement )
{
p0 = ap[0];
p1 = np[0];
}
else
{
p0 = ap[2];
p1 = np[0];
}
}
else
{
if ( kind == NSCurveToBezierPathElement )
{
p1 = ap[2];
p0 = ap[1];
}
else
{
p1 = ap[0];
nextKind = [self elementAtIndex:i - 1 associatedPoints:np];
if ( nextKind != NSCurveToBezierPathElement )
p0 = np[0];
else
p0 = np[2];
}
}
slope = atan2f( p1.y - p0.y, p1.x - p0.x ) + ( pi * 0.5 );
// calculate the position of the modified point
dx = delta * cosf( slope );
dy = delta * sinf( slope );
switch( kind )
{
case NSMoveToBezierPathElement:
ap[0].x += dx;
ap[0].y += dy;
[newPath moveToPoint:ap[0]];
break;
case NSLineToBezierPathElement:
ap[0].x += dx;
ap[0].y += dy;
[newPath lineToPoint:ap[0]];
break;
case NSCurveToBezierPathElement:
ap[0].x += pdx;
ap[0].y += pdy;
ap[1].x += dx;
ap[1].y += dy;
ap[2].x += dx;
ap[2].y += dy;
[newPath curveToPoint:ap[2] controlPoint1:ap[0] controlPoint2:ap[1]];
break;
case NSClosePathBezierPathElement:
[newPath closePath];
break;
default:
break;
}
pdx = dx;
pdy = dy;
}
}
return newPath;
}
static NSPoint CornerPoint( const NSPoint* pointsIn, float offset, float miterLimit )
{
// given 3 points in pointsIn, this returns the point that bisects the angle between the vertices, and is extended to intercept the <offset>
// parallel up to <miterLimit>. This is used to compute the correct location of a vertex for a parallel offset path.
// for zero offset, result is simply second point.
// The three points are three consecutive vertices from the original path.
if( offset == 0.0 )
return pointsIn[1];
NSPoint rp;
float relAngle, r, s1, s2, angle;
s1 = Slope( pointsIn[0], pointsIn[1]);
s2 = Slope( pointsIn[1], pointsIn[2]);
relAngle = ( s2 - s1 ) * 0.5f;
r = offset / cosf( relAngle );
angle = s1 + relAngle + NINETY_DEGREES;
float maxR = fabs( miterLimit * offset );
if( r > maxR )
r = maxR;
if( r < -maxR )
r = -maxR;
rp.x = pointsIn[1].x + r * cosf( angle );
rp.y = pointsIn[1].y + r * sinf( angle );
return rp;
}
static BOOL CornerArc( const NSPoint* pointsIn, float offset, NSBezierPath* newPath )
{
// returns an arc segment that is centred at the middle vertex having a radius of <offset> and a start point and end point such that the offset normals to the original
// edges are joined by the arc. If the vertex is an inside bend, returns nil in which case the CornerPoint should be used.
if( offset == 0.0 )
return NO;
float s1, s2, ra;
s1 = Slope( pointsIn[0], pointsIn[1]);
s2 = Slope( pointsIn[2], pointsIn[1]);
// only the arc that goes around the "outside" of the bend is required, so we need a way to detect which side of the line we are offsetting to
// and only append an arc for the outside case.
ra = s2 - s1;
if( ra > pi )
ra = pi - ra;
if( ra < -pi )
ra = -pi - ra;
//NSLog(@"ra = %f, offset = %f", ra, offset );
if(( ra < 0 && offset > 0 ) || ( ra > 0 && offset < 0 ))
return NO;
s2 = Slope( pointsIn[1], pointsIn[2]);
[newPath appendBezierPathWithArcWithCenter:pointsIn[1] radius:offset startAngle:RADIANS_TO_DEGREES(s1 + NINETY_DEGREES) endAngle:RADIANS_TO_DEGREES(s2 + NINETY_DEGREES) clockwise:offset > 0 ];
return YES;
}
static BOOL CornerBevel( const NSPoint* pointsIn, float offset, NSBezierPath* newPath )
{
// appends a bevel segment that is centred at the middle vertex having a radius of <offset> and a start point and end point such that the offset normals to the original
// edges are joined by the arc. If the vertex is an inside bend, returns nil in which case the CornerPoint should be used.
if( offset == 0.0 )
return NO;
float s1, s2, ra;
s1 = Slope( pointsIn[0], pointsIn[1]);
s2 = Slope( pointsIn[2], pointsIn[1]);
// only the arc that goes around the "outside" of the bend is required, so we need a way to detect which side of the line we are offsetting to
// and only append an arc for the outside case.
ra = s2 - s1;
if( ra > pi )
ra = pi - ra;
if( ra < -pi )
ra = -pi - ra;
//NSLog(@"ra = %f, offset = %f", ra, offset );
if(( ra < 0 && offset > 0 ) || ( ra > 0 && offset < 0 ))
return NO;
NSPoint pa, pb;
pa.x = pointsIn[1].x + offset * cosf( s1 + NINETY_DEGREES );
pa.y = pointsIn[1].y + offset * sinf( s1 + NINETY_DEGREES );
pb.x = pointsIn[1].x + offset * cosf( s2 - NINETY_DEGREES );
pb.y = pointsIn[1].y + offset * sinf( s2 - NINETY_DEGREES );
[newPath lineToPoint:pa];
[newPath lineToPoint:pb];
return YES;
}
- (NSBezierPath*) paralleloidPathWithOffset2:(float) delta
{
// returns a path offset by <delta>, using the paralleloidPathWithOffset method above on a flattened version of the path. If the caller sets the
// default flatness prior to calling this they can control the fineness of the offset path. The offset joins are set to match the current line join style.
if( delta == 0.0 )
return self;
NSBezierPath* temp;
temp = [self bezierPathByFlatteningPath];
temp = [temp paralleloidPathWithOffset:delta];
return temp;
}
- (NSBezierPath*) paralleloidPathWithOffset22:(float) delta
{
// returns a path offset by <delta>, using the paralleloidPathWithOffset3 method below on a flattened version of the path. If the caller sets the
// default flatness prior to calling this they can control the fineness of the offset path. The offset joins are set to match the current line join style.
if( delta == 0.0 )
return self;
NSBezierPath* temp;
temp = [self bezierPathByFlatteningPath];
temp = [temp paralleloidPathWithOffset3:delta lineJoinStyle:[self lineJoinStyle]];
return temp;
}
- (NSBezierPath*) paralleloidPathWithOffset3:(float) delta lineJoinStyle:(NSLineJoinStyle) js
{
// requires flattened path, calculates correct points at the corners
NSBezierPath* newPath = [NSBezierPath bezierPath];
int i, m = [self elementCount], spc = 0, spStartIndex;
NSBezierPathElement element;
NSPoint ap[3];
NSPoint v[3];
NSPoint fp, op, fop, sop;
float slope;
v[0] = v[1] = v[2] = NSZeroPoint;
for( i = 0; i < m; ++i )
{
element = [self elementAtIndex:i associatedPoints:ap];
switch( element )
{
case NSMoveToBezierPathElement:
// starting a new subpath - don't start the new path yet as we need the next point to know the slope
fp = v[0] = ap[0];
spc = 0;
break;
case NSLineToBezierPathElement:
if( spc == 0 )
{
// recently started a new subpath, so set 2nd vertex
v[1] = ap[0];
spc++;
// ok, we have enough to work out the slope and start the new path
slope = Slope( v[0], v[1] ) + pi * 0.5f;
op.x = v[0].x + delta * cosf( slope );
op.y = v[0].y + delta * sinf( slope );
[newPath moveToPoint:op];
spStartIndex = [newPath elementCount] - 1;
fop = op;
}
else
{
v[2] = ap[0];
// we have three vertices, so we can calculate the required corner point
op = CornerPoint( v, delta, [self miterLimit] );
if( js == NSMiterLineJoinStyle )
[newPath lineToPoint:op];
else if( js == NSBevelLineJoinStyle )
{
if( !CornerBevel( v, delta, newPath ))
[newPath lineToPoint:op];
}
else
{
if( !CornerArc( v, delta, newPath ))
[newPath lineToPoint:op];
}
if( spc == 1 )
sop = op;
// shift vertex array
v[0] = v[1];
v[1] = v[2];
spc++;
}
break;
case NSCurveToBezierPathElement:
NSAssert( NO, @"paralleloidPathWithOffset3 requires a flattened path");
break;
case NSClosePathBezierPathElement:
// close the path by curving back to the first point
v[2] = fp;
op = CornerPoint( v, delta, [self miterLimit] );
if( js == NSMiterLineJoinStyle )
[newPath lineToPoint:op];
else if( js == NSBevelLineJoinStyle )
{
if( !CornerBevel( v, delta, newPath ))
[newPath lineToPoint:op];
}
else
{
if( !CornerArc( v, delta, newPath ))
[newPath lineToPoint:op];
}
v[0] = v[1];
v[1] = v[2];
v[2] = sop;
op = CornerPoint( v, delta, [self miterLimit]);
if( js == NSMiterLineJoinStyle )
{
[newPath lineToPoint:op];
[newPath setAssociatedPoints:&op atIndex:spStartIndex];
}
else if( js == NSBevelLineJoinStyle )
{
if( !CornerBevel( v, delta, newPath ))
{
[newPath lineToPoint:op];
[newPath setAssociatedPoints:&op atIndex:spStartIndex];
}
}
else
{
if( !CornerArc( v, delta, newPath ))
{
[newPath lineToPoint:op];
[newPath setAssociatedPoints:&op atIndex:spStartIndex];
}
}
spc = 0;
[newPath closePath];
break;
default:
break;
}
}
if( spc > 0 )
{
// open-ended path, place last offset point
slope = Slope( v[0], v[1] ) + pi * 0.5f;
op.x = v[1].x + delta * cosf( slope );
op.y = v[1].y + delta * sinf( slope );
[newPath lineToPoint:op];
}
return newPath;
}
- (NSBezierPath*) offsetPathWithStartingOffset:(float) delta1 endingOffset:(float) delta2
{
// similar to making a paralleloid path, but instead of a constant offset, each point has a different offset
// applied as a linear function of the difference between delta1 and delta2. So the result has a similar curvature
// to the original path, but also an additional ramp.
NSBezierPath* newPath = [NSBezierPath bezierPath];
if( ![self isEmpty])
{
int i, count = [self elementCount];
NSPoint lp, ap[3], np[3], p0, p1, fp;
NSBezierPathElement kind, nextKind;
float del, length, slope, dx, dy, pdx, pdy;
pdx = pdy = 0;
length = [self length];
for( i = 0; i < count; ++i )
{
del = ((( delta2 - delta1 ) * i ) / ( count - 1 )) + delta1;
// LogEvent_(kInfoEvent, @"segment %d, del = %f", i, del );
kind = [self elementAtIndex:i associatedPoints:ap];
if ( i < count - 1 )
{
nextKind = [self elementAtIndex:i + 1 associatedPoints:np];
// calculate the slope of the on-path point
if ( kind != NSCurveToBezierPathElement )
{
p0 = ap[0];
p1 = np[0];
}
else
{
p0 = ap[2];
p1 = np[0];
}
}
else
{
if ( kind == NSCurveToBezierPathElement )
{
p1 = ap[2];
p0 = ap[1];
}
else
{
p1 = ap[0];
nextKind = [self elementAtIndex:i - 1 associatedPoints:np];
if ( nextKind != NSCurveToBezierPathElement )
p0 = np[0];
else
p0 = np[2];
}
}
slope = atan2f( p1.y - p0.y, p1.x - p0.x ) + ( pi * 0.5 );
// calculate the position of the modified point
dx = del * cosf( slope );
dy = del * sinf( slope );
switch( kind )
{
case NSMoveToBezierPathElement:
ap[0].x += dx;
ap[0].y += dy;
[newPath moveToPoint:ap[0]];
fp = lp = ap[0];
break;
case NSLineToBezierPathElement:
ap[0].x += dx;
ap[0].y += dy;
[newPath lineToPoint:ap[0]];
lp = ap[0];
break;
case NSCurveToBezierPathElement:
{
ap[0].x += pdx;
ap[0].y += pdy;
ap[1].x += dx;
ap[1].y += dy;
ap[2].x += dx;
ap[2].y += dy;
[newPath curveToPoint:ap[2] controlPoint1:ap[0] controlPoint2:ap[1]];
lp = ap[2];
}
break;
case NSClosePathBezierPathElement:
[newPath closePath];
lp = fp;
break;
default:
break;
}
pdx = dx;
pdy = dy;
}
}
return newPath;
}
- (NSBezierPath*) offsetPathWithStartingOffset2:(float) delta1 endingOffset:(float) delta2
{
// When GPC is disabled, works exactly as above.
NSBezierPath* temp = self;
temp = [temp offsetPathWithStartingOffset:delta1 endingOffset:delta2];
return temp;
}
- (NSBezierPath*) bezierPathByInterpolatingPath:(float) amount
{
// smooths a vector (line segment) path by interpolation into curve segments. This algorithm from http://antigrain.com/research/bezier_interpolation/index.html#PAGE_BEZIER_INTERPOLATION
// existing curve segments are reinterpolated as if a straight line joined the start and end points. Note this doesn't simplify a curve - it merely smooths it using the same number
// of curve segments. <amount> is a value from 0..1 that yields the amount of smoothing, 0 = none.
amount = LIMIT( amount, 0, 1 );
if( amount == 0.0 || [self isEmpty])
return self; // nothing to do
NSBezierPath* newPath = [NSBezierPath bezierPath];
int i, m = [self elementCount], spc = 0;
NSBezierPathElement element;
NSPoint ap[3];
NSPoint v[3];
NSPoint fp, cp1, cp2, pcp;
fp = cp1 = cp2 = NSZeroPoint;
v[0] = v[1] = v[2] = NSZeroPoint;
for( i = 0; i < m; ++i )
{
element = [self elementAtIndex:i associatedPoints:ap];
switch( element )
{
case NSMoveToBezierPathElement:
// starting a new subpath
[newPath moveToPoint:ap[0]];
fp = v[0] = ap[0];
spc = 0;
break;
case NSLineToBezierPathElement:
if( spc == 0 )
{
// recently started a new subpath, so set 2nd vertex
v[1] = ap[0];
spc++;
}
else
{
v[2] = ap[0];
// we have three vertices, so we can interpolate
InterpolatePoints( v, &cp1, &cp2, amount );
// cp2 completes the curve segment v0..v1 so we can add that to the new path. If it was the first
// segment, cp1 == cp2
if( spc == 1 )
pcp = cp2;
[newPath curveToPoint:v[1] controlPoint1:pcp controlPoint2:cp2];
// shift vertex array
v[0] = v[1];
v[1] = v[2];
pcp = cp1;
spc++;
}
break;
case NSCurveToBezierPathElement:
if( spc == 0 )
{
// recently started a new subpath, so set 2nd vertex
v[1] = ap[2];
spc++;
}
else
{
v[2] = ap[2];
// we have three vertices, so we can interpolate
InterpolatePoints( v, &cp1, &cp2, amount );
// cp2 completes the curve segment v0..v1 so we can add that to the new path. If it was the first
// segment, cp1 == cp2
if( spc == 1 )
pcp = cp2;
[newPath curveToPoint:v[1] controlPoint1:pcp controlPoint2:cp2];
// shift vertex array
v[0] = v[1];
v[1] = v[2];
pcp = cp1;
spc++;
}
break;
case NSClosePathBezierPathElement:
// close the path by curving back to the first point
v[2] = fp;
InterpolatePoints( v, &cp1, &cp2, amount );
// cp2 completes the curve segment v0..v1 so we can add that to the new path. If it was the first
// segment, cp1 == cp2
if( spc == 1 )
pcp = cp2;
[newPath curveToPoint:v[1] controlPoint1:pcp controlPoint2:cp2];
// final segment closes the path
[newPath curveToPoint:fp controlPoint1:cp1 controlPoint2:cp1];
[newPath closePath];
spc = 0;
break;
default:
break;
}
}
if( spc > 1 )
{
// path ended without a closepath, so add in the final curve segment to the end
[newPath curveToPoint:v[1] controlPoint1:pcp controlPoint2:pcp];
}
//NSLog(@"new path = %@", newPath);
return newPath;
}
static void InterpolatePoints( const NSPoint* v, NSPoint* cp1, NSPoint* cp2, const float smooth_value )
{
// given the vertices of the path v0..v2, this calculates cp1 and cp2 being the control points for the curve segments v0..v1 and v1..v2. i.e. this
// calculates only half of the control points, but does so for two segments. The caller needs to accumulate cp1 until it has cp2 for the same segment