Monthly Archives: January 2015

Computer Graphics : Bezier Curve Construction

/*====Bezier Curve====*/

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void bezier(int x[4],int y[4])
{
int gd=DETECT,gm,i;
double u;
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(15);
outtextxy(400,400,"-> Control Points");
outtextxy(400,430,"-> Polygon");
outtextxy(400,460,"-> Bezier Curve");
setcolor(RED);
setfillstyle(1,RED);
circle(390,400,5);
floodfill(390,400,RED);
setcolor(YELLOW);
setfillstyle(1,YELLOW);
circle(390,430,5);
floodfill(390,430,YELLOW);
setcolor(WHITE);
setfillstyle(1,WHITE);
circle(390,460,5);
floodfill(390,460,WHITE);
delay(10);
for(u=0.0;u<1.0;u+=0.0002)
{
double xu= pow(1-u,3)*x[0]+3*u*pow(1-u,2)*x[1]+3*pow(u,2)*(1-u)*x[2]+pow(u,3)*x[3];
double yu= pow(1-u,3)*y[0]+3*u*pow(1-u,2)*y[1]+3*pow(u,2)*(1-u)*y[2]+pow(u,3)*y[3];
putpixel(xu,yu,WHITE);
delay(1);
}
for(i=0;i<4;i++)
{
setcolor(RED);
setfillstyle(1,RED);
circle(x[i],y[i],4);
floodfill(x[i],y[i],RED);
delay(1);
}
for(i=0;i<3;i++)
{
setcolor(YELLOW);
line(x[i],y[i],x[i+1],y[i+1]);
}
}
void main()
{
int x[4],y[4],i;
clrscr();
printf("\n\n====Bezier Curve====\n\n");
printf("Enter the x and y coordinates for the four control points :\n");
for(i=0;i<4;i++)
{
printf("x%d and y%d are :\n",i+1,i+1);
scanf("%d %d",&x[i],&y[i]);
}
bezier(x,y);
getch();
closegraph();
}

Output :

Untitled1Untitled2

That’s All. P)
Happy C-ing.

-Aayush Shrivastava


Are you satisfied?


 

Computer Graphics : Bresenham’s Line Drawing Algorithm

/*====Bresenham's Line Drawing Algorithm====*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int m,x,y,x1,y1,x2,y2,i,dx,dy,P,a;
int gd= DETECT,gm;
clrscr();
printf("\n ===Bresenham's Line Drawing Algorithm===\n\n");
printf(" Enter the co-ordinates of the first point :\n\tx1 : ");
scanf(" %d",&x1);
printf("\n y1 : ");
scanf(" %d",&y1);
printf(" Enter the co-ordinates of the second point :\n\tx2 : ");
scanf(" %d",&x2);
printf("\n y2 : ");
scanf(" %d",&y2);
dx=x2-x1;
dy=y2-y1;
m=(dy/dx);
dy=abs(dy);
dx=abs(dx);
initgraph(&gd,&gm,"C:\\TC\\BGI");
putpixel(x1,y1,15);
if(m<1)
{
P=(2*dy)-dx;
for(a=0;a<dx;a++)
{
if(P<0)
{
if(x1<x2)
x1++;
else
x1--;
P=P+(2*dy);
}
else
{
if(x1<x2)
x1++;
else
x1--;
if(y1<y2)
y1++;
else
y1--;
P=P+(2*(dy-dx));
}
putpixel(x1,y1,15);
delay(20);
}
}
else
{
P=(2*dx)-dy;
for(a=0;a<dy;a++)
{
if(P<0)
{
if(y1<y2)
y1++;
else
y1--;
P=P+(2*dx);
}
else
{
if(x1<x2)
x1++;
else
x1--;
if(y1<y2)
y1++;
else
y1--;
P=P+(2*(dx-dy));
}
putpixel(x1,y1,15);
delay(10);
}
}
getch();
closegraph();
}

Output :
Untitled Untitled1

That’s All. P)
Happy C-ing.

-Aayush Shrivastava


Are you satisfied?


 

Computer Graphics : DDA Line Drawing Algorithm

/*====Digital Differential Analyzer Line Drawing Algorithm====*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main ()
{
int gd=DETECT,gm,x1,y1,x2,y2,dy,dx,temp;
float xinc,yinc;
printf("\n===DDA Line Drawing Algorithm===\n\n");
printf("Enter the co-ordinates of the first point :\nx1 : ");
scanf("%d",&x1);
printf("\ny1 : ");
scanf("%d",&y1);
printf("Enter the co-ordinates of the second point :\nx2 : ");
scanf("%d",&x2);
printf("\ny2 : ");
scanf("%d",&y2);
dy=y2-y1;
dx=x2-x1;
if(abs(dx)>=abs(dy))
temp=abs(dx);
else
temp=abs(dy);
xinc=dx/temp;
yinc=dy/temp;
initgraph(&gd,&gm,"C://TC//BGI");
while(x1!=x2 && y1!=y2)
{
putpixel(x1,y1,15);
x1+=xinc;
y1+=yinc;
delay(20);
}
getch();
closegraph();
}

Output :

Untitled

Untitled1

That’s All. P)
Happy C-ing.

-Aayush Shrivastava


Are you satisfied?