Saturday, 21 February 2015

Bezier curve to generate sine wave

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>  
void bezier (int x[4][3], int y[4][3])
{
int gd = DETECT, gm; int i,j; double t;
   initgraph (&gd, &gm, "");
for(i=0;i<4;i++)
{  
for (t = 0.0; t < 1.0; t += 0.0005)
 {
double xt = pow (1-t, 2) * x[i][0] + 2 * t * pow (1-t, 1) * x[i][1] + pow (t, 2) * x[i][2];
double yt = pow (1-t, 2) * y[i][0] + 2 * t * pow (1-t, 1) * y[i][1] + pow (t, 2) * y[i][2];
putpixel (xt, yt, WHITE);
}
}
setcolor(RED);
for (i=0; i<4; i++)
  {
   for(j=0; j<2; j++)
    {
    line (x[i][j], y[i][j], x[i][j+1], y[i][j+1]);
    }
  }
    line(x[0][0],y[0][0],x[3][2],y[3][2]);  
    getch();
 closegraph();
 return;
 }  
void main()
{
 int x[4][3], y[4][3]; int i,j;
   printf ("Enter the x- and y-coordinates of the three control points.\n");
 for (i=0; i<4; i++)
  {
   for(j=0; j<3; j++)
    {
      scanf ("%d%d", &x[i][j], &y[i][j]);
     }
 }
bezier (x, y);
}

// Input
/*Enter the x- and y-coordinates of the three control points.
(10,50)     (40,10)     (70,50)
(70,50)        (100,90)     (130,50)
(130,50)     (160,10)     (190,50)
(190,50)     (220,90)     (250,50)
*/

No comments:

Post a Comment