Professional Documents
Culture Documents
Name: Sahbiya.Mohd.Saleem.Khan
Rollno: 18011 Semester:- Fourth
Subject: Computer Graphics and Animation
Academic: SY-BScIT
Professor-in-Charge:- Miss Rani Mario
1.drawpoly() function which is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon
etc
Code:-
#include<graphics.h>
#include<conio.h>
void main()
int gd=DETECT,gm,
initgraph(&gd,&gm,"C:\TC\BIN");
drawpoly(4, arr);
getch();
closegraph();
return 0;
Output:-
Conclusion:-
The above program has been executed successfully.
2)
fillpoly() function which is used to draw and fill a polygon i.e. triangle, rectangle, pentagon,
hexagon etc. It require same arguments as drawpoly().
Code:-
#include<graphics.h>
#include<conio.h>
void main()
int arr[]={320,150,400,250,250,350,320,150};
initgraph(&gd,&gm,"C:\TC\BIN");
fillpoly(4,arr);
getch();
closegraph();
return 0;
Output:-
Conclusion:-
The above program has been executed successfully.
3)
setfillstyle() function which sets the current fill pattern and fill color.
floodfill() function is used to fill an enclosed area. Current fill pattern and fill color is used to fill
the area.
Code:-
#include<graphics.h>
#include<conio.h>
int main()
initgraph(&gd,&gm,"C:\TC\BIN");
int radius=100;
setfillstyle(HATCH_FILL,RED);
circle(x_circle,y_circle,radius);
floodfill(x_circle,y_circle,border_color);
getch();
closegraph();
return 0;
Output:-
4
getx() function which returns the X coordinate of the current position.
Code:-
1b)
Aim:- Draw a co-ordinate axis at the centre of the screen.
Code:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
clrscr();
Int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode,=C:\\TC\\BGI=);
int xmax, ymax;
xmax = getmaxx();
ymax = getmaxy();
Iine(xmax/2, 0, xmax/2,ymax);
Iine(0, ymax/2, xmax, ymax/2);
getch();
closegraph();
}
Output:-
Screenshot:-
Conclusion:-
The above program has been executed successfully.
Screenshot:-
2b)
Aim:- Draw a simple hut on the screen.
Code:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
clrscr();
int gd=DETECT, gm;
initgraph(&gd,&gm,=C:\TC\BIN=);
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,RED);
floodfill(152,182,WHITE);
floodfill(252,182,WHITE);
setfillstyle(SLASH_FILL,BLUE);
floodfill(182,252,WHITE);
stylefillstyle(HATCH_FILL,GREEN);
floodfill(200,105,WHITE);
floodfill(210,105,WHITE);
getch();
closegraph();
return 0;
}
Output:-
Screenshot:-
Practical no 3:-Draw the following basic shapes in the centre of the screen:-
Aim:-
I.Circle
II.Rectangle
III.Square
IV.Concentric Circles
V.Ellipse
VI.Line
Code:-
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
Int main(void)
{
int gd = DETECT,gm,errorcode;
int midx,midy,eft,top,right,bottom;
int radius=100;
initgraph(&gd, &gm, <C:\TC\BIN=);
printf(<%d=,getmaxx());
midx = getmaxx() / 2;
midy = getmaxy() /2;
circle(midx,miy,radius);
getch();
left= getmaxx() / 2 –50;
top =getmaxy() /2 - 50;
right = getmaxx() / 2 + 50;
bottom = getmaxy() /2+50
rectangle(left,top,right,bottom);
getch();
rectangle(midx-20 , midy-20, midx+20, midy+20);
getch();
ellipse(midx,midy,0,360,100,50);
getch();
closegraph();
return 0;
)
Output:-
Screenshot:-
dx=(x2-x1);
dy=(y2-y1);
if(abs(dx)>=abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step
x=x1;
y=y1;
putpixel(x,y,4);
for(i=0;i<step;i++0
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,4);
}
getch();
closegraph();
}
Output: -
Screenshot:-
Conclusion:-
The above program has been executed successfully.
4b)
Aim:-Develop the program forBresenham’s Line drawing algorithm.
Code:-
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
int x1,y1,x2,y2,dx,dy,p;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\BIN");
cout<<"enter the values of x1,y1"<<endl;
cin>>x1>>y1;
cout>>x2>>y2;
dx=x2-x1;
dy=y2-1;
p=2*dy-dx;
putpixel(x1,y1,3);
for(int k=0;k<dx;k++)
{
if(p<0)
{
x1++;
p=p+2*dy;
}
else
{
x1++;
y1++;
p=p+2*dy-2*dx;
}
putpixel(x1,y1,3);
delay(50);
}
getch();
closegraph();
}
Output:-
Screenshot:-
Conclusion:-
The above program has been executed successfully.
Output:-
Screenshot:-
Conclusion:-
The above program has been executed successfully.
5b)
Aim:-Develop the program for the mid-point ellipse drawing algorithm.
Code:-
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float xc,yc,rx,ry,x,y,pk,p1k,p1k;p2k;
initgraph(&gd,&gm,=c:\\tc\\bin=);
scanf(<%f %f= , &rx,&ry);
printf(<\nenter centre of ellipse=);
scanf(<%f %f= , &rx,&ry);
x=0; y=ry;
pk=(ry*ry)-(rx*rx*ry)+((rx*rx*)/4);
while((2*ry*ry*x)<(2*rx*rx*y))
{
If (pk<=0)
{
x=x+1;
p1k=pk+(2*ry*ry*x)-(2*rx*rx*y)+(ry*ry);
}
else
{
x=x+1;
y=y+1;
p1k=pk+(2*ry*ry*x)-(2*rx*rx*y)+(ry*ry);
}
pk=p1k;
putpixel(xc+x,yc+y,WHITE)
putpixel(xc-x,yc+y,WHITE)
putpixel(xc+x,yc-y,WHITE)
putpixel(xc-x,yc-y,WHITE)
}
pk=((x-0.5)*(x+0.5)*ry*ry)+((y-1)*(y-1)*rx*rx)-(rrx*rx*ry*ry);
while(y>0)
{
If(pk>0)
{
y+y-1;
p1k=pk-(2*rx*rx*y)+(rx*rx);
}
pk=p1k;
putpixe(xc+x,yc+y,WHITE);
putpixe(xc-x,yc+y,WHITE);
putpixe(xc+x,yc-y,WHITE);
putpixe(xc-x,yc-y,WHITE);
}
line(xc+rx,yc,xc-rx,yc);
line(xc,yc+ry,xc,yc-ry);
getch();
Output:-
Screenshot:-
Conclusion:-
The above program has been executed successfully.
Output:-
Screenshot:-
Conclusion:-
The above program has been executed successfully.
6b)
Aim:- Write a program to perform 2d translation.
Code:-
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int gd=DETECT,gm;
int n,xs[100],ys[100],i,ty,tx;
void draw();
void translate();
void main()
{
printf(<Enter number of sides of polygon: <);
scanf(<%d=,&n);
printf(<Enter co-ordinates: x,y for each vertex=);
for(i=0;i<n;i++)
Scanf(8%d%d=, &xs[i],&ys[i]);
printf(<Enter distances for translation (in x and y directions <);
scanf(%d%d=,&tx,&ty);
initgraph(&gd,&gm,=C:\TC\BIN\=);
cleardevice();
setcolor(RED);
draw();
translate();
setcolor(YELLOW);
draw();
getch();
}
void draw()
{
for(i=0;i<n;i++)
line(xs[i],ys[i],xs[(i+1)%n],ys[(i+1)%n]);
}
void translate()
{
for(i=0;i<n;i++)
{
xs[i]+=tx;
ys[i]+=ty;
}
Output:-
Screenshot:-
Conclusion:-
The above program has been executed successfully.
Screenshot:-
Conclusion:-
The above program has been executed successfully.
7b)
Aim:- Program to create a house like figure and perform the following operations
i.Scaling about the origin followed by translation.
ii.Scaling with reference to an arbitrary point.
iii.Reflect about the line y=mx+c.
Code:-
#include<stdio.h>
#include<graphic.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
}
}
void translate (int h[][2], int dx, int dy)
{
int i;
for (i=0; i<9; i++)
{
h[i][0] += dx;
h[i][1] +=dy;
}
}
void reflect (int h[][2], int m, int c)
{
int i;
float angle;
for (i=0; i<9; i++)
h[i][1]-= c;
angle = M_P1/2 - atan (m);
rotate (h,angle);
for(i=0;i<9;i++)
h[i][1] += c;
}
void ini()
{
int gd=DETECT, gm;
initgraph(&gd,&gm,=C://TC//BIN=);
}
void dini()
{
getch();
closegraph();
}
void main()
{
Int h[9][2],sx,sy,x,y,m,c,choice;
Do
{
clrscr();
printf(<1. Scaling about the origin. \n=);
printf(<2. Scaling about the origin. \n=);
printf(<3. Scaling about the origin. \n=);
printf(<4. Scaling about the origin. \n=);
printf(<Enter the choice: =);
scanf(<%d=,&choice);
switch(choice)
{
Output:-
Screenshot:-
Conclusion:-
The above program has been executed successfully.
cout<<=after clipping=;
If(accept)
cleardevice();
rectanglr(xmin,ymin,xmax,ymax);
setcolor(RED);
line(x1,y1,x2,y2);
getch();
closegraph();
}
Output:-
Screenshot:-
Conclusion:-
The above program has been executed successfully.
8b)
Aim:- Write a program to implement Liang Barsky line Clipping Algorithm
Code:-
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
Int a[4],b[4];
float m,xnew,ynew;
float x1=100,y1=100,xh=300,yh=300,xa=10,ya=200,xb=250,yb=150;
int gd = DETECT,gm;
initgraph(&gd,&gm,=C:\TC\BIN=);
setcolor(5);
line(xa,ya,xb,yb);
setcolor(12);
rectangle(x1,y1,xh,yh);
m = (yb-ya)/(xb-xa);
If(xa < x1)
a[2] = 1;
else a[3] =0;
If (xa>xh)
a[2] = 1;
else a[2]=0;
If(xb <x1)
b[3] = 1;
else b[3] = 0;
if(xb>xh)
b[2] = 1;
else b[2] = 0;
printf("no clipping");
line(xa,ya,xb,yb);
}
else if(a[0]&&b[0] || a[1]&&b[1] || a[2]&&b[2] || a[3]&&b[3])
{
clrscr();
printf("line discarded");
rectangle(xl,yl,xh,yh);
}
else
{
if(a[3] == 1 && b[3]==0)
{
ynew = (m * (xl-xa)) + ya;
setcolor(12);
rectangle(xl,yl,xh,yh);
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xl,ynew,xb,yb);
}
else if(a[2] == 1 && b[2] == 0)
{
ynew = (m * (xh-xa)) + ya;
setcolor(12);
rectangle(xl,yl,xh,yh);
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xl,ynew,xb,yb);
}
else if(a[1] == 1 && b[1] == 0)
{
xnew = xa + (yl-ya)/m;
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xnew,yh,xb,yb);
}
Output:-
Screenshot:-
Conclusion:-
The above program has been executed successfully.
Screenshot:-
Conclusion:-
The above program has been executed successfully.
9b)
Aim:-Write a program to fill a circle using Boundary Fill Algorithm.
Code:-
#include<graphics.h>
#include<conio.h>
void boundaryFill(int x,int y, int newColor, int oldColor)
{
if((getpixel(x,y)!=newColor) && (getpixel(x,y)!=oldColor))
{
setcolor(newColor);
putpixel(x,y,newColor);
delay(5);
boundaryFill(x+1,y,newColor,oldColor);
boundaryFill(x-1,y,newColor,oldColor);
boundaryFill(x,y+1,newColor,oldColor);
boundaryFill(x,y-1,newColor,oldColor);
}
{
setcolor(newColor);
putpixel(x,y);
}
}
Void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,=C:\\TC\\BIN=);
setcolor(10);
circle(250,200,30);
boundaryFill(251,201,12,10);
getch();
closegraph();
}
}
Output:-
Screenshot:-
Conclusion:-
The above program has been executed successfully.
void main(){
int gdriver=DETECT,gmode,col=480,row=640,font=4,direction=2,size=8,color=15;
initgraph(&gdriver,&gmode,"C:\\TC\\BIN");
cleardevice();
while(!kbhit()){
settextstyle(random(font),random(direction),random(size));
setcolor(random(color));
outtextxy(random(col),random(row),"five finger");
delay(250);
}
closegraph();
}
Output:-
Screenshot:-
Conclusion:-
The above program has been executed successfully.
10b)
Screenshot:-
Conclusion:-
The above program has been executed successfully.
10c)
Aim:-Draw the moving car on the screen.
Code:-
#include<iostream.h>
#include<conio.h>
#include<graphic.h>
#include<dos.h.
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,=C://TC//BIN=)
line(0,400,639,390);
for(int i=0;i<500:i++)
{
line(50 +i,370,90 +i,370);
arc(110 +i,370,0,180 +i,20);
line(130 +i,370,220 +i,370);
arc(240 +i,370,0,180 +i,20);
line(260 +i,370,300 +i,370);
line(300 +i,370,300 +i,330);
line(300 +i,350,240 +i,300);
line(240 +i,330,220 +i,300);
line(200 +i,300,110 +i,300);
line(11 0 +i,300,80 +i,330);
line(80 +i;330,50 +i,340);
line(50+i,340,50 +i,370);
circle(110 +i,370,17);
circle(240 +i,370,17);
delay(10);
cleardevice();
}
getch();
Output:-
Screenshot:-
Conclusion:-
The above program has been executed successfully.