forked from dev2dev/BezierBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSBezierPathCodeBuilder.m
55 lines (45 loc) · 1.79 KB
/
NSBezierPathCodeBuilder.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
//
// NSBezierPathCodeBuilder.m
// BezierBuilder
//
// Created by Dave DeLong on 2/1/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "NSBezierPathCodeBuilder.h"
#import "BezierPoint.h"
@implementation NSBezierPathCodeBuilder
- (NSString *) codeForBezierPoints {
NSArray *points = [self effectiveBezierPoints];
NSMutableArray *lines = [NSMutableArray array];
[lines addObject:@"NSBezierPath *bp = [[NSBezierPath alloc] init];"];
for (NSUInteger i = 0; i < [points count]; ++i) {
BezierPoint *point = [points objectAtIndex:i];
if (i == 0) {
[lines addObject:[NSString stringWithFormat:@"[bp moveToPoint:NSMakePoint(%0.2f, %0.2f)]", [point mainPoint].x, [point mainPoint].y]];
} else {
[lines addObject:[NSString stringWithFormat:@"[bp curveToPoint:NSMakePoint(%0.2f, %0.2f) controlPoint1:NSMakePoint(%0.2f, %0.2f) controlPoint2:NSMakePoint(%0.2f, %0.2f)];",
[point mainPoint].x, [point mainPoint].y,
[point controlPoint1].x, [point controlPoint1].y,
[point controlPoint2].x, [point controlPoint2].y]];
}
}
[lines addObject:@"[bp stroke];"];
[lines addObject:@"[bp release];"];
return [lines componentsJoinedByString:@"\n"];
}
- (id) objectForBezierPoints {
NSArray *points = [self effectiveBezierPoints];
NSBezierPath *bp = [[NSBezierPath alloc] init];
for (NSUInteger i = 0; i < [points count]; ++i) {
BezierPoint *point = [points objectAtIndex:i];
if (i == 0) {
[bp moveToPoint:NSMakePoint([point mainPoint].x, [point mainPoint].y)];
} else {
[bp curveToPoint:NSMakePoint([point mainPoint].x, [point mainPoint].y)
controlPoint1:NSMakePoint([point controlPoint1].x, [point controlPoint1].y)
controlPoint2:NSMakePoint([point controlPoint2].x, [point controlPoint2].y)];
}
}
return [bp autorelease];
}
@end