Mzsmunna
10/16/2019 - 3:06 PM

Bezier Curve


//#include<glut.h>
//#include<math.h>
//#include<stdio.h>

#include <iostream>
#include <stdlib.h>
#include<GL/gl.h>
#include <GL/glut.h>
#include<math.h>
#include<cstring>
#include <stdio.h>
#include <string.h>


void bezierCoefficients(int n,int *c)
{
	int k,i;
	for(k=0;k<=n;k++)
	{
		c[k]=1;
		for(i=n;i>=k+1;i--)
		c[k]*=i;
		for(i=n-k;i>=2;i--)
			c[k]/=i;

	}
}
/*float power(float u,int k)
{
	float temp=1;
	for(;k>0;k--)
	{
		temp*=u;
	}
	return temp;
}*/
void display()
{

	int cp[4][2]={{20,20},{20,50},{40,50},{40,20}};
int c[4],k,n=3;
float x,y,u,blend;
bezierCoefficients(n,c);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0,0.0,0.0);
	glLineWidth(5.0);
	glBegin(GL_LINE_STRIP);

	for(u=0;u<1.0;u+=0.01)
	{x=0;y=0;
		for(k=0;k<4;k++)
		{
			blend=c[k]*pow(u,k)*pow(1-u,n-k);
			x+=cp[k][0]*blend;
			y+=cp[k][1]*blend;
		}
		glVertex2f(x,y);

	}
	glEnd();
	glFlush();
}
void myinit()
{
	glClearColor(1.0,1.0,1.0,1.0);
		glColor3f(1.0,0.0,0.0);
		glPointSize(5.0);
		gluOrtho2D(0.0,250.0,0.0,300.0);
}
int main(int argc, char ** argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize(600,600);

	glutCreateWindow("Bezier Curve");
	glutDisplayFunc(display);
	myinit();

	glutMainLoop();
	return 0;

}