0% found this document useful (0 votes)
97 views38 pages

Program 1: Line Using Dda Algorithm

This document contains 6 programs related to computer graphics and 2D line drawing algorithms: 1. The first program implements DDA line algorithm to draw a line between two points. 2. The second program implements Bresenham's line drawing algorithm. 3. The third program implements Bresenham's circle drawing algorithm to draw a circle. 4. The fourth program demonstrates 2D transformations on polygons like translation, rotation, scaling and reflection. 5. The fifth program implements Cohen-Sutherland line clipping algorithm to clip a line within a viewing window. 6. The sixth program implements Liang-Barsky line clipping algorithm to clip a line within boundaries.

Uploaded by

Avik Mukherjee
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views38 pages

Program 1: Line Using Dda Algorithm

This document contains 6 programs related to computer graphics and 2D line drawing algorithms: 1. The first program implements DDA line algorithm to draw a line between two points. 2. The second program implements Bresenham's line drawing algorithm. 3. The third program implements Bresenham's circle drawing algorithm to draw a circle. 4. The fourth program demonstrates 2D transformations on polygons like translation, rotation, scaling and reflection. 5. The fifth program implements Cohen-Sutherland line clipping algorithm to clip a line within a viewing window. 6. The sixth program implements Liang-Barsky line clipping algorithm to clip a line within boundaries.

Uploaded by

Avik Mukherjee
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 38

PROGRAM 1 : LINE USING DDA ALGORITHM.

#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <time.h>
void graph()
{
int x,y;
for(x=320,y=0;y<=480;y++)
putpixel(x,y,7);
for(y=240,x=0;x<=640;x++)
putpixel(x,y,7);
}
void exchange(float &a, float &b)
{
float temp;
temp=a;
a=b;
b=temp;
}
void scan_convert(float x, float y, float colour)
{
int X,Y;
X=floor(x+0.5);
Y=floor(y+0.5);
putpixel(320+X,240-Y,1);
}
void main()
{
clrscr();
float x1,x2,y1,y2,dx,dy,m,temp1,temp2;
int gdriver= DETECT, gmode;
initgraph(&gdriver,&gmode, "c:\\tc\\bgi");
graph();
printf("Enter the coordinates of the two endpoints \n");
printf("x1 ");
scanf("%f", &x1);

printf("y1 ");
scanf("%f", &y1);
printf("x2 ");
scanf("%f", &x2);
printf("y2 ");
scanf("%f", &y2);
m=(y2-y1)/(x2-x1);
if(m<=1&&m>=-1)
{
if(x1>x2)
{
exchange(x1,x2);
exchange(y1,y2);
}
for(;x1<=x2;x1++)
{
y1=y1+m;
scan_convert(x1,y1,1);
}
}
else
{
if(y1>y2)
{
exchange(y1,y2);
exchange(x1,x2);
}
for(;y1<y2;y1++)
{
x1=x1+(1/m);
scan_convert(x1,y1,1);
}
}
getch();
closegraph();
}

OUTPUT

PROGRAM 2. BRESENHAMS LINE ALGORITHM


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<time.h>
void put_pixel(int x, int y, int col)
{
putpixel(x+320, 240-y, col);
}
void brsnhm_line(int x1, int y1, int x2, int y2)
{
setcolor(RED);
line(320,0,320,480);
setcolor(BLUE);
line(0,240,640,240);
setcolor(WHITE);
int xa, ya,xb,yb;
if(x1<x2)
{
xa=x1;ya=y1;
xb=x2;yb=y2;
}
else
{
xa=x2;ya=y2;
xb=x1;yb=y1;
}
int dx,dy;
dx=xb-xa;
dy=yb-ya;
int d;
float x=xa, y=ya;
put_pixel(xa,ya,15);
float m=dy/dx;
if(m>=0&&m<=1)

{
d=2*dy-dx;
while(x<xb)
{
if(d<0)
{
d+=2*dy;
x++;
}
else
{
d+=2*(dy-dx);
x++;
y++;
}
put_pixel(x,y,15);
}
}
else if(m>1)
{
d=2*dx-dy;
while(x<xb)
{
if(d<0)
{
d+=2*dx;
y++;
}
else
{
d+=2*(dx-dy);
x++;
y++;
}
put_pixel(x,y,15);
}
}
else if(m>=-1 && m<0)
{
d=-2*dy-dx;
while(x<xb)
{
if(d<0)
{

d-=2*dy;
x++;
}
else
{
d-=2*(dx+dy);
y--;
x++;
}
put_pixel(x,y,15);
}
}
else
{
d=-2*dx-dy;
while(x<xb)
{
if(d>0)
{
d-= 2*dx;
y--;
}
else
{
d-= 2*(dx+dy);
y--;
x++;
}
put_pixel(x,y,15);
}
}
}
void main()
{
clrscr();
int x1,y1,x2,y2;
printf("Enter x1,y1 : ");
scanf("%d %d",&x1,&y1);
printf("Enter x2,y2 : ");
scanf("%d %d",&x2,&y2);
int gdriver = DETECT, gmode;

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");


brsnhm_line(x1,y1,x2,y2);
getch();
closegraph();
}

