Parihaspora Pattan, Baramulla, Kashmir
Parihaspora Pattan, Baramulla, Kashmir
CERTIFICATE
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
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");
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");
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");
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
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");
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
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");
x1=x1+tx;
y1=y1+ty;
x2=x2+tx;
y2=y2+ty;
getch();
cleardevice();
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");
x1=x1*Sx;
y1=y1*Sy;
x2=x2*Sx;
y2=y2*Sy;
getch();
cleardevice();
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");
a=a*(pi/180);
a2=abs(x2*cos(a)+y2*sin(a));
b2=abs(x2*sin(a)-y2*cos(a));
getch();
cleardevice();
getch();
cleardevice();
closegraph();
}
Page | 18
Computer Graphics
OUTPUT
PROGRAM No. 8
Page | 19
Computer Graphics
#include <stdio.h>
#include <graphics.h>
#include <math.h>
// initialize graphics
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
xmax = getmaxx();
ymax = getmaxy();
x0 = xmax/2;
y0= ymax/2;
x=0;
y=b;
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;
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();
}
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");
x1=x1+(shx*y1);
y1=y1+(shy*x1);
x2=x2+(shx*y2);
y2=y2+(shy*x2);
getch();
cleardevice();
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");
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);
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