Skip to content
This repository has been archived by the owner on Aug 24, 2019. It is now read-only.

soffes/SAMCubicSpline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SAMCubicSpline

I found this math somewhere and tweaked it some to work the way I needed it. A cubic spline is great for drawing the curve in a tone curve filter.

Installation

Simply add the following to your Podfile:

pod 'SAMCubicSpline'

If you're not using CocoaPods, simply add the source files in the SAMCubicSpline directory to your project. There are no dependencies and it works on Mac and iOS.

Example

A quick sample to draw a spline into an already setup CGContextRef and assumes you'll finish drawing the line at the end.

#import "SAMCubicSpline.h"

// Setup a spline with some control points
#if TARGET_OS_IOS
SAMCubicSpline *spline = [[SAMCubicSpline alloc] initWithPoints:@[
  [NSValue valueWithCGPoint:CGPointMake(0.0f, 0.039f)],
  [NSValue valueWithCGPoint:CGPointMake(0.588f, 0.525f)],
  [NSValue valueWithCGPoint:CGPointMake(1.0f, 1.0f)],
]];
#else
SAMCubicSpline *spline = [[SAMCubicSpline alloc] initWithPoints:@[
  [NSValue valueWithPoint:CGPointMake(0.0f, 0.039f)],
  [NSValue valueWithPoint:CGPointMake(0.588f, 0.525f)],
  [NSValue valueWithPoint:CGPointMake(1.0f, 1.0f)],
]];
#endif

// Iterate over the X values in the area we want to draw
CGSize graphSize = CGSizeMake(100.0f, 100.0f);
for (CGFloat x = 0.0f; x < size.width; x++) {
  // Get the Y value of our point
  CGFloat y = [spline interpolate:x / size.width] * size.height;

  // Add the point to the context's path
  if (x == 0.0f) {
    CGContextMoveToPoint(context, x, y);
  } else {
    CGContextAddLineToPoint(context, x, y);
  }
}

Thanks

This was abstracted from Footage. Thanks to Drew Wilson for allowing me to open source this!