Last active
December 14, 2015 07:49
-
-
Save commanda/5053654 to your computer and use it in GitHub Desktop.
Create a proportional S-shaped bezier curve between any two points. Objective-C, Cocos2d, iOS.
This file contains hidden or 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
-(ccBezierConfig)bezierForStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint curviness:(CGFloat)alpha | |
{ | |
CGPoint basis1 = ccpLerp(startPoint, endPoint, 0.3333); | |
CGPoint basis2 = ccpLerp(startPoint, endPoint, 0.6666); | |
// Get a point on the perpendicular line of each of the control points | |
CGPoint vector = ccpSub(endPoint, startPoint); | |
// First point is to the right, second is to the left, of the line between the start point and end point | |
CGPoint v1 = ccpRPerp(vector); | |
CGPoint v2 = ccpPerp(vector); | |
// Translate our two perpendicular vectors over onto our chosen basis points | |
CGPoint s1 = ccpAdd(basis1, v1); | |
CGPoint s2 = ccpAdd(basis2, v2); | |
// Apply the alpha to control curviness | |
CGPoint l1 = ccpLerp(basis1, s1, alpha); | |
CGPoint l2 = ccpLerp(basis2, s2, alpha); | |
ccBezierConfig bezier = (ccBezierConfig){ | |
endPoint, // end point | |
l1, // control point 1 | |
l2 // control point 2 | |
}; | |
return bezier; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment