0% found this document useful (0 votes)
10 views10 pages

Cga Prac

Uploaded by

rashminisargandh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Cga Prac

Uploaded by

rashminisargandh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

/**1.

Solve the following:


Aim :- Study and enlist the basic functions 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:\\TURBOC3\\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();
}

/**2.
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:\\TURBOC3\\BGI");
xmax=getmaxx();
ymax=getmaxy();
x0=xmax/2;
y0=ymax/2;
line(x0,0,x0,ymax);
line(0,y0,xmax,y0);
getch();
closegraph();
}

/**3.
Solve the following:
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;
int x0,y0,xmax,ymax;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
xmax=getmaxx();
ymax=getmaxy();
x0=xmax/2;
y0=ymax/2;
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();
}

/**4.
Aim :- 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:\\TURBOC3\\BGI");
setcolor(GREEN);
outtextxy(220,50,"MY SWEET HOME");
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
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,152,WHITE);
setfillstyle(HATCH_FILL,MAGENTA);
floodfill(200,105,WHITE);
floodfill(210,105,WHITE);
getch();
closegraph();
}

/**6.
Aim :- Draw the following basic shapes in the center of the screen :
i. Circle
**/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y,r=80;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
x=getmaxx()/2;
y=getmaxy()/2;
outtextxy(x-25,100,"Circle");
circle(x,y,r);
getch();
closegraph();
}

/**
ii. Rectangle
**/
#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,"Rectangle");
rectangle(x-120,y-80,x+120,y+80);
getch();
closegraph();
}

/**
iii. Square
**/
#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,"Square");
bar(x-100,y-100,x+100,y+100);
getch();
closegraph();
}

/**
iv. 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:";//100
cin>>rc;
cout<<"\nEnter the radius of the inner circle:";//50
cin>>rb;
cout<<"\nEnter the centre of both the circles:";//320 240
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();
}

/**
v. 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();
}

/**
vi. 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();
}

/**7.
Solve the following:
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:\\TURBOC3\\BGI");
cout<<"\t***Implementation of DDA line algorithm to draw a straight line***\n\n";
cout<<"Enter the co-ordinate values xa and ya for 1st end point:";//100 200
cin>>xa>>ya;
cout<<"Enter the co-ordinate values xb and yb for 2st end point:";//300 400
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();
}

/**8.
Solve the following:
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:\\TURBOC3\\BGI");
cleardevice();
cout<<"\t***Implementation of Midpoint Circle Algorithm***\n\n";
cout<<"Enter the values of x and y the midpoints:\t";//320 240
cin>>xc>>yc;
cout<<"Enter the value for radius:\t";//100
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();
}

/**9.
Solve the following:
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:\\TURBOC3\\BGI");
cout<<"\t\t\t***Scaling of line***\n\n";
cout<<"\tEnter the coordinates of the line :";//100 200 200 200
cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
cout<<"\nEnter the scaling factor :\n";//2 2
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();
}

/**10.
Aim :- Write a program to perform 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:\\TURBOC3\\BGI");
cout<<"\t\t\t***Translation of line***\n\n";
cout<<"\tEnter the coordinates of the line :";//100 100 200 200
cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
cout<<"\nEnter translation distance :\n";//150 150
cout<<"tx ,ty = ";
cin>>tx>>ty;
x3=x1+tx;
y3=y1+ty;
x4=x2+tx;
y4=x2+ty;
setcolor(YELLOW);
line(x3,y3,x4,y4);
getch();
closegraph();
}

/**11.
Solve the following:
a.
Write a program to fill a rectangle 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==0)
{
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:\\TURBOC3\\BGI");
rectangle(50,50,100,100);
flodfill(51,51,4,0);
getch();
}

/**
(save with “.c”)
12.
Aim :- Write a program to fill a rectangle 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:\\TURBOC3\\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;
}

/**13.
Solve the following:
Aim :- Develop a simple text screen saver using graphics functions.
**/
#include<iostream.h>
#include<stdlib.h>
#include<stdio.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:\\TURBOC3\\BGI");
cleardevice();
while(!kbhit())
{
settextstyle(random(f),random(dir),random(s));
setcolor(random(c));
outtextxy(random(col),random(row),"WELCOME TO COMPUTER GRAPHICS");
delay(250);
}
getch();
closegraph();
}

/**14.
Aim :- Perform smiling face animatiom using graphics function.
**/
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.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:\\TURBOC3\\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;
}

You might also like