Views:
These are the Computer Graphics practical codes for the Computer Engineering students of Mumbai University.
1) DDA line:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<Math.h>
int round(float m);
void DDA(int x1,int x2,int y1,int y2,int color)
{
int dx=x1-x2,dy=y1-y2;
float a=x1,b=y1,steps;
putpixel(round(a),round(b),color);
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
float xinc=dx/float(steps);
float yinc=float(dy)/steps;
for(int i=1;i<=steps;i++)
{
a+=xinc;
b+=yinc;
putpixel(round(a),round(b),color);
}
}
int round(float m)
{return(m+0.5);}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
DDA(50,50,250,350,RED);
DDA(250,250,250,350,GREEN);
DDA(150,250,350,450,RED);
DDA(150,50,350,450,GREEN);
DDA(50,-50,150,250,RED);
DDA(250,350,150,250,GREEN);
DDA(80,80,250,350,RED);
DDA(130,30,320,420,GREEN);
DDA(240,340,190,290,GREEN);
getch();
}
<input type="hidden" name="IL_IN_TAG" value="1"/>
2) Bresenham line:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int sign(int a,int b)
{
if((a-b)<0)
return -1;
else
if((a-b)==0)
return 0;
else
return 1;
}//end sign
void bresen(int x1,int y1,int x2,int y2)
{
int x,y,dx=abs(x2-x1),dy=abs(y2-y1),p,xend,yend;
if(dx>dy)
{
if(x1>x2)
{
x=x2; y=y2;
xend=x1;
}
else
{
x=x1; y=y1;
xend=x2;
}
p=2*dy-dx;
putpixel(x,y,RED);
while(x<xend)
{
x++;
if(p<0)
p=p+2*dy;
else
{
y=y+sign(y2,y1);
p=p+2*(dy-dx);
}
putpixel(x,y,RED);
}//end while
}//end if
else
{
if(y1>y2)
{
y=y2; x=x2;
yend=y1;
}
else
{
y=y1; x=x1;
yend=y2;
}
p=2*dx-dy;
putpixel(x,y,RED);
while(y<yend)
{
y++;
if(p<0)
p=p+2*dx;
else
{
x=x+sign(x2,x1);
p=p+2*(dx-dy);
}
putpixel(x,y,RED);
}//end while
}//end else
}//end bresen
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
bresen(120,50,90,120);
bresen(90,120,10,120);
bresen(10,120,72,185);
bresen(72,185,30,260);
bresen(30,260,120,225);
bresen(120,225,210,260);
bresen(230,120,168,185);
bresen(120,50,150,120);
bresen(230,120,150,120);
bresen(168,185,210,260);
getch();
}
3) Midpoint Circle:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void plot(int x,int y,int xc,int yc)
{
putpixel(xc+x,yc+y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc-x,yc-y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc+y,yc+x,RED);
putpixel(xc-y,yc+x,RED);
putpixel(xc-y,yc-x,RED);
putpixel(xc+y,yc-x,RED);
}
void midpoint(int xc,int yc,int radius)
{
int x=0,y=radius,p=1-radius;
plot(x,y,xc,yc);
while(x<y)
{
x++;
if(p<0)
p=p+2*x+1;
else
{
y--;
p=p+2*(x-y)+1;
}
plot(x,y,xc,yc);
}
}
void main()
{
int gm,get=DETECT;
initgraph(&get,&gm,"C:\\TC\\BGI");
midpoint(100,100,50);
midpoint(180,100,50);
midpoint(260,100,50);
midpoint(340,100,50);
midpoint(420,100,50);
midpoint(420,180,50);
midpoint(420,260,50);
getch();
}
4) Bresenham circle:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void plot(int x, int y,int xc,int yc)
{
putpixel(xc+x,yc+y,22);
putpixel(xc-x,yc+y,22);
putpixel(xc-x,yc-y,22);
putpixel(xc+x,yc-y,22);
putpixel(xc+y,yc+x,22);
putpixel(xc-y,yc+x,22);
putpixel(xc-y,yc-x,22);
putpixel(xc+y,yc-x,22);
}
void bresencircle(int xc,int yc,int radius)
{
int x=0,y=radius,p=3-2*radius;
plot(x,y,xc,yc);
while(x<y)
{
if(p<0)
p=p+4*x+2;
else
{
p=p+4*(x-y)+2;
y--;
}
x++;
plot(x,y,xc,yc);
}
}
void main()
{
int gm,gd=DETECT;
initgraph(&gd,&gm,"C:\\TC\\BGI");
bresencircle(300,230,200);
bresencircle(300,230,5);
setcolor(RED);
line(325,70,325,95);
line(328,70,328,95);
line(325,370,325,345);
line(328,370,328,345);
line(175,220,200,220);
line(175,223,200,223);
line(475,220,450,220);
line(475,223,450,223);
line(325,215,400,200);
line(325,225,400,200);
line(325,225,200,150);
line(325,215,200,150);
getch();
}
5) Bresenham Ellipse:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void plot(long,long,long,long,long);
void Ellipse(long xc,long yc,long rx,long ry,long color)
{
long px,py;
long x=0;
long y=ry;
long p=ry*ry-rx*rx*ry+(1/4)*rx*rx;
px=0;
py=2*rx*rx*ry;
while(px<py)
{
x++;
px=px+2*ry*ry;
if(p<0)
{
p=p+px+ry*ry;
}
else
{py=py-2*rx*rx;
y--;
p=p+px-py+ry*ry;
}
plot (x,y,xc,yc,color);
}
p=ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry;
while(y>0)
{
y--;
py=py-2*rx*rx;
if(p<0)
{x++;
px=px+2*ry*ry;
p=p+px-py+rx*rx;
}
else
{
p=p-py+rx*rx;
}
plot(x,y,xc,yc,color);
}
}
void plot(long x, long y, long xc,long yc,long color)
{
putpixel(xc+x,yc+y,color);
putpixel(xc-x,yc+y,color);
putpixel(xc-x,yc-y,color);
putpixel(xc+x,yc-y,color);
}
void main ()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
Ellipse(250,250,150,80,RED);
Ellipse(420,250,150,80,RED);
Ellipse(330,160,80,150,RED);
Ellipse(330,310,80,150,RED);
getch();
}
6) Boundary fill:
#include<dos.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void drawpolygon(int n,int arr[][2],int color);
void bfill(int x,int y,int fill,int boundary);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int arr[10][2],n,x,y;
cout<<"Enter the number of edges : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter the X and Y position for edge "<<(i+1)<<": ";
cin>>arr[i][0]>>arr[i][1];
}
drawpolygon(n,arr,RED);
cout<<"Enter X and Y co-ordinates of the point from where you want \n";
cout<<"to start BOUNDARY-FILL : ";
cin>>x>>y;
clrscr();
cleardevice();
drawpolygon(n,arr,RED);
bfill(x,y,CYAN,RED);
getch();
closegraph();
restorecrtmode();
}
void drawpolygon(int n,int arr[][2],int color)
{
setcolor(color);
for(int i=0;i<n;i++)
{
if( i == n-1 )
line(arr[i][0],arr[i][1],arr[0][0],arr[0][1]);
else
line(arr[i][0],arr[i][1],arr[i+1][0],arr[i+1][1]);
}
}
void bfill(int x,int y,int fill,int boundary) //boundary=RED
{ //fill=CYAN
int current;
current = getpixel(x+1,y+1);
if( (current != boundary) && (current != fill) )
{
putpixel(x+1,y+1,fill);
bfill(x+1,y,fill,boundary);
bfill(x-1,y,fill,boundary);
bfill(x,y+1,fill,boundary);
bfill(x,y-1,fill,boundary);
}
}
/*Note: preferably, tk input for drawing rectangle or square.... n start filling from top leftmost point... u may require to manipulate the bfill parameters as per d fig u draw! for any doubts... pls prefer to comment in d comment box below */
7) Flood fill:
#include<dos.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void drawpolygon(int n,int arr[][2],int color);
void ffill(int x,int y,int fill,int boundary);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int arr[10][2],n,x,y;
cout<<"Enter the number of edges : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter the X and Y position for edge "<<(i+1)<<": ";
cin>>arr[i][0]>>arr[i][1];
}
drawpolygon(n,arr,RED);
cout<<"Enter X and Y co-ordinates of the point from where you want \n";
cout<<"to start BOUNDARY-FILL : ";
cin>>x>>y;
clrscr();
cleardevice();
drawpolygon(n,arr,RED);
ffill(x,y,CYAN,RED);
getch();
closegraph();
restorecrtmode();
}
void drawpolygon(int n,int arr[][2],int color)
{
setcolor(color);
for(int i=0;i<n;i++)
{
if( i == n-1 )
line(arr[i][0],arr[i][1],arr[0][0],arr[0][1]);
else
line(arr[i][0],arr[i][1],arr[i+1][0],arr[i+1][1]);
}
}
void ffill(int x,int y,int fill,int boundary) //boundary=RED
{ //fill=CYAN
int current;
current = getpixel(x+1,y+1);
if( (current != boundary) && (current != fill) )
{
putpixel(x+1,y+1,fill);
ffill(x-1,y,fill,boundary);
ffill(x+1,y,fill,boundary);
ffill(x,y-1,fill,boundary);
ffill(x,y+1,fill,boundary);
ffill(x+1,y+1,fill,boundary);
ffill(x-1,y+1,fill,boundary);
ffill(x-1,y-1,fill,boundary);
ffill(x+1,y-1,fill,boundary);
}
}
/*Note: preferably, tk input for drawing rectangle or square.... n start filling from top leftmost point... u may require to manipulate the ffill parameters as per d fig u draw! for any doubts... pls prefer to comment in d comment box below */
8) Translation:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
class transform
{
int in[21][3],scalm[3][3],trans[3][3],result[21][3],tx,ty,e;
public:
void plot(float z[21][3]);
void plot(int z[21][3]);
void accept();
void translate();
void mul(int x[21][3],float y[3][3],float z[21][3]);
void mul(int x[21][3],int y[3][3],int z[21][3]);
};
void transform::mul(int x[21][3],int y[3][3],int z[21][3])
{
int i;
for(i=1;i<=e;i++)
{
z[i][1]=(x[i][1]*y[1][1])+(x[i][2]*y[2][1]);
z[i][2]=(x[i][1]*y[1][2])+(x[i][2]*y[2][2]);
}
}
void transform::mul(int x[21][3],float y[3][3],float z[21][3])
{
int i;
for(i=1;i<=e;i++)
{
z[i][1]=(x[i][1]*y[1][1])+(x[i][2]*y[2][1]);
z[i][2]=(x[i][1]*y[1][2])+(x[i][2]*y[2][2]);
}
}
void transform::plot(float z[21][3])
{
int i;
setcolor(RED);
int x=getmaxx()/2;
int y=getmaxy()/2;
line(0,y,2*x,y);
line(x,0,x,2*y);
for(i=1;i<e;i++)
line(z[i][1],z[i][2],z[i+1][1],z[i+1][2]);
line(z[1][1],z[1][2],z[i][1],z[i][2]);
}
void transform::plot(int z[21][3])
{
int i;
setcolor(RED);
int x=getmaxx()/2;
int y=getmaxy()/2;
line(0,y,2*x,y);
line(x,0,x,2*y);
for(i=1;i<e;i++)
line(z[i][1],z[i][2],z[i+1][1],z[i+1][2]);
line(z[1][1],z[1][2],z[i][1],z[i][2]);
getch();
}
void transform::accept()
{
int i,j;
cleardevice();
cout<<"Enter number of edges ";
cin>>e;
cout<<"Enter co-ordinates ";
for(i=1;i<=e;i++)
for(j=1;j<=2;j++)
cin>>in[i][j];
for(i=1;i<=e;i++)
{
in[i][1]=in[i][1]+320;
in[i][2]=240-in[i][2];
}
cleardevice();
plot(in);
for(i=1;i<=e;i++)
{
in[i][1]=in[i][1]-320;
in[i][2]=240-in[i][2];
}
}
void transform::translate()
{
int i,j;
accept();
cout<<"\nEnter tx factor ";
cin>>tx;
cout<<"\nEnter ty factor ";
cin>>ty;
tx=320-tx;
ty=ty+240;
for(i=1;i<=e;i++)
{
in[i][1]=in[i][1]+640-tx;
in[i][2]=480-in[i][2]-ty;
}
plot(in);
}
void main()
{
int get=DETECT,gm;
initgraph(&get,&gm,"C:\\TC\\BGI");
transform t;
t.translate();
}
9) Reflection:
#include <iostream.h>
#include <conio.h>
#include <process.h>
#include <graphics.h>
#include <math.h>
class transform
{
private:
int input[20][2],scalm[2][2];
int ref[2][2];
int resm[20][2];
int edges;
public:
void accept();
void reflect();
void multiply(int a[20][2],int b[2][2],int c[20][2]);
void multiplyxx(int a[20][2],int b[2][2],int c[20][2],int edges);
void plot(int mat[20][2]);
void plot1(float mat[20][2]);
};
void transform::accept()
{
int i,j;
cleardevice();
cout<<"Enter the number of edges in the figure : ";
cin>>edges;
cout<<"Enter the Co-ordinates in Matrix form : ";
for(i=1;i<=edges;i++)
{
for(j=1;j<=2;j++)
{
cout<<"\nA["<<i<<"]["<<j<<"] = ";
cin>>input[i][j];
}
}
for(i=1;i<=edges;i++)
{
input[i][1] = input[i][1] + 320;
input[i][2] = 240 - input[i][2];
}
plot(input); // Displaying the accepted polygon
for(i=1;i<=edges;i++)
{
input[i][1] = input[i][1] - 320;
input[i][2] = 240 - input[i][2];
}
}
void transform::reflect()
{
int choice,i;
cleardevice();
cout<<"\t\tMenu"
<<"\n1> Reflection About Y Axis"
<<"\n2> Reflection About X Axis"
<<"\n3> Reflection About Origin"
<<"\n4> Reflection About y = x Line"
<<"\n5> Reflection About y = -x Line"
<<"\nEnter Your Choice\t";
cin>>choice;
switch(choice)
{
case 1: // Matrix for reflection about Y-axis
ref[1][2] = ref[2][1] = 0;
ref[1][1] =-1;
ref[2][2] = 1;
break;
case 2: // Matrix for reflection about X-axis
ref[1][2] = ref[2][1] = 0;
ref[1][1] = 1;
ref[2][2] =-1;
break;
case 3: // Matrix for reflection about origin
ref[1][2] = ref[2][1] = 0;
ref[1][1] =-1;
ref[2][2] =-1;
break;
case 4: // Matrix for reflection about Y = X line
ref[1][2] = ref[2][1] = 1;
ref[1][1] = 0;
ref[2][2] = 0;
break;
case 5: // Matrix for reflection about Y = -X line
ref[1][1] =0;
ref[2][2] =0;
ref[1][2] = ref[2][1] =-1;
break;
}
accept(); // Accept the oroginal polygon
multiply(input,ref,resm);
for(i=1;i<=edges;i++)
{
resm[i][1] = resm[i][1] + 320;
resm[i][2] = 240 - resm[i][2];
}
plot(resm);
}
void transform:: multiply(int a[20][2],int b[2][2], int c[20][2])
{
int i;
for(i=1;i<=edges;i++)
{
c[i][1]=(a[i][1]*b[1][1])+(a[i][2]*b[2][1]);
c[i][2]=(a[i][1]*b[1][2])+(a[i][2]*b[2][2]);
}
}
void transform:: multiplyxx(int a[20][2],int b[2][2], int c[20][2],int edges)
{
int i;
for(i=1;i<=edges;i++)
{
c[i][1]=(a[i][1]*b[1][1])+(a[i][2]*b[2][1]);
c[i][2]=(a[i][1]*b[1][2])+(a[i][2]*b[2][2]);
}
}
void transform::plot(int mat[20][2])
{
int i;
setcolor(RED);
line(0,240,639,240);
line(320,0,320,479);
outtextxy(295,243,"0,0");
setcolor(RED);
for(i=1;i<edges;i++)
line(mat[i][1],mat[i][2],mat[i+1][1],mat[i+1][2]);
line(mat[1][1],mat[1][2],mat[i][1],mat[i][2]);
getch();
}
void transform::plot1(float mat[20][2])
{
int i;
setcolor(RED);
line(0,240,639,240);
line(320,0,320,479);
outtextxy(295,243,"0,0");
setcolor(RED);
for(i=1;i<edges;i++)
{
line(mat[i][1],mat[i][2],mat[i+1][1],mat[i+1][2]);
getch();
}
line(mat[1][1],mat[1][2],mat[i][1],mat[i][2]);
}
int main(void)
{
char ch;
int gd=DETECT,gm,i;
do
{
initgraph(&gd,&gm,"C:\\TC\\BGI");
transform t;
t.reflect();
closegraph();
cout<<"Do you want to continue(y/n)?\t";
ch=getch();
}
while (ch=='y'||ch=='Y');
return 0;
}
Note:1) the programs given r as per d list provided by CR on 3d May 2011
2) Ur suggestions r welcome 4 further development of filling programs.
All the best...!!!!