home *** CD-ROM | disk | FTP | other *** search
- /* «RM120»«PL99999»«TS4,8,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68,72,76» */
- #include <stdio.h>
- #define EXTERN extern
- #include <typedef.h>
-
- int bezier(A, MaxContrPoints, B, MaxIntPoints)
- double *A, *B;
- int MaxContrPoints, MaxIntPoints;
- {
- extern char *calloc();
- extern double pow();
-
- double *combi, SumX, SumY, u, DeltaU;
- double mult, amult;
- int IntPoint;
- int i, j, k;
-
- /* construct the binomial coefficients */
-
- if (NULL == (combi = (double *)calloc(MaxContrPoints, sizeof(double)))) {
- fprintf(stderr, "Ran out of memory in Bezier - 1.\n");
- return(-1);
- }
-
- combi[0] = 1.;
- combi[MaxContrPoints-1] = 1.;
- j = (MaxContrPoints+1) / 2;
- for (i = 1; i < j; i++) {
- combi[i] = combi[i-1] * (MaxContrPoints - i) /(double)i;
- combi[MaxContrPoints - 1 - i] = combi[i];
- }
-
- /* initial point: u = 0 */
- B[0] = A[0];
- B[1] = A[1];
-
- /* final point: u = 1 */
- B[2*MaxIntPoints-2] = A[2*MaxContrPoints-2];
- B[2*MaxIntPoints-1] = A[2*MaxContrPoints-1];
-
- DeltaU = .5 / (MaxIntPoints - 1); /* one half spacing */
-
- for (i = 2; i < 2*MaxIntPoints-2; i+=2) { /* loop through the inter- */
- /* polation points */
- u = i * DeltaU;
- mult = pow(1.-u, (double)(MaxContrPoints-1));
- amult = u / (1. - u);
- SumX = 0.;
- SumY = 0.;
- k = 0;
- for (j = 0; j < MaxContrPoints; j++) { /* loop thru Control Pts */
- SumX = SumX + combi[j] * mult * A[k++];
- SumY = SumY + combi[j] * mult * A[k++];
- /* printf("%d %d %lf %lf %lf %lf %lf %lf\n", i, j, u, mult,
- A[2*j], A[2*j+1], SumX, SumY);
- */
- mult = mult * amult;
- }
- B[i] = SumX;
- B[i+1] = SumY;
- }
-
- free(combi);
- }
-