Practical - Programs - Computer Graphics and Multimedia
Practical - Programs - Computer Graphics and Multimedia
#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;
lineDDA(xa,ya,xb,yb);
getch(); closegraph();
}
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;
if(xa<xb)
lineBRESEN(xa,ya,xb,yb);
else
lineBRESEN(xb,yb,xa,ya);
getch();
closegraph();
}
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);
}
}
}
#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;
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);
}
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;
}
}
}
#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();
}
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;
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();
}
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;
if(op==1)
lineT(xa,ya,xb,yb,tx);
else if(op==2)
triangleT(xa,ya,xb,yb,xc,yc,tx);
getch();
closegraph();
getch();
}
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);
#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;
//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;
delay(5000);
rectangle(xa,yb,xb,ya);
setcolor(7);
line(xa,ya,xb,yb);
getch();
closegraph();
}
}
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();
}
#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();
}
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);
}
{
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);
}