0% found this document useful (0 votes)
1K views

Practical - Programs - Computer Graphics and Multimedia

The document describes 9 programs related to computer graphics and geometric transformations. The programs implement algorithms for drawing lines using DDA and Bresenham's method, drawing circles using midpoint algorithm, translating and scaling lines and triangles, rotating lines and triangles, Cohen-Sutherland line clipping algorithm, and Bezier and Bspline curves. For each program, it provides the source code and brief description.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Practical - Programs - Computer Graphics and Multimedia

The document describes 9 programs related to computer graphics and geometric transformations. The programs implement algorithms for drawing lines using DDA and Bresenham's method, drawing circles using midpoint algorithm, translating and scaling lines and triangles, rotating lines and triangles, Cohen-Sutherland line clipping algorithm, and Bezier and Bspline curves. For each program, it provides the source code and brief description.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 19

INDEX

1. program implenting DDA algorithm for drawing a line

2. program implenting Bresenham's line algorithm

3. program implenting mid point algorithm for drawing a


circle
4. program of line and triangle translation

5. program for graphics scaling

6. program for line and triangle rotation

7. program implenting Cohen-Sutherland line clipping


algorithm

8. program implenting bezier curve

9. program to implement Bspline curve


1) program implenting DDA algorithm for drawing a line

#include<iostream.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void lineDDA(int,int,int,int);
void main()
{
int xa=0,ya=0,xb=0,yb=0;

cout<<"enter the co-ordinates of points\n";


cin>>xa>>ya>>xb>>yb;

int gdriver = DETECT, gmode;


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

lineDDA(xa,ya,xb,yb);
getch(); closegraph();
}

void lineDDA(int xa,int ya,int xb,int yb)


{
int dx,dy,steps,x,y,xi,yi;

dx=xb-xa; dy=yb-ya;

if(dx>dy) steps=dx;
else steps=dy;

xi=dx/steps; yi=dy/steps;
x=xa; y=ya; putpixel(x,y,5);

for(int k=1;k<=steps;k++)
{
x=x+xi; y=y+yi; putpixel(x,y,5);
}
}
2) program implenting Bresenham's line algorithm

#include<iostream.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void lineBRESEN(int,int,int,int);
void main()
{
int xa=0,ya=0,xb=0,yb=0;

cout<<"enter the co-ordinates of points/n";


cin>>xa>>ya>>xb>>yb;

int gdriver = DETECT, gmode;


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

if(xa<xb)
lineBRESEN(xa,ya,xb,yb);
else
lineBRESEN(xb,yb,xa,ya);
getch();
closegraph();
}

void lineBRESEN(int xa,int ya,int xb,int yb)


{
int dx,dy,x=xa,y=ya;
putpixel(xa,ya,5);

dx=xb-xa; dy=yb-ya;
int p=2*dy-dx;

while(y<yb||x<xb)
{
if(p<0)
{
x+=1; putpixel(x,y,5); p+=2*dy;
}
else
{
x+=1;y+=1; putpixel(x,y,5); p+=2*(dy-dx);
}
}
}

3) program implenting mid point algorithm for drawing a


circle

#include<iostream.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void circleMID(int,int,int);
void cpixel1(int,int,int,int);
void main()

{
int xa=0,ya=0,r=0;

cout<<"enter the co-ordinates of center\n";


cin>>xa>>ya;
cout<<"\nenter radius\n";
cin>>r;

int gdriver = DETECT, gmode;

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


setbkcolor(2);

circleMID(xa,ya,r);

getch();
closegraph();
getch();
}
void cpixel1(int x,int y,int xa,int ya)
{

putpixel(x+xa,y+ya,5);
putpixel(x+xa,-y+ya,5);
putpixel(-x+xa,y+ya,5);
putpixel(-x+xa,-y+ya,5);
putpixel(y+xa,x+ya,5);
putpixel(y+xa,-x+ya,5);
putpixel(-y+xa,x+ya,5);
putpixel(-y+xa,-x+ya,5);
}

void circleMID(int xa,int ya,int r)


{
int x=xa,y=ya+r;

cpixel1(x,y,xa,ya);

int p=1-r;

while(y>x)
{
if(p<0)
{
x+=1;

cpixel1(x,y,xa,ya);

p+=2*x+1;
}
else
{
x+=1;y-=1;
cpixel1(x,y,xa,ya);
p+=2*x+1-2*y;
}
}
}

4) program of line and triangle translation

#include<iostream.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void lineT(int,int,int,int,int,int);
void triangleT(int,int,int,int,int,int,int,int);
void main()

