Curve of Bézier out of C

This code makes it possible to trace a Courbe of Bézier defined by 4 check-points. It is thus a cubic curve. This algorithm is an implementation of the Algorithme of Casteljau.

To adapt this code C to your system, it is enough for you to replace the procedure of layout of segment (here in OpenGL).

The structure Global_Bezier contains the 4 check-points of the curve of Bézier to be traced. level contains the number of recursive call. typedef struct { float X, there; } not;

typedef struct { not P; } Bezier;

/* Aggregate variable * Bezier Global_Bezier; int level=1;

// Draw has line with color (R, v, b) void DrawLine (int x1, int y1, int x2, int y2, float R, float v, float b) { glColor3f (R, v, b); glBegin (GL_LINES); glVertex2i (x1, y1); glVertex2i (x2, y2); glEnd (); glFlush (); }

void DrawBezierBase (Bezier p, float R, float v, float b) { DrawLine (p.P.x, p.P.y, p.P.x, p.P.y, R, v, b); DrawLine (p.P.x, p.P.y, p.P.x, p.P.y, R, v, b); DrawLine (p.P.x, p.P.y, p.P.x, p.P.y, R, v, b); }

void DrawBezierRecursive (Bezier B, int level) { yew (level <= 0) { /* Draw has line segment * DrawLine ((int) (b.P.x + 0.5), (int) (b.P.y + 0.5), (int) (b.P.x + 0.5), (int) (b.P.y + 0.5), 1,1,1); } else { Bezier left, right; /* subdivide into 2 Bezier segments * left. P.x = b.P.x; left. P.y = b.P.y; left. P.x = (b.P.x + b.P.x)/2; left. P.y = (b.P.y + b.P.y)/2; left. P.x = (b.P.x + 2*b.P.x + b.P.x)/4; left. P.y = (b.P.y + 2*b.P.y + b.P.y)/4; left. P.x = (b.P.x + 3*b.P.x + 3*b.P.x + b.P.x)/8; left. P.y = (b.P.y + 3*b.P.y + 3*b.P.y + b.P.y)/8; DrawBezierBase (left, 0,1,0); right. P.x = left. P.x; right. P.y = left. P.y; right. P.x = (b.P.x + 2*b.P.x + b.P.x)/4; right. P.y = (b.P.y + 2*b.P.y + b.P.y)/4; right. P.x = (b.P.x + b.P.x)/2; right. P.y = (b.P.y + b.P.y)/2; right. P.x = b.P.x; right. P.y = b.P.y; DrawBezierBase (right, 0,0,1); /* Draw the 2 segments recursively * DrawBezierRecursive (left, level -1); DrawBezierRecursive (right, level -1); } }

void DrawBezier () { DrawBezierBase (Global_Bezier, 1,0,0); DrawBezierRecursive (Global_Bezier, level); }

Random links:Lahontan | Chloropidae | List highways of Sweden | Gorkhi-Terelj national park | Eugene Gabriel Mansuelle | Acritarch