OUTPUT

PROGRAM 3. BRESENHAMS CIRCLE ALGORITHM


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
#include<time.h>
void graph()
{
int x,y;
for(x=320,y=0;y<480;y++)
{
putpixel(x,y,4);
}
for(y=240,x=0;x<640;x++)
{
putpixel(x,y,4);
}
}
void scan_convert(float h,float k,float X,float Y,int colour)
{
int x,y;
x=floor(X+0.5);
y=floor(Y+0.5);
putpixel((h+x+320),(240-k-y),colour);
putpixel((h-x+320),(240-k+y),colour);
putpixel((h+x+320),(240-k+y),colour);
putpixel((h-x+320),(240-k-y),colour);
putpixel((h+y+320),(240-k-x),colour);
putpixel((h-y+320),(240-k+x),colour);
putpixel((h+y+320),(240-k+x),colour);
putpixel((h-y+320),(240-k-x),colour);
}

void circle(float r,float p,float q)


{
float d,x,y=r;
d=3-2*r;
for(x=0;x<r/sqrt(2);x+=0.01)
{
if(d<0)
d=d+4*x+6;
else
{
d=d+4*(x-y)+10;
y=y-0.01;
}

scan_convert(p,q,x,y,14);
}
}

void main()
{
clrscr();
int gdriver= DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
float r,x,y;
printf("Enter the radius:");
scanf("%f",&r);
printf("Enter the co-ordinates of the centre:");
scanf("%f %f",&x,&y);
graph();
circle(r,x,y);
getch();
}

OUTPUT

PROGRAM 4. 2D TRANSFORMATIONS
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<process.h>
int x[10],y[10],x1[10],y1[10],i,sides;
void graph()
{
int x,y;
for(x=320,y=0;y<480;y++)
{
putpixel(x,y,4);
}
for(y=240,x=0;x<640;x++)
{
putpixel(x,y,4);
}
}
void translation(int tx,int ty)
{
int gdriver= DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
graph();
for(i=0;i<=sides;i++)
{
x1[i]=x[i]+tx;
y1[i]=y[i]+ty;
}
for(i=0;i<sides;i++)
{
line(320+x1[i],240-y1[i],320+x1[i+1],240-y1[i+1]);
}
getch();
closegraph();
}
void rotation(int angle)
{
int gdriver= DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
graph();

for(i=0;i<=sides;i++)
{
x1[i]=x[i]*cos(angle)+y[i]*sin(angle);
y1[i]=-x[i]*sin(angle)+y[i]*cos(angle);
}
for(i=0;i<sides;i++)
{
line(320+x1[i],240-y1[i],320+x1[i+1],240-y1[i+1]);
}
getch();
closegraph();
}

void scaling(int sx,int sy)