{
int xa=0,ya=0,xb=0,yb=0,xc=0,yc=0,tx=0,ty=0,op;
cout<<"select option for translation";
cout<<"\n1 :line\t2 :triangle\n";
cin>>op;

if(op==1)
{
cout<<"enter the co-ordinates of line\n";
cin>>xa>>ya>>xb>>yb;
cout<<"enter translation units";
cin>>tx>>ty;
}
else if(op==2)
{
cout<<"enter the co-ordinates of triangle\n";
cin>>xa>>ya>>xb>>yb>>xc>>yc;
cout<<"enter translation units";
cin>>tx>>ty;
}
else
{
cout<<"wrong input\npress any key to terminate";
getch();
exit(0);
}
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setbkcolor(2);
setcolor(5);

if(op==1)
lineT(xa,ya,xb,yb,tx,ty);
else if(op==2)
triangleT(xa,ya,xb,yb,xc,yc,tx,ty);

getch();
closegraph();
getch();
}

void lineT(int xa,int ya,int xb,int yb,int tx,int ty)


{
setlinestyle(DOTTED_LINE,1,1);
line(xa,ya,xb,yb);

setlinestyle(SOLID_LINE,1,1);
line(xa+tx,ya+ty,xb+tx,yb+ty);
}
void triagleT(int xa,int ya,int xb,int yb,int xc,int yc,int tx,int ty)
{
setlinestyle(DOTTED_LINE,1,1);
line(xa,ya,xb,yb);
line(xb,yb,xc,yc);
line(xc,yc,xa,ya);

setlinestyle(SOLID_LINE,1,1);
line(xa+tx,ya+ty,xb+tx,yb+ty);
line(xb+tx,yb+ty,xc+tx,yc+ty);
line(xc+tx,yc+ty,xa+tx,ya+ty);

}
5) program for graphics scaling

#include<iostream.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void lineT(int,int,int,int,int,int);
void triangleT(int,int,int,int,int,int,int,int);
void main()

{
int xa=0,ya=0,xb=0,yb=0,xc=0,yc=0,tx=0,ty=0,op;
cout<<"select option for scalling transformation";
cout<<"\n1 :line\t2 :triangle\n";
cin>>op;

if(op==1)
{
cout<<"enter the co-ordinates of line\n";
cin>>xa>>ya>>xb>>yb;
cout<<"enter scaling factor";
cin>>tx>>ty;
}
else if(op==2)
{
cout<<"enter the co-ordinates of triangle\n";
cin>>xa>>ya>>xb>>yb>>xc>>yc;
cout<<"enter scalling factor";
cin>>tx>>ty;
}
else
{
cout<<"wrong input\npress any key to terminate";
getch();
exit(0);
}
int gdriver = DETECT, gmode;

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


setbkcolor(2);
setcolor(5);

if(op==1)
lineT(xa,ya,xb,yb,tx,ty);
else if(op==2)
triangleT(xa,ya,xb,yb,xc,yc,tx,ty);

getch();
closegraph();
getch();
}

void lineT(int xa,int ya,int xb,int yb,int tx,int ty)


{
setlinestyle(DOTTED_LINE,1,1);
line(xa,ya,xb,yb);

setlinestyle(SOLID_LINE,1,1);
line(xa*tx,ya*ty,xb*tx,yb*ty);
}
void triagleT(int xa,int ya,int xb,int yb,int xc,int yc,int tx,int ty)
{
setlinestyle(DOTTED_LINE,1,1);
line(xa,ya,xb,yb);
line(xb,yb,xc,yc);
line(xc,yc,xa,ya);

setlinestyle(SOLID_LINE,1,1);
line(xa*tx,ya*ty,xb*tx,yb*ty);
line(xb*tx,yb*ty,xc*tx,yc*ty);
line(xc*tx,yc*ty,xa*tx,ya*ty);

}
6) program for line and triangle rotation

#include<iostream.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>

void lineT(int,int,int,int,int);
void triangleT(int,int,int,int,int,int,int);
void main()

{
int xa=0,ya=0,xb=0,yb=0,xc=0,yc=0,tx=0,op;
cout<<"select option for rotation";
cout<<"\n1 :line\t2 :triangle\n";
cin>>op;

if(op==1)
{
cout<<"enter the co-ordinates of line\n";
cin>>xa>>ya>>xb>>yb;
cout<<"enter rotation angle";
cin>>tx;
}
else if(op==2)
{
cout<<"enter the co-ordinates of triangle\n";
cin>>xa>>ya>>xb>>yb>>xc>>yc;
cout<<"enter rotation angle";
cin>>tx;
}
else
{
cout<<"wrong input\npress any key to terminate";
getch();
exit(0);
}
int gdriver = DETECT, gmode;

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


setbkcolor(2);
setcolor(5);

if(op==1)
lineT(xa,ya,xb,yb,tx);
else if(op==2)
triangleT(xa,ya,xb,yb,xc,yc,tx);

getch();
closegraph();
getch();
}

