BIT-101 [2003-2017]

Bezier Curve functions for cocos2d


I needed to draw a curve tonight in cocos2d. The Primitives file has a few basic OpenGL based drawing methods, but no curves. So I created these two methods:

[c]
#import <openGLES/ES1/gl.h>
#import <math.h>
#import <stdlib.h>
#import <string.h>

void drawQuadBezier(CGPoint origin, CGPoint control, CGPoint destination, int segments)
{
CGPoint vertices[segments + 1];

float t = 0.0;
for(int i = 0; i < segments; i++) { float x = pow(1 - t, 2) * origin.x + 2.0 * (1 - t) * t * control.x + t * t * destination.x; float y = pow(1 - t, 2) * origin.y + 2.0 * (1 - t) * t * control.y + t * t * destination.y; vertices[i] = CGPointMake(x, y); t += 1.0 / segments; } vertices[segments] = destination; glVertexPointer(2, GL_FLOAT, 0, vertices); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_LINE_STRIP, 0, segments); glDisableClientState(GL_VERTEX_ARRAY); } void drawCubicBezier(CGPoint origin, CGPoint control1, CGPoint control2, CGPoint destination, int segments) { CGPoint vertices[segments + 1]; float t = 0.0; for(int i = 0; i < segments; i++) { float x = pow(1 - t, 3) * origin.x + 3.0 * pow(1 - t, 2) * t * control1.x + 3.0 * (1 - t) * t * t * control2.x + t * t * t * destination.x; float y = pow(1 - t, 3) * origin.y + 3.0 * pow(1 - t, 2) * t * control1.y + 3.0 * (1 - t) * t * t * control2.y + t * t * t * destination.y; vertices[i] = CGPointMake(x, y); t += 1.0 / segments; } vertices[segments] = destination; glVertexPointer(2, GL_FLOAT, 0, vertices); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_LINE_STRIP, 0, segments); glDisableClientState(GL_VERTEX_ARRAY); }[/c] Been trying to post these on the mailing list, but keep getting rejected with the message that this list contains spam or malware. Anyone know what’s up with that?

« Previous Post
Next Post »