A C++ program to draw a Bezier Curve of nth Degree


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
using namespace std;
void bezier_curve(const int[8]);
double nCr(int,int);
double factorial(int);
void dashed_line(const int,const int,const int,const int,const int=0);
int main(){
int gd=DETECT,gm;
int control_points[8]={0};
for(int count=0;count<=3;count++) {
cout<<“coordinates of point–“<<count<<” (x”<<count<<“,y”<<count<<“)
:”<<endl;
cout<<“IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII”<<endl;
cout<<“Enter tyhe value of x”<<count<<” = “<<endl;
cin>>control_points[(count*2)];
cout<<“Enter tyhe value of y”<<count<<” = “<<endl;
cin>>control_points[((count*2)+1)];
cout<<” “<<endl;
cout<<” “<<endl;
cout<<” “<<endl;}
initgraph(&gd,&gm,””);
setcolor(15);
bezier_curve(control_points);
getch();
closegraph();
return 0;}
void bezier_curve(const int cp[8]){
int color=getcolor();
setcolor(7);
for(int count=0;count<3;count++)
dashed_line(cp[(count*2)],cp[((count*2)+1)],cp[((count+1)*2)],cp[(((count+1)*2)+1)]
);
float x;
float y;
for(float u=0.0005;u<=1;u+=0.0005){
float x=0;
float y=0;
for(int k=0;k<=3;k++){
x+=(cp[(k*2)]*nCr(3,k)*pow(u,k)*pow((1-u),(3-k)));
y+=(cp[((k*2)+1)]*nCr(3,k)*pow(u,k)*pow((1-u),(3-k))); }
putpixel((int)(x+0.5),(int)(y+0.5),color);
}}
double nCr(int n,int r){
double nf,rf,nrf,ncr;
nf=factorial(n);
rf=factorial(r);
nrf=factorial((n-r));
ncr=(nf/(rf*nrf));
return ncr;}
double factorial(int number){
double factorial=1;
if(number==0 || number==1);
else {
for(int count=1;count<=number;count++)
factorial=factorial*count;}
return factorial;}
void dashed_line(const int x_1,const int y_1,const int x_2,const int y_2,const int
line_type){
int count=0;
int color=getcolor();
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>=x_2){
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1; }
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy) {
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2) {
x++;
if(p<0)
p+=two_dy;
else {
y+=inc_dec;
p+=two_dy_dx; }
if((count%2)!=0 && line_type==0)
putpixel(x,y,color);
else if((count%5)!=4 && line_type==1)
putpixel(x,y,color);
else if((count%10)!=8 && (count%10)!=9 && line_type==2)
putpixel(x,y,color);
else if((count%20)!=18 && (count%20)!=19 && line_type==3)
putpixel(x,y,color);
else if((count%12)!=7 && (count%12)!=8 && (count%12)!=10 &&
(count%12)!=11 && line_type==4)
putpixel(x,y,color);
count++; } }
else{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2) {
y+=inc_dec;
if(p<0)
p+=two_dx;
else{
x++;
p+=two_dx_dy; }
if((count%2)!=0 && line_type==0)
putpixel(x,y,color);
else if((count%5)!=4 && line_type==1)
putpixel(x,y,color);
else if((count%10)!=8 && (count%10)!=9 && line_type==2)
putpixel(x,y,color);
else if((count%20)!=18 && (count%20)!=19 && line_type==3)
putpixel(x,y,color);
else if((count%12)!=7 && (count%12)!=8 && (count%12)!=10 &&
(count%12)!=11 && line_type==4)
putpixel(x,y,color);
count++;
}}}