Wednesday, 4 June 2014

Cubic Bezier Curve Drawing Algorithm

                         
/* This routine draws cubic Bezier curves with four points. It accepts four input points and finds out some other points on the curve at specified parameter values.              
/* Output is presented in text mode only.                    
/******************************************************************/


#include<stdio.h>
#include<stdlib.h>

typedef struct {float x, y;} point;
point SmpPt[4], NewPt[10];

main()
{
 int i, n;
 float j0, j1, j2, j3;
 float v[10], u;

 for(i=0;i<4;i++)
   {
    printf("\nGive sample point[%d]:",i);
    scanf("%f %f", &SmpPt[i].x, &SmpPt[i].y);
   }
 printf("\nHow many new points? ");
 scanf("%d", &n);

 for(i=0;i<n;i++)
   {
    printf("\nGive u value[%d](0.0 - 1.0): ",i);
    scanf("%f", &v[i]);
   }

 printf("u \t j0\t j1\t j2\t j3\t x\t y\n");
 for(i=0;i<n;i++)
 {
  u=v[i];
  j0=(1.0-u)*(1.0-u)*(1.0-u);
  j1=3.0*u*(1.0-u)*(1.0-u);
  j2=3.0*u*u*(1.0-u);
  j3=u*u*u;

  NewPt[i].x = SmpPt[0].x*j0 + SmpPt[1].x*j1 +SmpPt[2].x*j2 +SmpPt[3].x*j3;
  NewPt[i].y = SmpPt[0].y*j0 + SmpPt[1].y*j1 +SmpPt[2].y*j2 +SmpPt[3].y*j3;

  printf("%4.3f   %4.3f   %4.3f   %4.3f   %4.3f   %4.3f   %4.3f\n",
                  u,j0,j1,j2,j3, NewPt[i].x,NewPt[i].y); 
 }
}


No comments:

Post a Comment