Programs
for
“Computer
Graphics”
SUBMITTED TO- SUBMITTED BY–
PROF.RAJINDER KUMAR Deepak Sharma
ROLL NO-008816219566
BRANCH-CSE-3
SEM-6TH
RIMT POLYTECHNIC COLLEGE
INDEX
PARTICULARS SIGNATURE
1. Write A Program To Print A Line Using Bresenham Algorithm
2. Write A Program To Print A Line Using DDA Algorithm
3. Write A Program To Print A Circle Using Bresenham Algorithm
4. Write Program For Area Filling Using Boundary Fill
5. Write A Program To Perform Mirror Reflection About A Line
6. Write A Program To Implement Line
Clipping Using Cohen Sutherland Algorithm And Mid Point
7. Write A Program To Plot An Arc
8. Write A Program To Draw X-Y Curve
9. Write A Program On Parallel Projection
10. Write A Program To Scaling Of Object
11. Write A Program Move A Character Along X-Axis
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
Write A Program To Print A Line Using Bresenham
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
void main()
{
Clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
float dx,dy,x,y,xend,yend,p,x1,y1,x2,y2;
cout<<”enter the value of x1=”;
cin>>x1;
cout<<”enter the value of y1=”;
cin>>y1;
cout<<”enter the value of x2=”;
cin>>x2;
cout<<”enter the value of y2=”;
cin>>y2;
dx=abs(x1-x2);
dy=abs(y1-y2);
if(dx>=dy)
{
p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
yend=y1;
}
else
{
x=x1;
y=y1;
xend=x2;
yend=y2;
}
putpixel(x,y,3)
for(int i=0;i<dx;i++)
{
x=x+1;
if(p<0)
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
{
p=p+2*dy;
}
else
{
If(yend<y)
{
y=y-1;
}
p=p+2*(dy-dx);
}
putpixel(x,y,3);
}}
else
{
if(dx<dy)
{
p=2*dx-dy;
if(y1>y2)
{
x=x2;
y=y2;
xend=x1;
yend=y1;
}
else
{
x=x1;
y=y1;
xend=x2;
yend=y2;
putpixel(x,y,3);
for(int i=0;i<dy;i++)
{
y=y+1;
if(p<0)
{
p=p+2*dx;
}
else
{
if(xend<x)
{
x=x+1;
}
p=p+2*(dx-dy);
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
}
putpixel(x,y,3);
}}}}
getch();
closegraph();
}
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
OUTPUT
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
Write A Program To Print A Line Using DDA Algorithm
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
voidlinedda(int xa,int ya,int xb,int yb)
{
int dx,dy,steps,k;
float xinc,ynics,x,y;
dx=xb-xa;
dy=yb-ya;
if(abs(dx),abs(dy))
step=abs(dx);
else
steps=abs(dy);
xinc=1.0*dx/steps;
yinc=1.0*dx/steps;
x=xa;
y=ya;
putpixel(int(x),int(y),3);
for(k=1;k<=steps;k++)
{
x+=xinc;
y+=yinc;
putpixel(int(x),int(y),3);
}
}
void main()
{
int x1,y1,x2,y2;
clrscr();
cout<<”Enter the x co-ordinates of first point =”;
cin>>x1;
cout<<”Enter the y co-ordinates of first point =”;
cin>>y1;
cout<<”Enter the x co-ordinates of 2nd point =”;
cin>>x2;
cout<<”Enter the y co-ordinates of 2nd point =”;
cin>>y2;
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
linedda(x1,y1,x2,y2);
getch();
closegraph();
}
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
OUTPUT
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
Write A Program To Print A Circle Using Bresenham
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
void main()
{
Clrscr();
int gdrivers=DETECT,gmodes;
initgraph(&gdrivers,&gmode,”c:\\tc\\bgi”);
float x,y,dx,dy,xend,yend,p,x1,y1,r;
cout<<”enter the value of x=”;
cin>>x;
cout<<”enter the value of y=”;
cin>>y;
cout<<”enter the value of radius=”;
cin>>r;
x=0;
y=r;
p=1-r;
while(x,y)
{
x=x+1;
if(p<0)
{
p=p+2*x+1;
}
Else
{
y=y-1;
p=p+2*(x-y)+1;
}
delay(50);
sound(1000+(x+y));
putpixel(x1+y,y1+x,1);
putpixel(x1+x,y1+y,2);
putpixel(x1+x,y1-y,3);
putpixel(x1-y,y1-x,4);
putpixel(x1-y,y1-x,5);
putpixel(x1-x,y1-y,6);
putpixel(x1-x,y1+y,7);
putpixel(x1-y,y1+x,8);
nosound();
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
}
getch();
closegraph();
}
OUTPUT
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
Write A Program For Area Filling Using Boundary Fill
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(x,y)
int x , y ;
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_right(++x,y);
x=x-1;
fill_right(x,y-1);
fill_right(x,y+1);
}
}
void fill_left(x,y)
int x , y ;
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill_left(--x,y);
x=x+1;
fill_left(x,y-1);
fill_left(x,y+1);
}
}
void main()
{
int x , y ,a[10][10];
int gd, gm ,n,i;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");
for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
printf("\n\n\tEnter the seed pt. : ");
scanf("%d%d",&x,&y);
cleardevice();
setcolor(WHITE);
for(i=0;i<n;i++) /*- draw poly -*/
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
fill_right(x,y);
fill_left(x-1,y);
getch();
}
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
OUTPUT
Program To Perform Mirror Reflection About A Line
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
#include<graphics.h>
#include<iostream.h>
#include<Math.h>
#include<conio.h>
#define MAXSIZE 3
class D_2
{
private:
double Points[MAXSIZE][MAXSIZE];
void Mult(double [MAXSIZE][MAXSIZE]);
void MultTwoMat(double [MAXSIZE][MAXSIZE],double [MAXSIZE][MAXSIZE]);
void Print();
int x,y;
public:
D_2();
void initialize();
void GetPoints();
void Draw(int);
void DrawCord();
void Translate();
void Rotate();
void Reflect();
void Display(double[MAXSIZE][MAXSIZE]);
void Shear();
void Scale_Fixed();
void Scale_Dir();
};
D_2::D_2()
{
for(int i=0;i<MAXSIZE;i++)
{
for(int j=0;j<MAXSIZE;j++)
{
if(i == (MAXSIZE-1))
Points[i][j] = 1;
else
Points[i][j] = 0;
}
}
initialize();
x = getmaxx();
y = getmaxy();
}
void D_2::initialize()
{
int gdrive = DETECT,gmode;
initgraph(&gdrive,&gmode,"c:cgi");
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
}
void D_2::GetPoints()
{
closegraph();
cout<<"Enter The Points Of The Triangle"<<endl;
for(int j=0;j<MAXSIZE;j++)
{
cout<<"Enter Point "<<j+1<<":-"<<endl;
for(int i=0;i<MAXSIZE-1;i++)
{
cout<<"Enter "<<char(i+'X')<<": ";
cin>>Points[i][j];
}
}
initialize();
}
void D_2::Mult(double temp[MAXSIZE][MAXSIZE])
{
int i,j,k;
double z[MAXSIZE][MAXSIZE];
for(i=0;i<MAXSIZE;i++)
{
for(j=0;j<MAXSIZE;j++)
z[i][j]=0;
}
for(i=0;i<MAXSIZE;i++)
{
for(j=0;j<MAXSIZE;j++)
{
for(k=0;k<MAXSIZE;k++)
z[i][j]=z[i][j]+(temp[i][k] * Points[k][j]);
}
}
for(i=0;i<MAXSIZE;i++)
{
for(j=0;j<MAXSIZE;j++)
{
Points[i][j] = z[i][j];
}
}
}
void D_2::Draw(int color)
{
int Poly[2*MAXSIZE+2];
int k = 0;
if(color == GREEN)
DrawCord();
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
for(int j=0;j<MAXSIZE;j++)
{
for(int i=0;i<MAXSIZE-1;i++)
{
if(i==0)
Poly[k++] = x/2+Points[i][j];
else
Poly[k++] = y/2-Points[i][j];
}
}
Poly[k++] = Poly[0];
Poly[k] = Poly[1];
setcolor(color);
drawpoly(4,Poly);
}
void D_2::Display(double Mat[MAXSIZE][MAXSIZE])
{
for(int i=0;i<MAXSIZE;i++)
{
for(int j=0;j<MAXSIZE;j++)
{
cout<<Mat[i][j]<<" ";
}
cout<<" ";
}
}
void D_2::Print()
{
setcolor(GREEN);
setfillstyle(SOLID_FILL,GREEN);
fillellipse(19,36,2,2);
outtextxy(23,34," Original Triangle");
setcolor(MAGENTA);
setfillstyle(SOLID_FILL,MAGENTA);
fillellipse(x-178,y-32,2,2);
outtextxy(x-175,y-34," Tranformed Triangle");
}
void D_2::DrawCord()
{
setcolor(12);
line(x/2,0,x/2,y);
line(0,y/2,x,y/2);
setcolor(10);
setfillstyle(SOLID_FILL,10);
fillellipse(x/2,y/2,2,2);
for(int i=(x/2+50),j=(x/2-50);i<=x,j>=0;i=i+50,j=j-50)
{
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
fillellipse(i,y/2,2,2);
fillellipse(j,y/2,2,2);
}
for(i=(y/2+50),j=(y/2-50);i<=x,j>=0;i=i+50,j=j-50)
{
fillellipse(x/2,i,2,2);
fillellipse(x/2,j,2,2);
}
outtextxy(x/2+3,y/2+4,"0");
outtextxy(x/2+45,y/2+5,"50");
outtextxy(x/2+95,y/2+5,"100");
outtextxy(x/2+145,y/2+5,"150");
outtextxy(x/2+195,y/2+5,"200");
outtextxy(x/2+245,y/2+5,"250");
outtextxy(x/2+295,y/2+5,"300");
outtextxy(x/2-65,y/2+5,"-50");
outtextxy(x/2-115,y/2+5,"-100");
outtextxy(x/2-165,y/2+5,"-150");
outtextxy(x/2-215,y/2+5,"-200");
outtextxy(x/2-265,y/2+5,"-250");
outtextxy(x/2-315,y/2+5,"-300");
outtextxy(x/2+5,y/2+45,"-50");
outtextxy(x/2+5,y/2+95,"-100");
outtextxy(x/2+5,y/2+145,"-150");
outtextxy(x/2+5,y/2+195,"-200");
outtextxy(x/2+5,y/2-50,"50");
outtextxy(x/2+5,y/2-100,"100");
outtextxy(x/2+5,y/2-150,"150");
outtextxy(x/2+5,y/2-200,"200");
}
void D_2::MultTwoMat(double temp[MAXSIZE][MAXSIZE],double
temp1[MAXSIZE][MAXSIZE])
{
int i,j,k;
double z[MAXSIZE][MAXSIZE];
for(i=0;i<MAXSIZE;i++)
{
for(j=0;j<MAXSIZE;j++)
z[i][j]=0;
}
for(i=0;i<MAXSIZE;i++)
{
for(j=0;j<MAXSIZE;j++)
{
for(k=0;k<MAXSIZE;k++)
z[i][j]=z[i][j]+(temp[i][k] * temp1[k][j]);
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
}
}
for(i=0;i<MAXSIZE;i++)
{
for(j=0;j<MAXSIZE;j++)
{
temp1[i][j] = z[i][j];
}
}
}
void D_2::Reflect()
{
double ang;
double a,b,c;
double xr,yr;
double TransMat[MAXSIZE][MAXSIZE];
double RotMat[MAXSIZE][MAXSIZE];
double InvTransMat[MAXSIZE][MAXSIZE];
double InvRotMat[MAXSIZE][MAXSIZE];
double RefMat[MAXSIZE][MAXSIZE];
closegraph();
cout<<"Enter The Line (ax+by+c=0): "<<endl;
cout<<"a: ";
cin>>a;
cout<<"b: ";
cin>>b;
cout<<"c: ";
cin>>c;
if(b!=0)
{
yr = (-c/b);
xr = 0;
double m = -a/b;
ang = atan(m);
}
else
{
yr = 0;
xr = (-c/a);
ang = 22.0/14.0; // Angle = PI/2
}
initialize();
//Transformation Matrix
//Translate arbitrary point to origin then rotate then translate back.
for(int i=0;i<MAXSIZE;i++)
{
for(int j=0;j<MAXSIZE;j++)
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
{
if(i == j)
{
TransMat[i][j] = 1;
InvTransMat[i][j] = 1;
RotMat[i][j] = 1;
InvRotMat[i][j] = 1;
RefMat[i][j] = 1;
}
else
{
TransMat[i][j] = 0;
InvTransMat[i][j] = 0;
RotMat[i][j] = 0;
InvRotMat[i][j] = 0;
RefMat[i][j] = 0;
}
}
}
TransMat[0][2] = -xr;
TransMat[1][2] = -yr;
InvTransMat[1][2] = xr;
InvTransMat[1][2] = yr;
RotMat[0][0] = cos(ang);
RotMat[0][1] = sin(ang);
RotMat[1][0] = -sin(ang);
RotMat[1][1] = cos(ang);
InvRotMat[0][0] = cos(ang);
InvRotMat[0][1] = -sin(ang);
InvRotMat[1][0] = sin(ang);
InvRotMat[1][1] = cos(ang);
for(i=0;i<MAXSIZE;i++)
{
for(int j=0;j<MAXSIZE;j++)
{
if(i==j)
RefMat[i][j] = pow(-1,i)*1;
else
RefMat[i][j] = 0;
}
}
Print();
Draw(GREEN);
MultTwoMat(InvTransMat,InvRotMat);
MultTwoMat(InvRotMat,RefMat);
MultTwoMat(RefMat,RotMat);
MultTwoMat(RotMat,TransMat);
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
Mult(TransMat);
Draw(MAGENTA);
}
void main()
{
D_2 D1;
D1.DrawCord();
getch();
int ch;
D1.GetPoints();
D1.Draw(GREEN);
getch();
do
{
closegraph();
clrscr();
cout<<"1.Reflecting The Triangle About Arbitrary Line."<<endl;
cout<<"2.Exit."<<endl;
cout<<"Enter The Choice: "<<endl;
cin>>ch;
D1.initialize();
switch(ch)
{
case 1:
cleardevice();
D1.Reflect();
getch();
closegraph();
break;
case 2:
return;
default:
cout<<"WRONG CHOICE.";
getch();
break;
}
}while(1);
}
OUTPUT
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
Write A Program To Implement Line Clipping Using Cohen Sutherland
Algorithm And Mid Point
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
int pixels[2][4];
float xn1,xn2,yn1,yn2,x3,y3,m;
void show_quadrant()
{
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(int i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
}
void su_co(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax)
{
int i,j,fl;
for(i=0;i<2;i++)
for(j=0;j<4;j++)
pixels[i][j]=0;
if(y1>ymax)
pixels[0][0]=1;
if(y1<ymin)
pixels[0][1]=1;
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
if(x1>xmax)
pixels[0][2]=1;
if(x1<xmin)
pixels[0][3]=1;
if(y2>ymax)
pixels[1][0]=1;
if(y2<ymin)
pixels[1][1]=1;
if(x2>xmax)
pixels[1][2]=1;
if(x2<xmin)
pixels[1][3]=1;
for(j=0;j<4;j++)
{
if(pixels[0][j]==0&&pixels[1][j]==0)
continue;
if(pixels[0][j]==1&&pixels[1][j]==1)
{
fl=3;
break;
}
fl=2;
}
switch(fl)
{
case 1:
line(320+x1,240-y1,320+x2,240-y2);
break;
case 3:
cout<<"a" "Line Is Not Visible...:-(";
break;
case 2:
m=(y2-y1)/(x2-x1);
xn1=x1;
yn1=y1;
xn2=x2;
yn2=y2;
if(pixels[0][0]==1)
{
xn1=x1+(ymax-y1)/m;
yn1=ymax;
}
if(pixels[0][1]==1)
{
xn1=x1+(ymin-y1)/m;
yn1=ymin;
}
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
if(pixels[0][2]==1)
{
yn1=y1+(xmax-x1)*m;
xn1=xmax;
}
if(pixels[0][3]==1)
{
yn1=y1+(xmin-x1)*m;
xn1=xmin;
}
if(pixels[1][0]==1)
{
xn2=x2+(ymax-y2)/m;
yn2=ymax;
}
if(pixels[1][1]==1)
{
xn2=x2+(ymin-y2)/m;
yn2=ymin;
}
if(pixels[1][2]==1)
{
yn2=y2+(xmax-x2)*m;
xn2=xmax;
}
if(pixels[1][3]==1)
{
yn2=y2+(xmin-x2)*m;
xn2=xmin;
}
line(320+xn1,240-yn1,320+xn2,240-yn2);
break;
}
}
void midpt(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax)
{
int fl=1;
int i,j;
int ox1=x1,ox2=x2,oy1=y1,oy2=y2;
for(i=0;i<2;i++)
for(j=0;j<4;j++)
pixels[i][j]=0;
if(y1>ymax)
pixels[0][0]=1;
if(y1<ymin)
pixels[0][1]=1;
if(x1>xmax)
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
pixels[0][2]=1;
if(x1<xmin)
pixels[0][3]=1;
if(y2>ymax)
pixels[1][0]=1;
if(y2<ymin)
pixels[1][1]=1;
if(x2>xmax)
pixels[1][2]=1;
if(x2<xmin)
pixels[1][3]=1;
for(j=0;j<4;j++)
{
if(pixels[0][j]==0&&pixels[1][j]==0)
continue;
if(pixels[0][j]==1&&pixels[1][j]==1)
{
fl=3;
break;
}
fl=2;
}
switch(fl)
{
case 1:
line(320+x1,240-y1,320+x2,240-y2);
break;
case 3:
cout<<"a" "Line Is Not Visible...:-(";
break;
case 2:
xn1=x1;
yn1=y1;
xn2=x2;
yn2=y2;
fl=0;
x3=x1;
y3=y1;
while(1)
{
if(!(y1>ymax || y1<ymin || x1>xmax || x1<xmin) && (x3 || y3)!=0.1)
break;
x3=(x1+x2)/2;
y3=(y1+y2)/2;
if(!(y3>ymax || y3<ymin || x3>xmax || x3<xmin))
fl=1;
else
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
fl=0;
if(fl)
{
x2=x3;
y2=y3;
}
else
{
x1=x3;
y1=y3;
}
}
xn1=x3;
yn1=y3;
fl=0;
x1=ox1;
x2=ox2;
y1=oy1;
y2=oy2;
x3=x2;
y3=y2;
while(1)
{
if(!(y2>ymax || y2<ymin || x2>xmax || x2<xmin) && (x3 || y3)!=0.1)
break;
x3=(x1+x2)/2;
y3=(y1+y2)/2;
if(!(y3>ymax || y3<ymin || x3>xmax || x3<xmin))
fl=1;
else
fl=0;
if(fl)
{
x1=x3;
y1=y3;
}
else
{
x2=x3;
y2=y3;
}
}
xn2=x3;
yn2=y3;
line(320+xn1,240-yn1,320+xn2,240-yn2);
break;
}
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
void show_message()
{
char *mess[]={"-","=","["," ","L","i","n","e"," ","C","l","i",
"p","p","i","n","g"," ","]","=","-",};
int xx=29,xxx=50,i,j;
setcursortype(_NOCURSOR);
for(i=0,j=21;i<13,j>=11;i++,j--)
{
gotoxy(xx,1);
cout<<mess[i];
xx++;
gotoxy(xxx,1);
cout<<mess[j];
xxx--;
delay(50);
}
setcursortype(_NORMALCURSOR);
}
void main()
{
clrscr();
int gd=DETECT,gm,i,j;
int xmin,ymin,xmax,ymax,x1,y1,x2,y2;
int choice,ed[20],num;
show_message();
cout<<" Enter The Co-Ordinates Of The Clipping Window";
cout<<" Enter X(min) & Y(min) "":=";
cin>>xmin>>ymin;
cout<<" Enter X(max) & Y(max) "":=";
cin>>xmax>>ymax;
cout<<" Enter The Co-Ordinates Of The Line";
cout<<" Enter X(1) & Y(1) "":=";
cin>>x1>>y1;
cout<<" Enter X(2) & Y(2) "":=";
cin>>x2>>y2;
clrscr();
cout<<"1:==>" "Sutherland-Cohen ";
cout<<"2:==>" "Mid-Point Method ";
cout<<"3:==>" "Exit ";
cout<<" Enter Your Choice "":=";
cin>>choice;
switch(choice)
{
case 1:
initgraph(&gd,&gm,"..\bgi");
clearviewport();
show_quadrant();
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
line (320+x1,240-y1,320+x2,240-y2);
getch();
cleardevice();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
su_co(x1,y1,x2,y2,xmin,ymin,xmax,ymax);
getch();
break;
case 2:
initgraph(&gd,&gm,"..\bgi");
clearviewport();
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
line (320+x1,240-y1,320+x2,240-y2);
show_quadrant();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
midpt(x1,y1,x2,y2,xmin,ymin,xmax,ymax);
getch();
break;
case 3:
exit(0);
default:
cout<<"a" "Press A Valid Key...!!! ";
getch();
main();
break;
}
closegraph();}
OUTPUT
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
Write A Program To Plot An Arc
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\turboc");
int x,y,r;
cout<<"Enter the radius of the arc :";
cin>>r;
for(x=0;x<20;x++)
{
y=sqrt(pow(r,2)-pow(x,2));
putpixel(x+200,y+200,1);
}
getch();
closegraph();
}
OUTPUT
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
Write A Program To Draw X-Y Curve
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
# include <stdio.h>
class trans
{
float xm,ym;
int gd,gm;
public:
void graphmode();
void mapgraph();
};
void trans::graphmode()
{
gd=DETECT;
initgraph(&gd,&gm,"d:\\turboc");
}
void trans::mapgraph()
{
xm=getmaxx()/2;
ym=getmaxy()/2;
line(xm,0,xm,2*ym);
line(0,ym,2*xm,ym);
outtextxy(0,ym,"X-AXIS");
outtextxy(xm,0,"Y-AXIS");
}
void main()
{
clrscr();
trans t1;
t1.graphmode();
t1.mapgraph();
getch();
}
OUTPUT
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
Write A Program On Parallel Projection
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
# include <iostream.h>
# include<graphics.h>
# include <conio.h>
# include<math.h>
# define a 0.3
# define b 0.3
# define c 1.0
void show_screen( );
void draw_cube(int [8][3]);
void draw_pyramid(int [5][3]);
void get_projected_point(int&,int&,int&);
void multiply_matrices(const float[4],const float[4][4],float[4]);
void Line(const int,const int,const int,const int);
int main( )
{
int driver=VGA;
int mode=VGAHI;
initgraph(&driver,&mode,"..\\Bgi");
show_screen( );
int cube[8][3]={
{370,200,50}, // front left top
{470,200,50}, // front right top
{470,300,50}, // front right bottom
{370,300,50}, // front left bottom
{370,200,-50}, // back left top
{470,200,-50}, // back right top
{470,300,-50}, // back right bottom
{370,300,-50} // back left bottom
};
setcolor(15);
draw_cube(cube);
settextstyle(0,0,1);
outtextxy(400,330,"Cube");
int pyramid[5][3]={
{120,300,50}, // base front left
{220,300,50}, // base front right
{220,300,-50}, // base back right
{120,300,-50}, // base back left
{170,150,0} // top
};
setcolor(15);
draw_pyramid(pyramid);
settextstyle(0,0,1);
outtextxy(135,330,"Pyramid");
getch( );
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
closegraph( );
return 0;
}
/************************************************************************/
//---------------------------- draw_cube( ) --------------------------//
/************************************************************************/
void draw_cube(int edge_points[8][3])
{
for(int i=0;i<8;i++)
get_projected_point(edge_points[i][0],
edge_points[i][1],edge_points[i][2]);
Line(edge_points[0][0],edge_points[0][1],
edge_points[1][0],edge_points[1][1]);
Line(edge_points[1][0],edge_points[1][1],
edge_points[2][0],edge_points[2][1]);
Line(edge_points[2][0],edge_points[2][1],
edge_points[3][0],edge_points[3][1]);
Line(edge_points[3][0],edge_points[3][1],
edge_points[0][0],edge_points[0][1]);
Line(edge_points[4][0],edge_points[4][1],
edge_points[5][0],edge_points[5][1]);
Line(edge_points[5][0],edge_points[5][1],
edge_points[6][0],edge_points[6][1]);
Line(edge_points[6][0],edge_points[6][1],
edge_points[7][0],edge_points[7][1]);
Line(edge_points[7][0],edge_points[7][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[0][0],edge_points[0][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[1][0],edge_points[1][1],
edge_points[5][0],edge_points[5][1]);
Line(edge_points[2][0],edge_points[2][1],
edge_points[6][0],edge_points[6][1]);
Line(edge_points[3][0],edge_points[3][1],
edge_points[7][0],edge_points[7][1]);
}
/************************************************************************/
//-------------------------- draw_pyramid( ) -------------------------//
/************************************************************************/
void draw_pyramid(int edge_points[5][3])
{
for (int i=0;i<5;i++)
get_projected_point(edge_points[i][0],
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
edge_points[i][1],edge_points[i][2]);
Line(edge_points[0][0],edge_points[0][1],
edge_points[1][0],edge_points[1][1]);
Line(edge_points[1][0],edge_points[1][1],
edge_points[2][0],edge_points[2][1]);
Line(edge_points[2][0],edge_points[2][1],
edge_points[3][0],edge_points[3][1]);
Line(edge_points[3][0],edge_points[3][1],
edge_points[0][0],edge_points[0][1]);
Line(edge_points[0][0],edge_points[0][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[1][0],edge_points[1][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[2][0],edge_points[2][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[3][0],edge_points[3][1],
edge_points[4][0],edge_points[4][1]);
}
/************************************************************************/
//--------------------- get_projected_point( ) -----------------------//
/************************************************************************/
void get_projected_point(int& x,int& y,int& z)
{
float Par_V[4][4]={
{1,0,0,0},
{0,1,0,0},
{(-a/c),(-b/c),0,0},
{0,0,0,1}
};
float xy[4]={x,y,z,1};
float new_xy[4]={0};
multiply_matrices(xy,Par_V,new_xy);
x=(int)(new_xy[0]+0.5);
y=(int)(new_xy[1]+0.5);
z=(int)(new_xy[2]+0.5);
}
/************************************************************************/
//---------------------- multiply_matrices( ) ------------------------//
/************************************************************************/
void multiply_matrices(const float matrix_1[4],
const float matrix_2[4][4],float matrix_3[4])
{
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
for(int count_1=0;count_1<4;count_1++)
{
for(int count_2=0;count_2<4;count_2++)
matrix_3[count_1]+=
(matrix_1[count_2]*matrix_2[count_2][count_1]);
}
}
/*************************************************************************/
//-------------------------- Line( ) ------------------------//
/*************************************************************************/
void Line(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
putpixel(x,y,color);
}
}
else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else
{
x++;
p+=two_dx_dy;
}
putpixel(x,y,color);
}
}
}
/*************************************************************************/
//-------------------------- show_screen( ) ---------------------------//
/*************************************************************************/
void show_screen( )
{
setfillstyle(1,1);
bar(40,26,595,38);
settextstyle(0,0,1);
setcolor(15);
outtextxy(5,5,"****************************************************************************
**");
outtextxy(5,17,"*-
**************************************************************************-*");
outtextxy(5,29,"*--
outtextxy(5,41,"*-
**************************************************************************-*");
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
outtextxy(5,53,"*-
**************************************************************************-*");
outtextxy(45,29,"Orthographics Parallel Projection w.r.t. xy-plane & along a vector v");
setcolor(15);
for(int count=0;count<=30;count++)
outtextxy(5,(65+(count*12)),"*-*
outtextxy(5,438,"*-
**************************************************************************-*");
outtextxy(5,450,"*--------------------------- ------------------------*");
outtextxy(5,462,"**************************************************************************
****");
setcolor(12);
outtextxy(227,450," Press any key to exit ");
}
OUTPUT
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
Write A Program To Scaling Of Objects
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
int x();
int y();
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,”c;\\tc\\bgi”);
int
mx,my,th,I,j,sx,sy,h,k,ka,s,a[3][4],b[3][3],c[3][4];
int tmp,tmp1;
cout<<”enter the co-ordinates of A=”;
cin>>a[0][0]>>a[1][0];
cout<<”enter the co-ordinates of B=”;
cin>>a[0][1]>>a[1][1];
cout<<”enter the co-ordinates of C=”;
cin>>a[0][2]>>a[1][2];
cout<<”enter the co-ordinates of D=”;
cin>>a[0][3]>>a[1][3];
a[2][0]=a[2][1]=a[2][3]=a[2][2]=1;
line(0,my/2,mx,my/2);
line(mx/2,0,mx/2,my);
line(x([0][0]),y(a[1][0],x(a[0][1]),y(a[1][1])); line(x([0][0]),y(a[1][0],x(a[0][3]),y(a[1][3]));
line(x([0][1]),y(a[1][1],x(a[0][2]),y(a[1][2]));
line(x([0][3]),y(a[1][3],x(a[0][2]),y(a[1][2]));
cout<<”enter the value of scaling factor sx,sy=”;
cin>>sx>>sy;
cout<<”enter the value of h and k=”;
cin>>h>>k;
tmp=(-h*sx)+h;
b[0][0]=sx;
b[0][1]=0;
b[0][2]=tmp;
b[1][0]=0;b[1][1]=sy;b[1][2]=temp1;
b[2][0]=0;b[2][2]=0;b[2][2]=1;
for(i=0;i<3;i++)
{
cout<<”\n\n”;
{
for(k=0;k<4;k++)
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
{
c[i][j]=b[i][j]*a[j][k];
}}}
getch();
closegraph();
line(0,my/2,mx,my/2);
line(mx/2,0,mx/2,my);
line(x([0][0]),y(a[1][0],x(a[0][1]),y(a[1][1])); line(x([0][0]),y(a[1][0],x(a[0][3]),y(a[1][3]));
line(x([0][1]),y(a[1][1],x(a[0][2]),y(a[1][2]));
line(x([0][3]),y(a[1][3],x(a[0][2]),y(a[1][2]));
}
int x(int x)
{
return(get max x()/2+x);
}
Int y(int y)
{
return(get max y()/2-y);
}
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581
RIMT POLYTECHNIC COLLEGE
Write A Program Move A Character Along X-Axis
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
int y,x,I;
char z1[100],z2[100];
initgraph(&gd,&gm,””);
outtextxy(100,100,”enter the initial position :”);
scanf(“%d%d”,&x,&y);
outtextxy(110,110,”enter the first string to be moved :”);
scanf(“%s”,z1);
outtextxy(120,120,”enter the second string to be moved :”);
scanf(“%s”,z2);
for(i=x;i<590-x;i++
{
delay(100);
clrscr();
outtextxy(x+1,y,z1);
outtextxy(590-x-I,y,z2);
}
getch();
closegraph();
}
DEEPAK SHARMA, C.S.E-6(C), 6TH SEM, 8581