void lineT(int xa,int ya,int xb,int yb,int tx)


{
setlinestyle(DOTTED_LINE,1,1);
line(xa,ya,xb,yb);

int xa1=double(xa*cos(tx)-ya*sin(tx));
int ya1=double(xa*sin(tx)+ya*cos(tx));
int xb1=double(xb*cos(tx)-yb*sin(tx));
int yb1=double(xb*sin(tx)+yb*cos(tx));

setlinestyle(SOLID_LINE,1,1);
line(xa1,ya1,xb1,yb1);
}
void triagleT(int xa,int ya,int xb,int yb,int xc,int yc,int tx)
{
setlinestyle(DOTTED_LINE,1,1);
line(xa,ya,xb,yb);
line(xb,yb,xc,yc);
line(xc,yc,xa,ya);

int xa1=double(xa*cos(tx)-ya*sin(tx));
int ya1=double(xa*sin(tx)+ya*cos(tx));
int xb1=double(xb*cos(tx)-yb*sin(tx));
int yb1=double(xb*sin(tx)+yb*cos(tx));
int xc1=double(xc*cos(tx)-yc*sin(tx));
int yc1=double(xc*sin(tx)+yc*cos(tx));

setlinestyle(SOLID_LINE,1,1);
line(xa1,ya1,xb1,yb1);
line(xb1,yb1,xc1,yc1);
line(xc1,yc1,xa1,ya1);

7) program implenting Cohen-Sutherland line clipping


algorithm

#include<iostream.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<dos.h>

void main()
{
int xa=0,ya=0,xb=0,yb=0,px1,py1,px2,py2;
int opc,opc2,tpx1,tpx2,tpy1,tpy2,x,x1,y,y1;

cout<<"enter the co-ordinates of line\n";


cin>>xa>>ya>>xb>>yb;

cout<<"\nenter windows' left-bottom corner points' x y cordinates\n";


cin>>px1>>py1;
cout<<"\nenter windows' right-top corner points' x y cordinates\n";
cin>>px2>>py2;

//opcode of point 1
if(xa>px1&&xa<px2&&ya>py1&&ya<py2)
opc=0000;

else if(xa>px2&&ya>py1&&ya<py2)
opc=0010;

else if(xa<px1&&ya>py1&&ya<py2)
opc=0001;

else if(xa<px1&&ya<py1)
opc=0101;

else if(xa>px1&&xa<px2&&ya<py1)
opc=0100;

else if(xa<px2&&ya<py1)
opc=0110;

else if(xa<px1&&ya>py2)
opc=1001;

else if(xa>px1&&xa<px2&&ya>py2)
opc=1000;

else if(xa>px2&&ya>py2)
opc=1010;

//opcode of point 2

if(xb>px1&&xb<px2&&yb>py1&&yb<py2)
opc2=0000;

else if(xb>px2&&yb>py1&&yb<py2)
opc2=0010;

else if(xb<px1&&yb>py1&&yb<py2)
opc2=0001;

else if(xb<px1&&yb<py1)
opc2=0101;
else if(xb>px1&&xb<px2&&yb<py1)
opc2=0100;

else if(xb<px2&&yb<py1)
opc2=0110;

else if(xb<px1&&yb>py2)
opc2=1001;

else if(xb>px1&&xb<px2&&yb>py2)
opc2=1000;

else if(xb>px2&&yb>py2)
opc2=1010;

if(opc==0&&opc2==0)
cout<<"\nline is completely visible\n";

else if((opc==0100||opc==0110||opc==0101)&&(opc2==0100||
opc2==0110||opc2==0101))
cout<<"\nline is invisible\n";

else if((opc==1000||opc==1010||opc==1001)&&(opc2==1000||
opc2==1010||opc2==1001))
cout<<"\nline is invisible\n";

else if((opc==0110||opc==1010||opc==0010)&&(opc2==0110||
opc2==1010||opc2==0010))
cout<<"\nline is invisible\n";

else if((opc==0101||opc==1001||opc==0001)&&(opc2==0101||
opc2==1001||opc2==0001))
cout<<"\nline is invisible\n";

else
cout<<"line is partial visible";

x1=px1;
y=((yb-ya)/(xb-xa))*(x1-xa)+ya;
if((y<ya&&y<yb)||(y>ya&&y>yb))
{
x1=px2;
y=((yb-ya)/(xb-xa))*(x1-xa)+ya;
}
tpx1=x1;tpy1=y;

y1=py1;
x=((xb-xa)/(yb-ya))*(y1-ya)+xa;

if((x<xa&&x<xb)||(x>xa&&x>xb))
{
y1=py2;
x=((xb-xa)/(yb-ya))*(y1-ya)+xa;
}

tpx2=x;tpy2=y1;

cout<<"partial visible part is between following points:-\n\t(";


cout<<tpx1<<","<<tpy1<<"), ("<<tpx2<<","<<tpy2<<")\n";

delay(5000);

int gdriver = DETECT, gmode;


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setbkcolor(2);
setcolor(5);

rectangle(xa,yb,xb,ya);
setcolor(7);
line(xa,ya,xb,yb);

getch();
closegraph();

8) program to implement Bspline curve