{
int gdriver= DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
graph();
for(i=0;i<=sides;i++)
{
x1[i]=x[i]*sx;
y1[i]=y[i]*sy;
}
for(i=0;i<sides;i++)
{
line(320+x1[i],240-y1[i],320+x1[i+1],240-y1[i+1]);
}
getch();
closegraph();
}
void reflection(int m,int c)
{
int gdriver= DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
graph();
for(i=0;i<=sides;i++)
{
x1[i]=x[i]*(1-m*m)/(1+m*m)+y[i]*2*m/(1+m*m)-2*c*m/(1+m*m);
y1[i]=x[i]*2*m/(1+m*m)+y[i]*(m*m-1)/(1+m*m)+2*c/(1+m*m);
}
for(i=0;i<sides;i++)
{
line(320+x1[i],240-y1[i],320+x1[i+1],240-y1[i+1]);
}

getch();
closegraph();
}
void main()
{
clrscr();
int sx,sy,tx,ty,angle,m,c,ch;
printf("Enter the number of sides of the polygon:");
scanf("%d",&sides);
printf("Enter the co-ordinates of the vertices of the polygon:");
for(i=0;i<sides;i++)
{
scanf("%d %d",&x[i],&y[i]);
}
x[sides]=x[0];
y[sides]=x[0];
int gdriver= DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
graph();
for(i=0;i<sides;i++)
{
line(320+x[i],240-y[i],320+x[i+1],240-y[i+1]);
}
printf("What do you wish to do? Enter your
choice-\n1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter the translation distance along x and y direction:");
scanf("%d %d",&tx,&ty);
translation(tx,ty);
break;
case 2:printf("enter the rotation angle:");
scanf("%d",&angle);
rotation(angle);
break;
case 3:printf("Enter the scaling factor along x and y axis:");
scanf("%d %d",&sx,&sy);
scaling(sx,sy);

break;
case 4:printf("Reflection about y=mx+c. \nEnter m and c:");
scanf("%d %d",&m,&c);
reflection(m,c);
break;
default: printf("Wrong choice!! Breaking!!");
exit(0);
}
getch();
}

OUTPUT

PROGRAM 5. COHEN-SUTHERLAND ALGORITHM


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#define LEFT 0x01
#define RIGHT 0x4
#define BOTTOM 0x2
#define TOP 0x8
char getcode(float x, float y, float xwmin, float ywmin, float xwmax, float ywmax)
{
unsigned char code = 0x00;
if(x<xwmin)
code = code|LEFT;
if(x>xwmax)
code = code|RIGHT;
if(y>ywmin)
code = code|BOTTOM;
if(y<ywmax)
code = code|TOP;
return code;
}
void lin(float x1, float y1, float x2, float y2, float xwmin, float ywmin, float xwmax, int
ywmax)
{
int done = 0, accept = 0;
unsigned char code1, code2;
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
setcolor(BLUE);
line(300,0,300,479);
setcolor(RED);
line(0,240,639,240);
setcolor(YELLOW);
rectangle(xwmin, ywmin, xwmax, ywmax);
setcolor(GREEN);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);

float m;
while(done==0)
{
code1 = getcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
code2 = getcode(x2,y2,xwmin,ywmin,xwmax,ywmax);
/* case I - accept line */
if(((code1&code2)==0) && ((code1|code2)==0))
{
accept = 1;
done = 1;
}
else if((code1&code2)!=0)
{
done = 1;
outtextxy(10,300,"\n Sorry! Line rejected");
}
else
{
if((x1>= xwmin && x1<= xwmax) && (y1>= ywmax && y1<=ywmin))
{
float temp = x1;
x1 = x2;
x2=temp;
temp = y1;
y1=y2;
y2=temp;
char t;
t=code1;
code1=code2;
code2=t;
}
if(x1!=x2)
m = (y2-y1)/(x2-x1);
if( code1 & LEFT != 0)
{
y1+= (xwmin-x1)*m;
x1 = xwmin;
}
else if(code1 & RIGHT)
{
y1+= (xwmax-x1)*m;
x1 = xwmax;
}

else if(code1 & BOTTOM)


{
if(x2!=x1)
x1+= (ywmin - y1)/m;
y1 = ywmin;
}
else
{
if(x2!=x1)
x1+= (ywmax-y1)/m;
y1 = ywmax;
}
}
}
if(accept == 1)
line(x1,y1,x2,y2);
}
void main()
{
int gdriver = DETECT, gmode;
float xwmin, xwmax, ywmin, ywmax;
cout<<" Enter the x limits for the clipping window : ";
cin>>xwmin>>xwmax;
cout<<"\n Enter the y limits fro the clipping window :";
cin>>ywmin>>ywmax;
cout<<"\n Enter end point 1 : ";
float x1,y1,x2,y2;
cin>>x1>>y1;
cout<<"\n Enter end point 2 : ";
cin>>x2>>y2;
lin(x1+300,240-y1,x2+300,240-y2,xwmin+300,240-ywmin,xwmax+300,240-ywmax);
// i have interchanged ywmin and ywmax here beacuse we are doing 240-y
getch();
closegraph();
}

OUTPUT

PROGRAM 6. LIANG-BARSKY ALGORITHM


#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<process.h>
float max(float a,float b,float c,float d)
{
float e,f;
if(a>b)
{
e=a;
}
else
{
e=b;
}
if(c>d)
{
f=c;
}
else
{
f=d;
}
if(e>f)
{
return e;
}
else
{
return f;
}
}
float min(float g,float h,float m,float j)
{
float k,l;
if(g<h)
{
k=g;
}
else
{
k=h;

}
if(m<j)
{
l=m;
}
else
{
l=j;
}
if(k<l)
{
return k;
}
else
{
return l;
}
}
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
float x1,x2,y1,y2,u[4]={0},p[10],i;
float q[10],dx,dy,xmin,xmax,ymin,ymax;
float xm,ym,xn,yn;
cout<<"enter x1,y1,x2,y2,xmin,xmax,ymin,ymax";
cin>>x1>>y1>>x2>>y2>>xmin>>ymin>>xmax>>ymax>>ymin>>ymax;
setcolor(BLUE);
line(320+xmin,240-ymin,320+xmax,240-ymin);
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmin,240-ymax,320+xmax,240-ymax);
setcolor(GREEN);
line(320+x1,240-y1,320+x2,240-y2);
getch();
dx=x2-x1;
dy=y2-y1;
p[1]=-dx;p[2]=dx;p[3]=-dy;p[4]=dy;
q[1]=x1-xmin;
q[2]=xmax-x1;
q[3]=y1-ymin;
q[4]=ymax-y1;
float u1=0.0;

float u2=1.0;
for(i=1;i<=4;i++)
{
if(p[i]<0 )
u[i]=q[i]/p[i];
}
u1=max(u[1],u[2],u[3],u[4]);
if(u1<0)
{
u1=0;
}
u[1]=2;u[2]=2;u[3]=2;u[4]=2;
for(i=1;i<=4;i++)
{
if(p[i]>0)
u[i]=q[i]/p[i];
}
u2=min(u[1], u[2],u[3],u[4]);
if(u2>1)
{
u2=1;
}
if(p[1]==0)
{
if(q[1]<0 ||q[2]<0)
{
exit(0);
}
}
if(u1>u2)
{
exit(0);
}
xm=x1+u1*dx;
ym=y1+u1*dy;
xn=x1+u2*dx;
yn=y1+u2*dy;
setcolor(WHITE);
line(320+xm,240-ym,320+xn,240-yn);
getch();
}

OUTPUT

PROGRAM 7 : FERGUSON CURVE


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void ferguson();
void main()
{
int driver,mode;
driver=DETECT;
initgraph(&driver,&mode,"c:\\tc\\bgi");
ferguson();
}
void ferguson()
{
float p1[3],p2[3],p3[3],p4[3],temp[3];
cout<<"\n enter the coordinates of P1 \n";
cin>>p1[0]>>p1[1]>>p1[2];
cout<<"\n enter the coordinates of P2 \n";
cin>>p2[0]>>p2[1]>>p2[2];
cout<<"\n enter the coordinates of P3 \n";
cin>>p3[0]>>p3[1]>>p3[2];
cout<<"\n enter the coorinatesds of P4 \n";
cin>>p4[0]>>p4[1]>>p4[2];
temp[0]=p1[0]; temp[1]=p1[1]; temp[2]=p1[2];
cleardevice();
for(float u=.001;u<=1;u+=.001)
{
temp[0]=(1-(3*u*u)+(2*u*u*u))*p1[0] + u*(1-(2*u)+(u*u))*p2[0] + (u*u)*(-1+u)*p3[0] +
(u*u)*(3-(2*u))*p4[0];
temp[1]=(1-(3*u*u)+(2*u*u*u))*p1[1] + u*(1-(2*u)+(u*u))*p2[1] + (u*u)*(-1+u)*p3[1] +
(u*u)*(3-(2*u))*p4[1];
temp[2]=(1-(3*u*u)+(2*u*u*u))*p1[2] + u*(1-(2*u)+(u*u))*p2[2] + (u*u)*(-1+u)*p3[2] +
(u*u)*(3-(2*u))*p4[2];
putpixel(temp[0],temp[1],BLUE);
}
setcolor(WHITE);
line(p1[0],p1[1],p2[0],p2[1]);
line(p2[0],p2[1],p3[0],p3[1]);
line(p3[0],p3[1],p4[0],p4[1]);
line(p1[0],p1[1],p4[0],p4[1]);
getch();
}

OUTPUT

PROGRAM 8 : BEZIER CURVE


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void bezier();
void main()
{
int driver,mode;
driver=DETECT;
initgraph(&driver,&mode,"c:\\tc\\bgi");
bezier();
}
void bezier()
{
float p1[3],p2[3],p3[3],p4[3],temp[3];
cout<<"\n enter the coordinates of P1 \n";
cin>>p1[0]>>p1[1]>>p1[2];
cout<<"\n enter the coordinates of P2 \n";
cin>>p2[0]>>p2[1]>>p2[2];
cout<<"\n enter the coordinates of P3 \n";
cin>>p3[0]>>p3[1]>>p3[2];
cout<<"\n enter the coorinatesds of P4 \n";
cin>>p4[0]>>p4[1]>>p4[2];
temp[0]=p1[0]; temp[1]=p1[1]; temp[2]=p1[2];
cleardevice();
for(float t=.001;t<=1;t+=.001)
{
temp[0]=(1-t)*(1-t)*(1-t)*p1[0] + (3*t*(1-t)*(1-t))*p2[0] + ((3*t*t)*(1-t))*p3[0] +
((t*t*t))*p4[0];
temp[1]=(1-t)*(1-t)*(1-t)*p1[1] + (3*t*(1-t)*(1-t))*p2[1] + ((3*t*t)*(1-t))*p3[1] +
((t*t*t))*p4[1];
temp[2]=(1-t)*(1-t)*(1-t)*p1[2] + (3*t*(1-t)*(1-t))*p2[2] + ((3*t*t)*(1-t))*p3[2] +
((t*t*t))*p4[2];
putpixel(temp[0],temp[1],BLUE);
}
setcolor(WHITE);
line(p1[0],p1[1],p2[0],p2[1]);
line(p2[0],p2[1],p3[0],p3[1]);
line(p3[0],p3[1],p4[0],p4[1]);
line(p1[0],p1[1],p4[0],p4[1]);
getch();
}

OUTPUT

PROGRAM 9. COMPARE EXECUTION TIMES FOR


DIFFERENT LINE ALGORITHM
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <time.h>
void graph()
{
int x,y;
for(x=320,y=0;y<=480;y++)
putpixel(x,y,7);
for(y=240,x=0;x<=640;x++)
putpixel(x,y,7);
}
void exchange(float &a, float &b)
{
float temp;
temp=a;
a=b;
b=temp;
}
void scan_convert(float x, float y, float colour)
{
int X,Y;
X=floor(x+0.5);
Y=floor(y+0.5);
putpixel(320+X,240-Y,1);
}
void put_pixel(int x, int y, int col)
{
putpixel(x+320, 240-y, col);
}
void brsnhm_line(int x1, int y1, int x2, int y2)
{
setcolor(RED);

line(320,0,320,480);
setcolor(BLUE);
line(0,240,640,240);
setcolor(WHITE);
int xa, ya,xb,yb;
if(x1<x2)
{
xa=x1;ya=y1;
xb=x2;yb=y2;
}
else
{
xa=x2;ya=y2;
xb=x1;yb=y1;
}
int dx,dy;
dx=xb-xa;
dy=yb-ya;
int d;
float x=xa, y=ya;
put_pixel(xa,ya,15);
float m=dy/dx;
if(m>=0&&m<=1)
{
d=2*dy-dx;
while(x<xb)
{
if(d<0)
{
d+=2*dy;
x++;
}
else
{
d+=2*(dy-dx);
x++;
y++;
}
put_pixel(x,y,15);
}
}

else if(m>1)
{
d=2*dx-dy;
while(x<xb)
{
if(d<0)
{
d+=2*dx;
y++;
}
else
{
d+=2*(dx-dy);
x++;
y++;
}
put_pixel(x,y,15);
}
}
else if(m>=-1 && m<0)
{
d=-2*dy-dx;
while(x<xb)
{
if(d<0)
{
d-=2*dy;
x++;
}
else
{
d-=2*(dx+dy);
y--;
x++;
}
put_pixel(x,y,15);
}
}
else
{
d=-2*dx-dy;
while(x<xb)
{
if(d>0)
{

d-= 2*dx;
y--;
}
else
{
d-= 2*(dx+dy);
y--;
x++;
}
put_pixel(x,y,15);
}
}
}
void main()
{
clrscr();
float a,b,c,d,x1,x2,y1,y2,dx,dy,m,temp1,temp2,i;
double dif1,dif2,dif3;
int gdriver= DETECT, gmode;
initgraph(&gdriver,&gmode, "c:\\tc\\bgi");
//clock_t start,end,diff;
graph();
time_t start,end;
printf("Enter the coordinates of the two endpoints \n");
printf("x1 ");
scanf("%f", &a);
printf("y1 ");
scanf("%f", &b);
printf("x2 ");
scanf("%f", &c);
printf("y2 ");
scanf("%f", &d);
//DDA Algorithm
//start=clock();
time (&start);
for(i=1;i<=10000;i++)
{
x1=a;
y1=b;
x2=c;
y2=d;
m=(y2-y1)/(x2-x1);
if(m<=1&&m>=-1)

{
if(x1>x2)
{
exchange(x1,x2);
exchange(y1,y2);
}
for(;x1<=x2;x1++)
{
y1=y1+m;
scan_convert(x1,y1,1);
}
}
else
{
if(y1>y2)
{
exchange(y1,y2);
exchange(x1,x2);
}
for(;y1<y2;y1++)
{
x1=x1+(1/m);
scan_convert(x1,y1,1);
}
}
}
//end=clock();
time (&end);
dif1=difftime(end,start);
dif1=dif1/10000;
//diff=end-start;
printf("Time taken using DDA Algorithm is %f seconds.",dif1);
printf("Press 1 to continue");
scanf("%f", &i);
getch();
closegraph();
clrscr();
initgraph(&gdriver,&gmode, "c:\\tc\\bgi");
time (&start);
for(i=1;i<=10000;i++)
brsnhm_line(a,b,c,d);

time (&end);
dif2=difftime(end,start);
dif2=dif2/10000;
printf("Time taken using Bresenham's Algorithm is %f seconds",dif2);
getch();
closegraph();
}

