-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNSBezierPath+Schneider_FitCurves.m
63 lines (55 loc) · 2.12 KB
/
NSBezierPath+Schneider_FitCurves.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
//
// NSBezierPath+Schneider_FitCurves.m
//
// Uses the FitCurve-Algorithm by Philip J. Schneider
// http://tog.acm.org/resources/GraphicsGems/gems/FitCurves.c
//
// Created by Tobias Conradi on 29.04.11.
// Copyright 2011 Tobias Conradi. All rights reserved.
// https://github.com/toco/NSBezierPath-Additions
// http://tobias-conradi.de/index.php/2011/05/06/nsbezierpath-additions
#import "NSBezierPath+Schneider_FitCurves.h"
#import "GraphicsGems.h"
@implementation NSBezierPath (NSBezierPath_Schneider_FitCurves)
/*
performs the FitCurve function by Philip J. Schneider on the NSBezierPath
does NOT work for NSBezierPaths containing NSCurvesToBezierPathElements
*/
- (int) schneiderFitCurves:(double) error {
// number of elements
int count = [self elementCount];
// allocating C-style array of CGPoints building the path
CGPoint *pathPointArray = malloc(sizeof(CGPoint)*count);
// allocating C-style array of CGPoints to get the points of the single elements
CGPoint *aPointArray = malloc(sizeof(CGPoint)*3);
//iterating through the path
for (int i=0;i<count;i++) {
//get elementType and associated point for the element
NSBezierPathElement type = [self elementAtIndex:i associatedPoints:aPointArray];
//if a element is a NSCurveToBezierPathElement the algorythm does not work
//returning without doing any changes to the Path
if (type==NSCurveToBezierPathElement) {
NSLog(@"Schneinder_FitCurves: Error: Path contains CurveElement");
free(aPointArray);
free(pathPointArray);
return 1;
}
//adding point of current element to the pointArray
pathPointArray[i] = aPointArray[0];
}
// free the temp pointArray
free(aPointArray);
//remove all points the curveelements will be added to this curve
[self removeAllPoints];
// move Path to the startPoint
[self moveToPoint:pathPointArray[0]];
/*
Start Algorythm
*/
FitCurve(pathPointArray, count, 4.0,self);
//free pointArray
free(pathPointArray);
//no error return 0
return 0;
}
@end