Skip to content

Instantly share code, notes, and snippets.

@brunoro
Created August 20, 2015 15:01
Show Gist options
  • Save brunoro/ec3904230f53dd6118fa to your computer and use it in GitHub Desktop.
Save brunoro/ec3904230f53dd6118fa to your computer and use it in GitHub Desktop.
Visualize Bézier curves in cocos2d
#import "cocos2d.h"
@interface BezierCurveNode : CCNode
@property (nonatomic) CGPoint origin;
@property (nonatomic) CGPoint cp1;
@property (nonatomic) CGPoint cp2;
@property (nonatomic) CGPoint destination;
@property (nonatomic) ccColor3B color;
- (id)initWithOrigin:(CGPoint)origin
controlPoint1:(CGPoint)cp1
controlPoint2:(CGPoint)cp2
destination:(CGPoint)destination
color:(ccColor3B)color;
- (id)initWithOrigin:(CGPoint)origin config:(ccBezierConfig)config;
@end
#import "BezierCurveNode.h"
#import "cocos2d.h"
@implementation BezierCurveNode
- (id)initWithOrigin:(CGPoint)origin
controlPoint1:(CGPoint)cp1
controlPoint2:(CGPoint)cp2
destination:(CGPoint)destination
color:(ccColor3B)color
{
self = [super init];
if (self)
{
_origin = origin;
_cp1 = cp1;
_cp2 = cp2;
_destination = destination;
_color = color;
}
return self;
}
- (id)initWithOrigin:(CGPoint)origin config:(ccBezierConfig)config
{
return [self initWithOrigin:origin
controlPoint1:config.controlPoint_1
controlPoint2:config.controlPoint_2
destination:config.endPosition
color:ccc3(120, 0, 120)];
}
- (void)visit
{
[super visit];
ccPointSize(10);
ccDrawColor4F(_color.r / 255.0, _color.g / 255.0, _color.b / 255.0, 1.0f);
ccDrawCircle(self.cp1, 5, 0, 20, NO);
ccDrawCircle(self.cp2, 8, 0, 20, NO);
ccDrawCubicBezier(self.origin, self.cp1, self.cp2, self.destination, 20);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment