forked from aryansbtloe/ExperimentWithTesseract
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alok
committed
May 16, 2013
0 parents
commit 1247cc6
Showing
158 changed files
with
400,847 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// BezierCurve.h | ||
// Curve | ||
// | ||
// BezierCurve is an abstract class representing a Bezier curve of any degree. It should not be instantiated | ||
// directly. Use LinearBezierCurve, QuadraticBezierCurve, and CubicBezierCurve instead. | ||
// | ||
// Created by Bryan Spitz on 10-01-26. | ||
// Copyright 2010 Bryan Spitz. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
|
||
@interface BezierCurve : NSObject { | ||
CGPoint p1, p2; | ||
} | ||
|
||
// Start point | ||
@property (nonatomic, readonly) CGPoint p1; | ||
// End point | ||
@property (nonatomic, readonly) CGPoint p2; | ||
|
||
// An array of two Bezier curves of the same degree as the original. | ||
// These two curves, placed together, encompass exactly the same points | ||
// as the original. | ||
-(NSArray *)subdivided; | ||
|
||
// Returns whether the curve may be treated as linear for drawing or other purposes. | ||
-(BOOL)isNearLinear; | ||
|
||
// Returns an array of NSValues representing CGPoints dividing the curve into near-linear subsections. | ||
-(NSArray *)asPointArray; | ||
|
||
// The same as asPointArray, but adds points to an existing array for efficiency. | ||
-(void)addToPointArray:(NSMutableArray *)pointArray; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// BezierCurve.m | ||
// Curve | ||
// | ||
// Created by Bryan Spitz on 10-01-26. | ||
// Copyright 2010 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import "BezierCurve.h" | ||
|
||
|
||
@implementation BezierCurve | ||
@synthesize p1, p2; | ||
|
||
- (NSArray *)subdivided { | ||
[self doesNotRecognizeSelector:_cmd]; | ||
return nil; | ||
} | ||
|
||
- (BOOL)isNearLinear { | ||
[self doesNotRecognizeSelector:_cmd]; | ||
return NO; | ||
} | ||
|
||
- (NSArray *)asPointArray { | ||
[self doesNotRecognizeSelector:_cmd]; | ||
return nil; | ||
} | ||
|
||
- (void)addToPointArray:(NSMutableArray *)pointArray { | ||
[self doesNotRecognizeSelector:_cmd]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* CGPointArithmetic.h | ||
* Curve | ||
* | ||
* A set of tools for doing CGPoint calculations. For efficiency, these are preprocessor macros | ||
* instead of C functions. | ||
* | ||
* Created by Bryan Spitz on 10-01-26. | ||
* Copyright 2010 Bryan Spitz. All rights reserved. | ||
* | ||
*/ | ||
|
||
#import <math.h> | ||
|
||
#define CGPointDifference(p1,p2) (CGPointMake(((p1.x) - (p2.x)), ((p1.y) - (p2.y)))) | ||
#define CGPointMagnitude(p) sqrt(p.x*p.x + p.y*p.y) | ||
#define CGPointSlope(p) (p.y / p.x) | ||
#define CGPointScale(p, d) CGPointMake(p.x * d, p.y * d) | ||
#define CGPointAdd(p1, p2) CGPointMake(p1.x + p2.x, p1.y + p2.y) | ||
#define CGPointMidpoint(p1, p2) CGPointMake((p1.x + p2.x)/2., (p1.y + p2.y)/2.) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// CatmullRomSpline.h | ||
// Curve | ||
// | ||
// CatmullRomSpline is a class representing a Catmull-Rom Spline (i.e. a spline with | ||
// continuous derivative, passing through a set of arbitrary control points). The tangent | ||
// of the spline at any control point (except the first and last) is parallel to the | ||
// line connecting the previous control point with the next one. | ||
// | ||
// Most of the segments of a CatmullRomSpline are cubic bezier curves. The last segment is | ||
// a quadratic curve. When a new point is added, the last segment is removed and replaced with a | ||
// cubic curve making use of the new control point information, and a new quadratic curve is | ||
// added to the end. Application that attempt to cache data related to the spline should be | ||
// aware that the final points are subject to change. | ||
// | ||
// Created by Bryan Spitz on 10-01-28. | ||
// Copyright 2010 Bryan Spitz. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "Spline.h" | ||
|
||
@interface CatmullRomSpline : Spline { | ||
CGPoint p3, p2, p1, p; | ||
} | ||
|
||
+(CatmullRomSpline *)catmullRomSplineAtPoint:(CGPoint)start; | ||
|
||
// Add a control point, through which the spline must pass, to the end of the spline. | ||
-(void)addPoint:(CGPoint)point; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// CatmullRomSpline.m | ||
// Curve | ||
// | ||
// Created by Bryan Spitz on 10-01-28. | ||
// Copyright 2010 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import "CatmullRomSpline.h" | ||
#import "CGPointArithmetic.h" | ||
|
||
|
||
@implementation CatmullRomSpline | ||
|
||
|
||
+(CatmullRomSpline *)catmullRomSplineAtPoint:(CGPoint)start { | ||
return [[[CatmullRomSpline alloc] initAtPoint:start] autorelease]; | ||
} | ||
|
||
|
||
-(id)initAtPoint:(CGPoint)start { | ||
if (self = [super initAtPoint:start]) { | ||
p = start; | ||
p1 = start; | ||
p2 = start; | ||
p3 = start; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
-(void)addPoint:(CGPoint)point { | ||
CGPoint diff = CGPointMake(point.x - p.x, point.y - p.y); | ||
double length = sqrt(pow(diff.x, 2) + pow(diff.y, 2)); | ||
|
||
|
||
|
||
if ([curves count] > 0) { | ||
[self removeLastCurve]; | ||
} | ||
|
||
|
||
if (length >= 15) { | ||
p3 = p2; | ||
p2 = p1; | ||
p1 = p; | ||
p = point; | ||
|
||
CGPoint tangent = CGPointMake((p1.x - p3.x), (p1.y - p3.y)); | ||
CGFloat tangentLength = CGPointMagnitude(tangent); | ||
CGPoint unitTangent = (tangentLength == 0.)?tangent:CGPointScale(tangent, 1. / tangentLength); | ||
CGPoint diff = CGPointDifference (p1, p2); | ||
CGFloat desiredLength = CGPointMagnitude(diff) / 3.; | ||
CGPoint desiredTangent = CGPointScale(unitTangent, desiredLength); | ||
|
||
CGPoint ctrl1 = CGPointMake(p2.x + desiredTangent.x, p2.y + desiredTangent.y); | ||
|
||
tangent = CGPointMake((p.x - p2.x), (p.y - p2.y)); | ||
tangentLength = CGPointMagnitude(tangent); | ||
unitTangent = (tangentLength == 0.)?tangent:CGPointScale(tangent, 1. / tangentLength); | ||
desiredTangent = CGPointScale(unitTangent, desiredLength); | ||
|
||
CGPoint ctrl2 = CGPointMake(p1.x - desiredTangent.x, p1.y - desiredTangent.y); | ||
|
||
[self addCubicCurveWithControl1:ctrl1 control2:ctrl2 toPoint:p1]; | ||
|
||
|
||
} | ||
|
||
CGPoint currtemp = current; | ||
CGPoint tangent2 = CGPointMake((p.x - p2.x)/5., (p.y - p2.y)/5.); | ||
CGPoint ctrl = CGPointMake(p1.x + tangent2.x, p1.y + tangent2.y); | ||
[self addQuadCurveWithControl:ctrl toPoint:point]; | ||
current = currtemp; | ||
|
||
|
||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// CubicBezierCurve.h | ||
// Curve | ||
// | ||
// Created by Bryan Spitz on 10-01-28. | ||
// Copyright 2010 Bryan Spitz. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "BezierCurve.h" | ||
|
||
@interface CubicBezierCurve : BezierCurve { | ||
CGPoint ctrl1, ctrl2; | ||
} | ||
|
||
+(CubicBezierCurve *)cubicCurveWithStart:(CGPoint)start controlPoint1:(CGPoint)control1 controlPoint2:(CGPoint)control2 end:(CGPoint)end; | ||
-(id)initWithStart:(CGPoint)start controlPoint1:(CGPoint)control1 controlPoint2:(CGPoint)control2 end:(CGPoint)end; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// | ||
// CubicBezierCurve.m | ||
// Curve | ||
// | ||
// Created by Bryan Spitz on 10-01-28. | ||
// Copyright 2010 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import "CubicBezierCurve.h" | ||
#import "CGPointArithmetic.h" | ||
|
||
|
||
@implementation CubicBezierCurve | ||
|
||
+(CubicBezierCurve *)cubicCurveWithStart:(CGPoint)start controlPoint1:(CGPoint)control1 controlPoint2:(CGPoint)control2 end:(CGPoint)end { | ||
return [[[CubicBezierCurve alloc] initWithStart:start | ||
controlPoint1:control1 | ||
controlPoint2:control2 | ||
end:end] autorelease]; | ||
} | ||
|
||
-(id)initWithStart:(CGPoint)start controlPoint1:(CGPoint)control1 controlPoint2:(CGPoint)control2 end:(CGPoint)end { | ||
if (self = [super init]) { | ||
p1 = start; | ||
p2 = end; | ||
ctrl1 = control1; | ||
ctrl2 = control2; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
-(BOOL)isNearLinear { | ||
|
||
if (CGPointEqualToPoint(p1, p2)) { | ||
return YES; | ||
} | ||
|
||
CGPoint p1Ctrl1 = CGPointDifference(ctrl1, p1); | ||
CGPoint p1p2 = CGPointDifference(p2, p1); | ||
|
||
CGFloat p1p2length = CGPointMagnitude(p1p2); | ||
CGFloat projectionLength = (p1Ctrl1.x * p1p2.x + p1Ctrl1.y * p1p2.y) / (p1p2length * p1p2length); | ||
|
||
CGPoint projectedPt = CGPointScale(p1p2, projectionLength); | ||
|
||
CGPoint diff = CGPointDifference(p1Ctrl1, projectedPt); | ||
CGFloat distance = CGPointMagnitude(diff); | ||
|
||
CGPoint p2Ctrl2 = CGPointDifference(ctrl2, p2); | ||
projectionLength = (p2Ctrl2.x * -p1p2.x + p2Ctrl2.y * -p1p2.y) / (p1p2length * p1p2length); | ||
projectedPt = CGPointScale(p1p2, -projectionLength); | ||
diff = CGPointDifference(p2Ctrl2, projectedPt); | ||
CGFloat distance2 = CGPointMagnitude(diff); | ||
|
||
return distance < 0.5 && distance2 < 0.5; | ||
|
||
} | ||
|
||
-(NSArray *)subdivided { | ||
CGPoint midStartCtrl1 = CGPointMidpoint(p1, ctrl1); | ||
CGPoint midCtrl1Ctrl2 = CGPointMidpoint(ctrl1, ctrl2); | ||
CGPoint midCtrl2End = CGPointMidpoint(ctrl2, p2); | ||
CGPoint newCtrl1 = CGPointMidpoint(midStartCtrl1, midCtrl1Ctrl2); | ||
CGPoint newCtrl2 = CGPointMidpoint(midCtrl1Ctrl2, midCtrl2End); | ||
CGPoint mid = CGPointMidpoint(newCtrl1, newCtrl2); | ||
|
||
CubicBezierCurve *curve1 = [[CubicBezierCurve alloc] initWithStart:p1 | ||
controlPoint1:midStartCtrl1 | ||
controlPoint2:newCtrl1 | ||
end:mid]; | ||
CubicBezierCurve *curve2 = [[CubicBezierCurve alloc] initWithStart:mid | ||
controlPoint1:newCtrl2 | ||
controlPoint2:midCtrl2End | ||
end:p2]; | ||
|
||
NSArray *result = [NSArray arrayWithObjects:curve1, curve2, nil]; | ||
|
||
[curve1 release]; | ||
[curve2 release]; | ||
|
||
return result; | ||
} | ||
|
||
-(NSArray *)asPointArray { | ||
/* | ||
if (pointArray != nil) { | ||
return pointArray; | ||
} | ||
*/ | ||
if ([self isNearLinear]) { | ||
return [NSArray arrayWithObjects:[NSValue valueWithCGPoint:p1], [NSValue valueWithCGPoint:p2], nil]; | ||
} else { | ||
NSArray *div = [self subdivided]; | ||
NSMutableArray *pointArray = [[NSMutableArray alloc] initWithArray:[[div objectAtIndex:0] asPointArray]]; | ||
[pointArray autorelease]; | ||
[pointArray removeLastObject]; | ||
[pointArray addObjectsFromArray:[[div objectAtIndex:1] asPointArray]]; | ||
|
||
return pointArray; | ||
} | ||
|
||
} | ||
|
||
-(void)addToPointArray:(NSMutableArray *)pointArray { | ||
if ([self isNearLinear]) { | ||
[pointArray addObject:[NSValue valueWithCGPoint:p1]]; | ||
} else { | ||
NSArray *div = [self subdivided]; | ||
[[div objectAtIndex:0] addToPointArray:pointArray]; | ||
[[div objectAtIndex:1] addToPointArray:pointArray]; | ||
} | ||
|
||
} | ||
|
||
|
||
-(void)dealloc { | ||
//[pointArray release]; | ||
[super dealloc]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// LinearBezierCurve.h | ||
// Curve | ||
// | ||
// Created by Bryan Spitz on 10-01-26. | ||
// Copyright 2010 Bryan Spitz. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "BezierCurve.h" | ||
|
||
@interface LinearBezierCurve : BezierCurve { | ||
} | ||
|
||
+(LinearBezierCurve *)linearCurveWithStartPoint:(CGPoint)start endPoint:(CGPoint)end; | ||
-(id)initWithStartPoint:(CGPoint)start endPoint:(CGPoint)end; | ||
|
||
@end |
Oops, something went wrong.