This little test application loads a custom NSView subclass, where it draws a control points polyline (the blue line) and, subsequently, calculates intermediate points, using a B-Spline interpolation algorithm to draw the red curved line.
Following code shows the two methods that are actually responsible for spline interpolation.
- (CGFloat) bWithI:(int)i T:(float)t {
switch (i) {
case -2:
return (((-t + 3) * t - 3) * t + 1) / 6;
case -1:
return (((3 * t - 6) * t) * t + 4) / 6;
case 0:
return (((-3 * t + 3) * t + 3) * t + 1) / 6;
case 1:
return (t * t * t) / 6;
}
return 0;
}
- (NSPoint) pWithI:(int)i T:(float)t {
CGFloat px = 0;
CGFloat py = 0;
for (int j = -2; j <= 1; j++) {
px += [self bWithI:j T:t] * [[[self points] objectAtIndex:i + j] pointValue].x;
py += [self bWithI:j T:t] * [[[self points] objectAtIndex:i + j] pointValue].y;
}
return NSMakePoint(px, py);
}
switch (i) {
case -2:
return (((-t + 3) * t - 3) * t + 1) / 6;
case -1:
return (((3 * t - 6) * t) * t + 4) / 6;
case 0:
return (((-3 * t + 3) * t + 3) * t + 1) / 6;
case 1:
return (t * t * t) / 6;
}
return 0;
}
- (NSPoint) pWithI:(int)i T:(float)t {
CGFloat px = 0;
CGFloat py = 0;
for (int j = -2; j <= 1; j++) {
px += [self bWithI:j T:t] * [[[self points] objectAtIndex:i + j] pointValue].x;
py += [self bWithI:j T:t] * [[[self points] objectAtIndex:i + j] pointValue].y;
}
return NSMakePoint(px, py);
}
3 comments:
Hello,
I was wondering if i could have a copy of your xcode project?
I'd like to implement an adaptive sampling algorithm to convert bezier curve drawings from illustrator to polygons and the work you've done would be a great help getting going. Of course I'll happily send you back a copy of any tool i make using your work.
cheers
mathew
compoundeye at gmail
Thanks, this is really helpful!!
I am glad you found it userful, Geoff.
Post a Comment