#include<graphics.h>
#include<conio.h>
void Bspline(int a[],int b[])
{
float x0,x1,x2,x3,y0,y1,y2,y3,x[10],y[10];
int k,xx,yy;
float i,dt,t,n=10000;
/*setbkcolor(BLUE);*/
setcolor(RED);
dt=1/n;
for(i=0;i<10;i++)
{
x[i]=a[i];
y[i]=b[i];
}
for(k=0;k<10;k++)
{
if(k==0) moveto(x[k],y[k]);
lineto(x[k],y[k]);
}
for(k=0;k<7;k++)
{
x0=x[k]; y0=y[k];
x1=x[k+1];y1=y[k+1];
x2=x[k+2];y2=y[k+2];
x3=x[k+3];y3=y[k+3];
for(i=0;i<=n;i++)
{
setcolor(GREEN);
t=i*dt;
xx=((-t*t*t+3*t*t-3*t+1)/6)*x0+((3*t*t*t-6*t*t+4)/6)*x1+((-
3*t*t*t+3*t*t+3*t+1)/6)*x2+(t*t*t/6)*x3;
yy=((-t*t*t+3*t*t-3*t+1)/6)*y0+((3*t*t*t-6*t*t+4)/6)*y1+((-
3*t*t*t+3*t*t+3*t+1)/6)*y2+(t*t*t/6)*y3;
if(i==0) moveto(xx,yy);
lineto(xx,yy);

}
}
void main()
{
int gdriver=DETECT,gmode,i;
int a[10]={50,80,100,150,200,250,300,340,380,450};
int b[10]={100,50,60,120,140,50,60,160,150,100};
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
cleardevice();
Bspline(a,b);
for(i=0;i<=9;i++)
putpixel(a[i],b[i],15);
getch();
closegraph();
}

9) program implenting bezier curve

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void bezier(float u,int n,int p[4][3]);
float c(int,int);

int x,y,z;
void main()
{
float u;
int gd,gm,ymax,i,n,c[4][3];

for(i=0;i<4;i++)
{
c[i][0]=0; c[i][1]=0;
}
printf("\n\n enter four points : \n\n");
for(i=0; i<4; i++)
{
printf("\t x%dy%d: ",i,i);
scanf("%d %d",&c[i][0],&c[i][1]);
}
c[4][0]=c[0][0];
c[4][1]=c[0][1];
detectgraph(&gd,&gm);

initgraph(&gd,&gm,"c:\\tc\\bgi");
ymax = 480;
setcolor(35);
for(i=0;i<3;i++)
{
line(c[i][0],ymax-c[i][1],c[i+1][0],ymax-c[i+1][1]);
}setcolor(63);
n=3;
for(i=0;i<=40;i++)

{
u=(float)i/40.0;
bezier(u,n,c);
if(i==0)
{ moveto(x,ymax-y);}
else
{ lineto(x,ymax-y); }
getch();
}getch();getch();
}

void bezier(float u,int n,int p[4][3])


{
int j;
float v,b;
float blend(int,int,float);
x=0;y=0;z=0;
for(j=0;j<=n;j++)
{

b=blend(j,n,u);
x=x+(p[j][0]*b);
y=y+(p[j][1]*b);
z=z+(p[j][2]*b);
}
}
float blend(int j,int n,float u)

{
int k;
float v,blend;
v=c(n,j);
for(k=0;k<j;k++)
{ v*=u; }
for(k=1;k<=(n-j);k++)
{ v *= (1-u); }
blend=v;
return(blend);
}

float c(int n,int j)

{
int k,a,c;
a=1;
for(k=j+1;k<=n;k++)
{ a*=k; }
for(k=1;k<=(n-j);k++)

{ a=a/k; }
c=a;
return(c);
}

You might also like