CG Practical File
CG Practical File
PRACTICAL FILE
“COMPUTER GRAPHICS”
Submitted by
GUNJAN KUMARI M
BCA 5th SEM.
35196702020
(Batch 2020-2023)
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h> void
main()
}
Output:-
Program No. 2:- Write a program 3 pixels with delay.
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h> void
main()
int g=DETECT,d,x,y,x1,y1,x2,y2;
initgraph(&g,&d,"c:\\bgi");
putpixel(x,y,10); delay(200);
putpixel(x1,y1,20); delay(400);
putpixel(x2,y2,30); delay(400);
getch(); closegraph();
}
Output:-
Program No. 3:- Write a program to draw a line through DDA algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
gdriver=DETECT,gmode,i;
initgraph(&gdriver,&gmode,"c:\\bgi");
len=abs(x2-x1); if(abs(y2-y1)>len)
{
len=abs(y2-y1);
incx=(x2-x1)/len;
incy=(y2-y1)/len;
for(i=1;i<=len;i++)
{
putpixel(x,y,9);
x=x+incx; y=y+incy;
delay(100);
getch(); closegraph();
Output:-
Program No. 4:- Write a program to draw a line through BREHESEM line
algorithm.
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
int gd=DETECT,gm;
int x1,x2,y1,y2,dx,dy,i,flag,d,x,y,t,s1,s2;
clrscr(); initgraph(&gd,&gm,"c:\\bgi");
scanf("%d",&y2); dx=abs(x2-x1);
s1=1; else
s1=-1; if((y2-
y1)>0) s2=1;
else s2=-1;
if(dy>dx)
{
t=dx;
dx=dy; dy=t;
flag=1;
outtextxy(x1,y1,"(x1,y1)");
outtextxy(x2,y2,"(x2,y2)");
i=1; a:
putpixel(x,y,9);
delay(40);
while(d>=0)
{
if(flag==1)
x=x+s1; else
y=y+s1; d=d-
2*dx;
if(flag==1)
y=y+s2; else
x=x+s2;
d=d+2*dy;
i++;
if(i<=dx) goto
a; getch();
closegraph();
}
Output:-
Program No. 5:- Write a program to make a triangle using 3 lines.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h> void
main()
int gdriver=DETECT,gmode,x,y,x1,y1,x2,y2;
initgraph(&gdriver,&gmode,"c:\\bgi");
line(100,100,160,160); line(100,100,150,90);
Output:-
s
Program No. 6:- Write a program to perform stranslation of a line.
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdio.h> void
main()
{
int x1,y1,x2,y2,tx,ty; int g=DETECT,d;
initgraph(&g,&d,"c:\\bgi"); printf("enter
the value of x1:"); scanf("%d",&x1);
printf("enter the value of y1:");
scanf("%d",&y1); printf("enter the value
of x2:"); scanf("%d",&x2); printf("enter
the value of y2:"); scanf("%d",&y2);
line(x1,y1,x2,y2); getch(); cleardevice();
printf("enter the value of translation\n");
printf("enter tha value of tx");
scanf("%d",&tx); printf("enter tha value of
ty"); scanf("%d",&ty); printf("\n The
translated object is:\n");
line(x1+tx,y1+ty,x2+tx,y2+ty); getch();
closegraph();
}
Output:-
Program No. 7:- Write a program to perform a translation of triangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
line(x1,y1,x2,y2);
x3=x2-100; line(x2,y2,x3,y2);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x2+tx,y2+ty,x3+ty,y2+ty);
line(x3+tx,y2+ty,x1+tx,y1+ty); getch();
closegraph();
}
Output:-
Program No. 8:- Write a program to perform a translation of a rectangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h> void
main()
{
int x1,y1,x2,y2,tx,ty; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates of x1:");
scanf("%d",&x1); printf("Enter the
coordinates of y1:"); scanf("%d",&y1);
printf("Enter the coordinates of x2:");
scanf("%d",&x2); printf("Enter the
coordinates of y2:"); scanf("%d",&y2);
rectangle(x1,y1,x2,y2); getch();
cleardevice(); printf("Enter the value for
Translation:\n"); printf("Enter the value of
tx:"); scanf("%d",&tx); printf("Enter the
value of ty:"); scanf("%d",&ty);
printf("Translated object is:");
rectangle(x1,y1+ty,x2+tx,y2+ty); getch();
closegraph();
}
Output:-
Program No. 9:- Write a program to perform a rotation of Line.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h> void
main()
{
int x1,y1,x2,y2,rx,ry; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates of x1:");
scanf("%d",&x1); printf("Enter the
coordinates of y1:"); scanf("%d",&y1);
printf("Enter the coordinates of x2:");
scanf("%d",&x2); printf("Enter the
coordinates of y2:"); scanf("%d",&y2);
line(x1,y1,x2,y2); getch(); cleardevice();
printf("Enter the value for Translation\n");
printf("Enter the value of rx:\n");
scanf("%d",&rx);
printf("Enter the value of ry:\n");
scanf("%d",&ry); printf("Rotated
object is:"); line(x1,y1,rx,ry);
getch(); closegraph();
}
Output:-
Program No. 10:- Write a program to perform a rotation of triangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
{
int x1,x2,y1,y2,x3,a; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
line(x1,y1,x2,y2); x3=x2-100;
line(x2,y2,x3,y2); line(x3,y2,x1,y1);
getch();
rotation\n"); scanf("%d",&a);
a=a*(3.14/180); x1=((x1*cos(a))-
(y1*sin(a))); y1=((x1*sin(a))+
(y1*cos(a))); x1=((x2*cos(a))-
(y2*sin(a))); x1=((x2*sin(a))+
(y2*cos(a))); line(x1,y1,x2,y2);
line(x2,y2,x3,y2); line(x3,y2,x1,y1);
getch(); closegraph();
}
Output:-
Program No. 11:- Write a program to perform a rotation of a rectangle.
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
{
int x1,x2,y1,y2,x3,a; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates for x1:");
scanf("%d",&x1); printf("Enter the
coordinates for y1:"); scanf("%d",&y1);
printf("Enter the coordinates for x2:");
scanf("%d",&x2); printf("Enter the
coordinates for y2:"); scanf("%d",&y2);
rectangle(x1,y1,x2,y2); getch();
cleardevice(); printf("Enter the angle for
rotation\n"); scanf("%d",&a);
a=a*(3.14/180); x1=((x1*cos(a))-
(y1*sin(a))); y1=((x1*sin(a))+
(y1*cos(a))); x1=((x2*cos(a))-
(y2*sin(a))); x1=((x2*sin(a))+
(y2*cos(a))); rectangle(x1,y1,x2,y2);
getch(); closegraph();
}
Output:-
s
Program No. 12:- Write a program to perform a scalene of triangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
{
int x1,x2,y1,y2,x3,y3,x4,tx,ty; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates of x1:");
scanf("%d",&x1); printf("Enter the
coordinates of y1:"); scanf("%d",&y1);
printf("Enter the coordinates of x2:");
scanf("%d",&x2); printf("Enter the
coordinates of y2:"); scanf("%d",&y2);
line(x1,y1,x2,y2); x3=x2-100;
line(x1,y1,x3,y2); line(x3,y2,x2,y2); getch();
cleardevice(); printf("\nEnter the value for
SCALLING:"); printf("\nEnter the value of
tx:"); scanf("%d",&tx); printf("\nEnter the
value of ty:"); scanf("%d",&ty);
printf("\nThe Scaled object is:");
line(x1,y1,x2+tx,y2);
line(x1,y1,x3-ty,y2); line(x3-
tx,y2,x2+tx,y2); getch();
closegraph();
}
Output:-
Program No. 13:- Write a program to perform a scalene of rectangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
{
int x1,x2,y1,y2,tx,ty; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates of x1:");
scanf("%d",&x1); printf("Enter the
coordinates of y1:"); scanf("%d",&y1);
printf("Enter the coordinates of x2:");
scanf("%d",&x2); printf("Enter the
coordinates of y2:"); scanf("%d",&y2);
rectangle(x1,y1,x2,y2); getch();
cleardevice(); printf("Enter the values for
SCALING:\n") printf("Enter the value of
tx:") scanf("%d",&tx); printf("Enter the
vaue of ty:"); scanf("%d",&ty);
printf("Scaled object is:");
rectangle(x1,y1+ty,x2+tx,y2+ty); getch();
closegraph();
}
Output:-
Program No. 14:- Write a program to perform a shearing of triangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
int x1,x2,y1,y2,x3,x4,y3,tx,ty; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates of x1:");
scanf("%d",&x1);
printf("Enter the coordinates of x2:"); scanf("%d",&x2);
printf("Enter the coordinates of y1:"); scanf("%d",&y1);
printf("Enter the coordinates of y2:");
scanf("%d",&y2);
line(x1,y1,x2,y2);
x3=x2-100;
line(x2,y2,x3,y2);
line(x3,y2,x1,y1);
getch(); cleardevice();
printf("Enter the value for shearing:\n");
printf("Enter the value of tx:");
scanf("%d",&tx); printf("Enter the value
of ty:"); scanf("%d",&ty); printf("Sheared
object is:"); line(x1+tx,y1+ty,x2,y2);
line(x2,y2,x3,y2);
line(x3,y2,x1+tx,y1+ty); getch();
closegraph();
}
Output:-
s
Program No. 15:- Write a program to perform a shearing of rectangle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
int x1,x2,y1,y2,tx,ty; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates of x1:");
scanf("%d",&x1);
printf("Enter the coordinates of y1:"); scanf("%d",&x2);
printf("Enter the coordinates of x2:"); scanf("%d",&y1);
printf("Enter the coordinates of y2:");
scanf("%d",&y2); rectangle(x1,y1,x2,y2);
getch(); cleardevice();
printf("Enter the values or shearing:\n");
printf("enter value of tx:");
scanf("%d",&tx); printf("enter
value of ty:"); scanf("%d",&ty);
printf("sheared object is:");
rectangle(x1,y1,x2+tx,y2+ty);
getch();
closegraph();
}
Output:-
s
Output:-
Output:-
s
Program No. 18:- Write a program to make a kite.
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
line(100,100,50,180);
line(100,100,150,180);
line(50,180,100,250);
line(150,180,100,250);
line(100,100,100,250);
line(50,180,150,180);
line(100,250,70,300);
line(100,250,130,300);
line(70,300,130,300);
line(100,300,120,320);
line(120,320,80,340);
line(80,340,120,360);
line(120,360,80,380); setcolor(4);
getch();
closegraph();
}
Output:-
s
Program No. 19:- Write a program to draw a circle using MidPoint
Algorithm.
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdio.h>
void plotpixel (int x,int y,int x1,int y1);
void main()
{
int x,y,x1,y1,p,r; int
g=DETECT,d;
initgraph(&g,&d,"c:\\bgi");
printf("enter the coordinates of x1,y1,&radius");
scanf("%d%d%d%",&x1,&y1,&r);
x=0;
y=5;
plotpixel(x,y,x1,y1);
p=1-r;
while(x<y)
{
if(p<0)
{
p=p+(2*x)+3; x+
+;
plotpixel(x,y,x1,y1);
}
else
{
p=p+2*(x-y)+5;
x++;
y--;
plotpixel(x,y,x1,y1);
}
}
getch();
closegraph();
}
void plotpixel(int x,int y, int x1, int y1)
{
putpixel(x1+x,y+y1,2); putpixel(x1-
x,y1+y,2); putpixel(x1+x,y1-y,2);
putpixel(x1-x,y1-y,2); putpixel(x1+y,y1+x,2);
putpixel(x1-y,y1+x,2); putpixel(x1+y,y1-x,2);
putpixel(x1-y,y1-x,2);
}
Output:-
Program No. 20:- Write a program to draw a circle using BRESENHEM’S
Algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
x,int y)
putpixel(x+h,y+k,7);
putpixel(y+h,x+k,7);
putpixel(-y+h,x+k,7);
putpixel(-x+h,y+k,7);
putpixel(-x+h,-y+k,7);
putpixel(-y+h,-x+k,7);
putpixel(y+h,-x+k,7);
putpixel(x+h,-y+k,7);
void bren_circle(int r)
int x=0,y=r,a=3-(2*r);
while(x<=y)
delay(100);
if(a<0) a=a+(4*x)
+6; else
a=a+(4*(x-y))+10;
y--;
x++; plotpixel(x,y);
void main()
int gd=DETECT,gm,r;
initgraph(&gd,&gm,"c:\\bgi");
bren_circle(r); getch();
}
Output:-
Program No. 21:- Write a program to draw a FLAG.
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
{
int g=DETECT,d;
initgraph(&g,&d,"c:\\bgi");
setcolor(5);
line(20,50,20,180);
line(20,50,100,50);
line(20,70,100,70);
line(20,90,100,90);
line(20,110,100,110);
line(100,50,100,110);
circle(60,80,10);
line(52,80,68,80);
line(60,72,60,88);
setfillstyle(SOLID_FILL,22);
floodfill(21,51,5);
setfillstyle(SOLID_FILL,7);
floodfill(21,75,5);
setfillstyle(SOLID_FILL,2);
floodfill(21,95,5);
setfillstyle(SOLID_FILL,7);
floodfill(81,75,5);
setfillstyle(SOLID_FILL,7);
floodfill(61,81,5); getch();
closegraph();
}
Output:-
Program No. 22:- Write a program to draw a SMILEY.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h> void
main()
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\bgi");
circle(100,100,50);
circle(80,80,10);
circle(120,80,10);
line(100,90,100,115);
arc(100,115,180,360,15);
getch(); closegraph();
}
Output:-