OUTPUT

PROGRAM 10. COMPARE EXECUTION TIMES FOR


DIFFERENT CIRCLE ALGORITHMS
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
#include<time.h>
void graph()
{
int x,y;
for(x=320,y=0;y<480;y++)
{
putpixel(x,y,4);
}
for(y=240,x=0;x<640;x++)
{
putpixel(x,y,4);
}
}
void scan_convert(float h,float k,float X,float Y,int colour)
{
int x,y;
x=floor(X+0.5);
y=floor(Y+0.5);
putpixel((h+x+320),(240-k-y),colour);
putpixel((h-x+320),(240-k+y),colour);
putpixel((h+x+320),(240-k+y),colour);
putpixel((h-x+320),(240-k-y),colour);
putpixel((h+y+320),(240-k-x),colour);
putpixel((h-y+320),(240-k+x),colour);
putpixel((h+y+320),(240-k+x),colour);
putpixel((h-y+320),(240-k-x),colour);
}

void circle1(float r,float p,float q)


{
float d,x,y=r;
d=3-2*r;
for(x=0;x<r/sqrt(2);x+=0.01)
{
if(d<0)
d=d+4*x+6;
else
{
d=d+4*(x-y)+10;

y=y-0.01;
}
scan_convert(p,q,x,y,14);
}
}
void circle2(float r,float p,float q)
{
float y;
for(float x=0;x<r/(sqrt(2));x+=0.01)
{
y=sqrt(r*r-x*x);
scan_convert(p,q,x,y,14);
}
}
void main()
{
clrscr();
int gdriver= DETECT,gmode;
int i;
time_t start,end;
double dif1,dif2;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
float r,x,y;
printf("Enter the radius:");
scanf("%f",&r);
printf("Enter the co-ordinates of the centre:");
scanf("%f %f",&x,&y);
graph();
time (&start);
for(i=1;i<=100;i++)
circle1(r,x,y);
time (&end);
dif1=difftime(end,start);
dif1=dif1/100;
printf("Time taken using Bresenham's algorithm= %f",dif1);
getch();
closegraph();
printf("Enter 1 to continue");
scanf("%f",&i);
clrscr();
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
graph();

time (&start);
for(i=1;i<100;i++)
circle2(r,x,y);
time (&end);
dif2=difftime(end,start);
dif2=dif2/100;
printf("Time taken using square root method= %f",dif2);
printf("Enter 1");
scanf("%f",&i);
getch();
closegraph();
}

OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy