SSM COLLEGE OF ENGINEERING AND TECHNOLOGY
PARIHASPORA PATTAN, BARAMULLA, KASHMIR
CERTIFICATE
Name: Cirhan Yaqoob Semester: 7th
Enroll: 5787 University Roll No: 15205135006
This is certified to be the bona fide work of the student in the Computer Graphics lab
during academic session 2019.
____________________ ____________________
Teacher Incharge HOD
INDEX
S.NO PRACTICAL PAGE NO. REMARKS
01. Write a program to draw a line using DDA’s 1-2
line drawing algorithm
02. Write a program to draw a line 3-5
using
Bresenham’s line drawing algorithm
03. Write a program to draw a circle 6-8
using
Bresenham’s circle drawing algorithm
04. Write a program to draw a circle using 9-11
midpoint circle drawing algorithm
05. Write a program to translate a line 12-14
about the origin
06. Write a program to scale a line about 15-17
the origin
07. Write a program to rotate a line about 18-19
a fixed point taken as one of the
endpoint of the line
08. Write a program to draw an ellipse 20-22
09. Write a program of 2D Shearing 23-24
10. Write a program of 2D Reflection 25-27
Computer Graphics
PROGRAM No. 1
Write a program to draw a line using DDA’s line drawing
algorithm
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
void main( )
{
float dx,dy,step;
int x,y,x1,y1,x2,y2,i,gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the value of x1 and y1 : ");
scanf("%d%d",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%d%d",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
{
putpixel(x,y,WHITE);
x=x+dx;
y=y+dy;
i=i+1;
}
getch();
closegraph();
OUTPUT
Page | 1
Computer Graphics
Page | 2
Computer Graphics
PROGRAM NO.2
Write a program to draw a line using Bresenham’s line
drawing algorithm
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
void main( )
{
float dx,dy,step;
int x,y,x1,y1,x2,y2,i,gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the value of x1 and y1 : ");
scanf("%d%d",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%d%d",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
p=2*dy-dx;
i=1;
do
{
putpixel(x,y,WHITE);
x+=1;
if(p<0)
{
p=p+2*dy;
}
else
{
p=p+2*dy-2*dx;
}
i=i+1;
} while(i<=dx);
getch();
Page | 3
Computer Graphics
closegraph();
}
Page | 4
Computer Graphics
OUTPUT
Page | 5
Computer Graphics
PROGRAM NO. 3
Write a program to draw a circle using Bresenham’s circle
drawing algorithm
#include <stdio.h>
#include <graphics.h>
#include <math.h>
void main( )
{
float p;
int i,gd=DETECT,gm,x,y,r;
// initialize graphics
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the radius of circle: ");
scanf("%d",&r);
x=0;
y=r;
p=3-2*r;
do
{
putpixel(200+x, 200+y, 15);
putpixel(200+y, 200+x, 15);
putpixel(200+x, 200-y, 15);
putpixel(200+y, 200-x, 15);
putpixel(200-x, 200-y, 15);
putpixel(200-x, 200+y, 15);
putpixel(200-y, 200+x, 15);
putpixel(200-y, 200-x, 15);
x+=1;
if(p < 0)
{
p=p+4*x+2;
}
else
{
y-=1;
p=p+4*(x-y)+2;
Page | 6
Computer Graphics
} while(x < y);
getch();
closegraph();
}
Page | 7
Computer Graphics
OUTPUT
Page | 8
Computer Graphics
PROGRAM NO. 4
Write a program to draw a circle using midpoint circle
drawing algorithm
#include <stdio.h>
#include <graphics.h>
#include <math.h>
void main( )
{
float p;
int i,gd=DETECT,gm,x,y,r;
// initialize graphics
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the radius of circle: ");
scanf("%d",&r);
x=0;
y=r;
p=1.25-r;
do
{
putpixel(200+x, 200+y, 15);
putpixel(200+y, 200+x, 15);
putpixel(200+x, 200-y, 15);
putpixel(200+y, 200-x, 15);
putpixel(200-x, 200-y, 15);
putpixel(200-x, 200+y, 15);
putpixel(200-y, 200+x, 15);
putpixel(200-y, 200-x, 15);
x+=1;
if(p < 0)
{
p=p+2*x+1;
}
else
{
y-=1;
p=p+2*(x-y)+1;
Page | 9
Computer Graphics
} while(x < y);
getch();
closegraph();
}
Page | 10
Computer Graphics
OUTPUT
Page | 11
Computer Graphics
PROGRAM NO. 5
Write a program to translate a line about the origin
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void main( )
{
int tx,ty;
int x1,y1,x2,y2,i,gd=DETECT,gm;
// initialize graphics
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the coordinates (x1,y1): ");
scanf("%d%d",&x1,&y1);
printf("Enter the coordinates (x2,y2): ");
scanf("%d%d",&x2,&y2);
printf("Enter the x-translation factor: ");
scanf("%d",&tx);
printf("Enter the y-translation factor: ");
scanf("%d",&ty);
outtextxy(10,100,"Line Before Translation");
line(x1,y1,x2,y2);
x1=x1+tx;
y1=y1+ty;
x2=x2+tx;
y2=y2+ty;
getch();
cleardevice();
outtextxy(10,100,"Line After Translation");
line(x1,y1,x2,y2);
getch();
cleardevice();
closegraph();
}
Page | 12
Computer Graphics
OUTPUT
Page | 13
Computer Graphics
Page | 14
Computer Graphics
PROGRAM NO. 6
Write a program to scale a line about the origin
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void main( )
{
float Sx,Sy;
int x1,y1,x2,y2,i,gd=DETECT,gm;
// initialize graphics
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the coordinates (x1,y1): ");
scanf("%d%d",&x1,&y1);
printf("Enter the coordinates (x2,y2): ");
scanf("%d%d",&x2,&y2);
printf("Enter the x-scaling factor: ");
scanf("%f",&Sx);
printf("Enter the y-scaling factor: ");
scanf("%f",&Sy);
outtextxy(10,100,"Line Before Scaling");
line(x1,y1,x2,y2);
x1=x1*Sx;
y1=y1*Sy;
x2=x2*Sx;
y2=y2*Sy;
getch();
cleardevice();
outtextxy(10,100,"Line After Scaling");
line(x1,y1,x2,y2);
getch();
cleardevice();
closegraph();
}
Page | 15
Computer Graphics
OUTPUT
Page | 16
Computer Graphics
Page | 17
Computer Graphics
PROGRAM No. 7
Write a program to rotate a line about a fixed point taken as
one of the endpoint of the line
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void main( )
{
float a,pi=22/7;
int x1,y1,x2,y2,a2,b2,i,gd=DETECT,gm;
// initialize graphics
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the coordinates (x1,y1): ");
scanf("%d%d",&x1,&y1);
printf("Enter the coordinates (x2,y2): ");
scanf("%d%d",&x2,&y2);
printf("Enter the angle of rotation(in degrees): ");
scanf("%f",&a);
outtextxy(10,100,"Line Before Rotation");
line(x1,y1,x2,y2);
a=a*(pi/180);
a2=abs(x2*cos(a)+y2*sin(a));
b2=abs(x2*sin(a)-y2*cos(a));
getch();
cleardevice();
outtextxy(10,100,"Line After Rotation");
line(x1,y1,a2,b2);
getch();
cleardevice();
closegraph();
}
Page | 18
Computer Graphics
OUTPUT
PROGRAM No. 8
Page | 19
Computer Graphics
Write a program to draw an ellipse using mid-point ellipse
drawing algorithm
#include <stdio.h>
#include <graphics.h>
#include <math.h>
void disp(int x0, int y0, int x, int y);
void main( )
{
int i,gd=DETECT,gm,a,b,aSq,bSq,dx,dy,xmax,ymax,x0,y0;
float x,y,p1,p2;
// initialize graphics
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the x-radius of ellipse: ");
scanf("%d",&a);
printf("Enter the y-radius of ellipse: ");
scanf("%d",&b);
xmax = getmaxx();
ymax = getmaxy();
x0 = xmax/2;
y0= ymax/2;
x=0;
y=b;
disp(x0, y0, x, y);
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x+=1;
if(p1<=0)
{
p1=p1+(2.0*b*b*x)+(b*b);
}
else
{
y-=1;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
Page | 20
Computer Graphics
}
disp(x0, y0, x, y);
}
x=a;
y=0;
disp(x0, y0, x, y);
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y+=1;
if(p2>0)
{
p2=p2+(a*a)-(2.0*a*a*y);
}
else
{
x-=1;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp(x0, y0, x, y);
}
getch();
closegraph();
}
void disp(int x0, int y0, int x, int y)
{
putpixel(x0+x, y0+y, 15);
putpixel(x0-x, y0-y, 15);
putpixel(x0+x, y0-y, 15);
putpixel(x0-x, y0+y, 15);
}
Page | 21
Computer Graphics
OUTPUT
Page | 22
Computer Graphics
PROGRAM No. 9
Write a program of 2D Shearing
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void main( )
{
float shx,shy;
int x1,y1,x2,y2,i,gd=DETECT,gm;
// initialize graphics
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the coordinates (x1,y1): ");
scanf("%d%d",&x1,&y1);
printf("Enter the coordinates (x2,y2): ");
scanf("%d%d",&x2,&y2);
printf("Enter the x-shearing factor: ");
scanf("%f",&shx);
printf("Enter the y-shearing factor: ");
scanf("%f",­);
outtextxy(10,100,"Line Before Shearing");
line(x1,y1,x2,y2);
x1=x1+(shx*y1);
y1=y1+(shy*x1);
x2=x2+(shx*y2);
y2=y2+(shy*x2);
getch();
cleardevice();
outtextxy(10,100,"Line After Shearing");
line(x1,y1,x2,y2);
getch();
cleardevice();
closegraph();
}
Page | 23
Computer Graphics
OUTPUT
Page | 24
Computer Graphics
PROGRAM No. 10
Write a program of 2D Reflection
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
void main( )
{
float dx,dy,step;
int a1,b1,a2,b2,x0,y0,x1,y1,x2,y2,ch,gd=DETECT,gm,xmax,ymax;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the value of x1 and y1 : ");
scanf("%d%d",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%d%d",&x2,&y2);
xmax = getmaxx();
ymax = getmaxy();
x0 = xmax/2;
y0= ymax/2;
a1=x1+x0;
b1=y1+y0;
a2=x2+x0;
b2=y2+y0;
line(x0,0,x0,ymax);
line(0,y0,xmax,y0);
line(a1,b1,a2,b2);
printf("1. Origin\n2. X-axis\n3. Y-axis\n4. Exit");
do
{
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
line(x0-x1,y0-y1,x0-x2,y0-y2);
break;
case 2:
Page | 25
Computer Graphics
line(a1,y0-y1,a2,y0-y2);
break;
case 3:
line(x0-x1,b1,x0-x2,b2);
break;
}
} while(ch!=4);
closegraph();
}
Page | 26
Computer Graphics
OUTPUT
Page | 27