www.pudn.com > CG2Programs.rar > bezier.c
/* bezier, Chapter 10, p. 329 */ /* EXAMPLE STARTS HERE */ #include#include "graphics.h" void computeCoefficients (int n, int * c) { int k, i; for (k=0; k<=n; k++) { /* Compute n!/(k!(n-k)!) */ c[k] = 1; for (i=n; i>=k+1; i--) c[k] *= i; for (i=n-k; i>=2; i--) c[k] /= i; } } void computePoint (float u, wcPt3 * pt, int nControls, wcPt3 * controls, int * c) { int k, n = nControls - 1; float blend; pt->x = 0.0; pt->y = 0.0; pt->z = 0.0; /* Add in influence of each control point */ for (k=0; k x += controls[k].x * blend; pt->y += controls[k].y * blend; pt->z += controls[k].z * blend; } } void bezier (wcPt3 * controls, int nControls, int m, wcPt3 * curve) { /* Allocate space for the coefficients */ int * c = (int *) malloc (nControls * sizeof (int)); int i; computeCoefficients (nControls-1, c); for (i=0; i<=m; i++) computePoint (i / (float) m, &curve[i], nControls, controls, c); free (c); } /* EXAMPLE ENDS HERE */ #define N_CONTROLS 4 #define N_PTS 128 void main (int argc, char ** argv) { wcPt3 pts[] = { 50, 50, 0, 150, 150, 0, 250, 40, 0, 300, 200, 0 }; wcPt3 curvePts[N_PTS+1]; int i; long windowID = openGraphics (*argv, 350, 250); float pt[3]; setBackground (WHITE); setColor (BLACK); bezier (pts, N_CONTROLS, N_PTS, curvePts); /* Use 'gl' to draw the lines */ bgnline (); for (i=0; i