|
| 1 | +// |
| 2 | +// CatmullRomSpline.m |
| 3 | +// Curve |
| 4 | +// |
| 5 | +// Created by Bryan Spitz on 10-01-28. |
| 6 | +// Copyright 2010 __MyCompanyName__. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "CatmullRomSpline.h" |
| 10 | +#import "CGPointArithmetic.h" |
| 11 | + |
| 12 | + |
| 13 | +@implementation CatmullRomSpline |
| 14 | + |
| 15 | + |
| 16 | ++(CatmullRomSpline *)catmullRomSplineAtPoint:(CGPoint)start { |
| 17 | + return [[[CatmullRomSpline alloc] initAtPoint:start] autorelease]; |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +-(id)initAtPoint:(CGPoint)start { |
| 22 | + if (self = [super initAtPoint:start]) { |
| 23 | + p = start; |
| 24 | + p1 = start; |
| 25 | + p2 = start; |
| 26 | + p3 = start; |
| 27 | + } |
| 28 | + |
| 29 | + return self; |
| 30 | +} |
| 31 | + |
| 32 | +-(void)addPoint:(CGPoint)point { |
| 33 | + CGPoint diff = CGPointMake(point.x - p.x, point.y - p.y); |
| 34 | + double length = sqrt(pow(diff.x, 2) + pow(diff.y, 2)); |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + if ([curves count] > 0) { |
| 39 | + [self removeLastCurve]; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + if (length >= 15) { |
| 44 | + p3 = p2; |
| 45 | + p2 = p1; |
| 46 | + p1 = p; |
| 47 | + p = point; |
| 48 | + |
| 49 | + CGPoint tangent = CGPointMake((p1.x - p3.x), (p1.y - p3.y)); |
| 50 | + CGFloat tangentLength = CGPointMagnitude(tangent); |
| 51 | + CGPoint unitTangent = (tangentLength == 0.)?tangent:CGPointScale(tangent, 1. / tangentLength); |
| 52 | + CGPoint diff = CGPointDifference (p1, p2); |
| 53 | + CGFloat desiredLength = CGPointMagnitude(diff) / 3.; |
| 54 | + CGPoint desiredTangent = CGPointScale(unitTangent, desiredLength); |
| 55 | + |
| 56 | + CGPoint ctrl1 = CGPointMake(p2.x + desiredTangent.x, p2.y + desiredTangent.y); |
| 57 | + |
| 58 | + tangent = CGPointMake((p.x - p2.x), (p.y - p2.y)); |
| 59 | + tangentLength = CGPointMagnitude(tangent); |
| 60 | + unitTangent = (tangentLength == 0.)?tangent:CGPointScale(tangent, 1. / tangentLength); |
| 61 | + desiredTangent = CGPointScale(unitTangent, desiredLength); |
| 62 | + |
| 63 | + CGPoint ctrl2 = CGPointMake(p1.x - desiredTangent.x, p1.y - desiredTangent.y); |
| 64 | + |
| 65 | + [self addCubicCurveWithControl1:ctrl1 control2:ctrl2 toPoint:p1]; |
| 66 | + |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + CGPoint currtemp = current; |
| 71 | + CGPoint tangent2 = CGPointMake((p.x - p2.x)/5., (p.y - p2.y)/5.); |
| 72 | + CGPoint ctrl = CGPointMake(p1.x + tangent2.x, p1.y + tangent2.y); |
| 73 | + [self addQuadCurveWithControl:ctrl toPoint:point]; |
| 74 | + current = currtemp; |
| 75 | + |
| 76 | + |
| 77 | +} |
| 78 | +@end |
0 commit comments