PRACTICAL FILE
SUBJECT: Computer Graphics
MAHARAJA SURAJMAL INSTITUTE (2014-2017)
Submitted to:
Mrs. Pooja Singh
Submitted by:
KUNAL GUPTA
Roll No.: 03114902014
Course: BCA ( Ist Shift)
Batch:2014-17
PRACTICAL FILE
SUBJECT: Computer Graphics
MAHARAJA SURAJMAL INSTITUTE (2014-2017)
Submitted to:
Mrs. Pooja Singh
Submitted by:
ANUJ KHATRI
Roll No.: 30514902014
Course: BCA ( Ist Shift)
Batch:2014-17
PRACTICAL FILE
SUBJECT: Computer Graphics
MAHARAJA SURAJMAL INSTITUTE (2014-2017)
Submitted to:
Mrs. Pooja Singh
Submitted by:
Himanshu sharma
Roll No.: 02514902014
Course: BCA ( Ist Shift)
Batch:2014-17
[Link] the following stationary objects:
a) A Hut
b) A Face
c) Character (of your choice)
Ans.
a) A Hut:
#include<graphics.h>
#include<conio.h>
int main()
{
int gd = DETECT,gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);
line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);
setfillstyle(SOLID_FILL, BROWN);
floodfill(152, 182, WHITE);
floodfill(252, 182, WHITE);
setfillstyle(SLASH_FILL, BLUE);
floodfill(182, 252, WHITE);
setfillstyle(HATCH_FILL, GREEN);
floodfill(200, 105, WHITE);
floodfill(210, 105, WHITE);
getch();
closegraph();
return 0;
}
Output:
b) A Face
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main()
{
int gd=DETECT,gm;
int color,pixel,maxx,maxy;
initgraph(&gd,&gm,"C:\\TC\\BGI");
setbkcolor(DARKGRAY);
maxx=getmaxx();
maxy=getmaxy();
setcolor(YELLOW);
circle(maxx/2,maxy/2,20);
setfillstyle(1,YELLOW);
fillellipse(maxx/2,maxy/2,100,100);
pixel=getpixel(1,1);
setfillstyle(1,pixel);
setcolor(pixel);
fillellipse(maxx/2-50,maxy/2-30,10,10);
fillellipse(maxx/2+50,maxy/2-30,10,10);
ellipse(maxx/2,maxy/2,220,320,60,60);
line(maxx/2,maxy/2-10,maxx/2,maxy/2+20);
getch();
closegraph();
}
Output:
[Link] the DDA line drawing Algorithm.
Ans.
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include<conio.h>
int main( )
{
float x,y,x1,y1,x2,y2,dx,dy,pixel;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"C://TurboC3//BGI");
printf("Enter the value of x1 : ");
scanf("%f",&x1);
printf("Enter the value of y1 : ");
scanf("%f",&y1);
printf("Enter the value of x2 : ");
scanf("%f",&x2);
printf("Enter the value of y2: ");
scanf("%f",&y2);
clrscr();
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
pixel=dx;
else
pixel=dy;
dx=dx/pixel;
dy=dy/pixel;
x=x1;
y=y1;
i=1;
while(i<=pixel)
{
putpixel(x,y,WHITE);
x=x+dx;
y=y+dy;
i=i+1;
}
getch();
closegraph();
}
Output:
[Link] Bresenhams Line Drawing Algorithm.
Ans.
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd=DETECT,gm;
clrscr();
printf("\n\n\tEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
Output:
[Link] Bresenhams Circle Algorithm.
Ans.
# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=240;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
cleardevice();
printf("Enter the radius ");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=3-(2*r);
for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);
}
getch();
closegraph();
}
Output:
[Link] the ellipse drawing algorithm.
Ans.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm,a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("*** Ellipse Generating Algorithm ***\n");
printf("Enter the value of Xc\t");
scanf("%d",&xc);
printf("Enter the value of yc\t");
scanf("%d",&yc);
printf("Enter X axis length\t");
scanf("%d",&a);
printf("Enter Y axis length\t");
scanf("%d",&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
delay(50);
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
delay(50);
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,7);
putpixel(xc-x,yc+y,7);
putpixel(xc+x,yc-y,7);
putpixel(xc+x,yc-y,7);
}
Output:
6. Program to show the changing radius of a circle
Ans: #include <stdio.h>
#include <graphics.h>
int main()
{
int gd=DETECT,gm;
int x,y,p,xc ,yc ,radius=80;
int i;
initgraph(&gd, &gm, NULL);
xc = getmaxx()/2;
yc = getmaxy()/2;
for(i=0;i<10;i++){
x=0;
y=radius;
putpixel(xc+x,yc-y,1);
p=3-(2*radius);
for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,WHITE);
}
radius+=10;
delay(1000);
cleardevice();
}
for(i=0;i<10;i++){
x=0;
y=radius;
putpixel(xc+x,yc-y,1);
p=3-(2*radius);
for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,WHITE);
}
radius-=10;
delay(1000);
cleardevice();
}
getch();
closegraph();
return 0;
}
Output: