/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Programmation du calcul */
/* d'une courbe de Bezier */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct coord_3D {
float x ;
float y ;
float z ; } coord_3D ;
typedef struct polygone {
int n ;
coord_3D *c ; } polygone ;
void pixel(float x,float y,float z) {
printf("%f %f %f\n",x,y,z) ;
}
double power(double v,int p) {
return(( !v && !p ) ?
1.:
pow(v,(double) p)) ;
}
/* En entree le polygone p, le nombre
de points n */
void bezier(polygone *p,int n) {
int i,j ;
float t,mt ;
float *cn,x,y,z,fac ;
cn =(float *) calloc(p->n,sizeof(float));
cn[0] = 1 ;
cn[1] =(float) (p->n-1) ;
for ( i = 2 ; i < p->n ; i++ )
cn[i] = cn[i-1] * (p->n - i) / i ;
for ( i = 0 ; i < n ; i++ ) {
t =(float) i/(n-1) ;
mt = 1-t ;
x = y = z = 0.0F ;
for ( j = 0 ; j < p->n ; j++ ) {
fac = cn[j]*(float) power(t,j)*
(float) power(mt,p->n-1-j);
x += fac * p->c[j].x ;
y += fac * p->c[j].y ;
z += fac * p->c[j].z ; }
pixel(x,y,z) ; }
free(cn) ;
}
int main(int argc,char **argv) {
coord_3D c[8] = { {-6.0F,-6.0F, 3.0F},
{-6.0F,-1.0F, 1.0F},
{-5.0F, 2.0F,-1.0F},
{-1.0F, 3.0F, 1.0F},
{ 2.0F, 1.0F, 4.0F},
{ 4.0F, 2.0F, 2.0F},
{ 5.0F, 5.0F,-2.0F},
{ 6.0F, 6.0F,-4.0F} } ;
polygone p = { 8,c } ;
bezier(&p,50) ;
return(0);
}