Bezier Curve

Click to Download

/*
* Status: Tested OK
* Title: Bezier curves
* Name: Akshay Thakare
* Date: 29-4-2014
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glu.h>

void bezier (double x[4], double y[4])
{
int i;
double t;

for (t = 0.0; t < 1.0; t += 0.0005)
{
double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] + 3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3];

double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] + 3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];

glColor3f(1,1,1);
glBegin(GL_POINTS);
glVertex2d(xt,yt);
glEnd();
}

glColor3f(1,1,0);
glBegin(GL_POINTS);
for (i=0;i<4;i++)
glVertex2d(x[i],y[i]);
glEnd();
return;
}

void disp()
{
double x1[] = {10,10,50,50};
double y1[] = {10,50,10,50};
bezier(x1,y1);
glFlush();
}

void Init()
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,1);
gluOrtho2D(0,70,0,70);
}

int main(int argv, char ** argc)
{
glutInit(&argv,argc);
glutInitWindowSize(300,300);
glutInitWindowPosition(300,300);
glutCreateWindow("Bezier");
Init();
glutDisplayFunc(disp);
glutMainLoop();
return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s