Sunday, December 7, 2008

6. B-Spline Test Implementation

And, finally, I have been able to implement a first B-Spline interpolation test.



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);
}