Practical :02
Aim:-study and enlist the basic function used for Graphics in C/C++/Python language .give an example for each of them:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd= DETECT,gm,points[]={320,150,420,300,250,300,320,150};
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(50,50,90,50);
arc(100,100,210,330,50);
circle(70,250,50);
drawpoly(4,points);
setcolor(RED);
circle(50,420,40);
floodfill(50,420,RED);
setcolor(WHITE);
outtext("press any key to clear the screen..");
getch();
cleardevice();
outtext("press any key to exit..");
getch();
closegraph();
}
------------------[practical 3]------------------------
Aim:-Draw a co-ordinate axis at the center of the screen
#include<iostream.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd,gm;
int x0,y0,xmax,ymax;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
xmax=getmaxx();
ymax=getmaxy();
x0=xmax/2;
y0=ymax/2;
line(x0,0,x0,ymax);
line(0,y0,xmax,y0);
getch();
closegraph();
}
----------------------[practical 4]---------------------
Aim:-Divide your screen into four region ,draw circle ,rectangle ,ellipse , and half ellipse in each region with appropriate message
#include<iostream.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd,gm;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(320,0,320,480);
line(0,240,640,240);
circle(125,100,50);
outtextxy(90,180,"Circle");
rectangle(400,150,550,50);
outtextxy(450,180,"Rectangle");
arc(100,320,200,340,50);
outtextxy(70,390,"Arc");
ellipse(450,350,0,360,100,50);
outtextxy(450,430,"Ellipse");
getch();
closegraph();
}
-----------------------------------------[practical5]-------------------------------
Draw a simple Hut on the screen :-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
clrscr();
initgraph(&gd
,&gm,"c:\\tc\\bgi");
setcolor(GREEN);
outtextxy(220,50,"MY SWEET HOME");
setcolor(WHITE);
rectangle(150,180,250,400);
rectangle(250,180,420,400);
rectangle(280,210,310,230);
rectangle(350,210,380,230);
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,BLUE);
floodfill(152,182,WHITE);
floodfill(252,182,WHITE);
setfillstyle(SLASH_FILL,RED);
floodfill(182,252,WHITE);
setfillstyle(HATCH_FILL,YELLOW);
floodfill(200,105,WHITE);
floodfill(210,105,WHITE);
getch();
closegraph();
}
---------------------------------[practical6]-------------------------
Draw the following basic shapes int he center of the screen,
(1)
circle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y,r=80;
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=getmaxx()/2;
y=getmaxy()/2;
outtextxy(x-25,100,"Circle");
circle(x,y,r);
getch();
closegraph();
}
(2) Rectangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y;
intitgraph(&gd,&gm,"c:\\tc\\bgi");
x=getmaxx()/2;
y=getmaxy()/2;
outtextxy(x-25,100,"Rectangle");
rectangle(x-120,y-80,x+120,y+80);
getch();
closegraph();
}
(3)Square:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y;
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=getmaxx()/2;
y=getmaxy()/2;
outtextxy(x-25,100,"Square");
bar(x-100,y-100,x+100,y+100);
getch();
closegraph();
}
(4) Concentric Circles
#include<iostream.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,rc,rb,xc,yc,i;
float x,y;
initgraph(&gd,&gm,"c:\\TURBOC3\\bgi");
cout<<"enter the radius of the outer circle:";
cin>>rc;
cout<<"enter the radius of the inner circle:";
cin>>rb;
cout<<"\n Enter the centre of both the circles:";
cin>>xc>>yc;
outtextxy(250,110,"Concentric Circles");
for(i=1;i<=360;i++)
{
x=xc+(rb*(cos(i)));
y=yc+(rb*(sin(i)));
putpixel(x,y,4);
}
for(i=1;i<=360;i++)
{
x=xc+(rc*(cos(i)));
y=yc+(rc*(sin(i)));
putpixel(x,y,4);
}
getch();
closegraph();
}
(5) Ellipse:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y;
initgraph(&gd,&gm,"c:\\TURBOC3\\bgi");
x=getmaxx()/2;
y=getmaxy()/2;
outtextxy(x-25,100,"Ellipse");
ellipse(x,y,0,360,130,80);
getch();
closegraph();
}
(6) Line:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y;
initgraph(&gd,&gm,"c:\\TURBOC3\\bgi");
x=getmaxx()/2;
y=getmaxy()/2;
outtextxy(x-25,100,"Line");
line(x-150,y-100,x+100,y+80);
getch();
closegraph();
}
-----------------------------[practical7]--------------------------
Aim:-Develop the program for DDA line drawing algorithm:-
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int xa,xb,ya,yb,dx,dy,steps,k;
float xinc,yinc,x,y;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"\t **Implementation of DDA line algorithm to draw a straigth line\n\n";
cout<<"Enter the coordinates values xa and ya for 1st end point:";
cin>>xa>>ya;
cout<<"\n Enter the coordinates values of xb and yb for 2nd end point";
cin>>xb>>yb;
dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xinc=dx/steps;
yinc=dy/steps;
x=xa;
y=ya;
putpixel(x,y,14);
for(k=1;k<=steps;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,14);
delay(100);
}
getch();
closegraph();
}
-----------------------------------[practical 8]---------------------------
Aim:-Develop the program for the mid-point circle drawing algorithm.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,x,y,r,xc,yc;
float d;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
cout<<"\t **Implementation of mid-point circle algorithm\n\n";
cout<<"enter the values of x and y the midpoints:\t";
cin>>xc>>yc;
cout<<"Enter the value of radius:\t";
cin>>r;
x=0;
y=r;
d=(5/4)-r;
do
{
putpixel(xc+x,yc+y,13);
putpixel(xc+y,yc+x,13);
putpixel(xc+y,yc-x,13);
putpixel(xc+x,yc-y,13);
putpixel(xc-x,yc-y,13);
putpixel(xc-y,yc-x,13);
putpixel(xc-y,yc+x,13);
putpixel(xc-x,yc+y,13);
if(d<=0)
d=d+2*x+3;
else
{
d=d+2*(x-y)+5;
y=y-1;
}
x=x+1;
delay(50);
}
while(x<y);
getch();
closegraph();
}
------------------[practical9]----------------------
Aim:-Write a program to implement 2D scaling.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3,x4,y4;
float sx,sy;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"\t\t\t**Scaling of aline ***\n\n";
cout<<"\tEnter the coordinate of the line:";
cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
cout<<"\n EWnter the scaling factor:\n";
cout<<"sx,sy=";
cin>>sx>>sy;
x3=(int)x1*sx;
y3=(int)y1*sy;
x4=(int)x2*sx;
y4=(int)x2*sy;
setcolor(YELLOW);
line(x3,y3,x4,y4);
getch();
closegraph();
}
//100 200 200 200
//2 2
-------------------------[practical 10]--------------------
Aim:-Write a program to implement 2D translation.:-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3,x4,y4,tx,ty;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"\t\t\t**Translation of aline\n\n";
cout<<"\t enter the coordinates of the line:";
cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
cout<<"\n enter translation distance:\n";
cout<<"tx,ty=";
cin>>tx>>ty;
x3=x1+tx;
y3=y1+ty;
x4=x2+tx;
y4=x2+ty;
setcolor(RED);
line(x3,y3,x4,y4);
getch();
closegraph();
}
-----------------------------[practical 11]------------------
Aim:-Write a program to fill a circle using Flood Fill Algorithm:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flodfill(int x,int y,int f,int o)
{
int c;
c=getpixel(x,y);
if(c==o)
{
setcolor(f);
putpixel(x,y,f);
delay(10);
flodfill(x+1,y,f,o);
flodfill(x,y+1,f,o);
flodfill(x+1,y+1,f,o);
flodfill(x-1,y-1,f,o);
flodfill(x-1,y,f,o);
flodfill(x,y-1,f,o);
flodfill(x-1,y+1,f,o);
flodfill(x+1,y-1,f,o);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(50,50,100,100);
flodfill(51,51,4,0)
getch();
}
-------------------------------[practical 12]--------------------------
Aim:-Write a program to fill a circle using Boundary Fill Algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
setbkcolor(14);
rectangle(50,50,100,100);
boundary(59,59,6,15);
getch();
closegraph();
}
boundary(int x,int y,int fillcolor, int backcolor)
{
if(getpixel(x,y)!=backcolor&&getpixel(x,y)!=fillcolor)
{
putpixel(x,y,fillcolor);
boundary(x+1,y,fillcolor,backcolor);
boundary(x,y+1,fillcolor,backcolor);
boundary(x+1,y+1,fillcolor,backcolor);
boundary(x-1,y-1,fillcolor,backcolor);
boundary(x-1,y,fillcolor,backcolor);
boundary(x,y-1,fillcolor,backcolor);
boundary(x+1,y-1,fillcolor,backcolor);
boundary(x-1,y+1,fillcolor,backcolor);
}
return 0;
}
---------------------------[practical13]----------------------
Aim:-Develop a simple text screen saver using graphics function:-
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,col=480,row=640,f=4,dir=2,s=8,c=15;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
while(!kbhit())
{
settextstyle(random(f),random(dir),random(s));
setcolor(random(c));
outtextxy(random(col),random(row),"welcome to computer graphics");
delay(0);
}
getch();
closegraph();
}
----------------------[practical 14]---------------------
Aim:-Perform smiling face animation using graphics function.
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
main()
{
int gd=DETECT,gm,area,temp1,temp2,left=25,top=75;
void*p;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
setcolor(14);
circle(50,100,25);
setfillstyle(SOLID_FILL,14);
floodfill(50,100,14);
setcolor(BLACK);
setfillstyle(SOLID_FILL,WHITE);
fillellipse(44,85,2,6);
fillellipse(56,85,2,6);
ellipse(50,100,205,335,20,9);
ellipse(50,100,205,335,20,10);
ellipse(50,100,205,335,20,11);
area=imagesize(left,top,left+50,top+50);
p=malloc(area);
setcolor(15);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(200,450,"Smiling Face Animation");
while(!kbhit())
{
temp1=1+random(588);
temp2=1+random(380);
getimage(left,top,left+50,top+50,p);
putimage(left,top,p,XOR_PUT);
putimage(temp1,temp2,p,XOR_PUT);
delay(500);
left=temp1;
top=temp2;
}
getch();
closegraph();
return 0;
}
Comments