0% found this document useful (0 votes)
85 views22 pages

Graphics

Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views22 pages

Graphics

Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Graphics

Programs

WAP to draw concentric circles with different colors


Program:

#include<graphics.h>
#include<conio.h>
#include<stdio.h>

void main()
{
clrscr();
int clr=1,rad=10;
int gd=DETECT,gm;
initgraph(&gd , &gm , " ");
for(rad=10;rad<=100;rad=rad+20)
{
setcolor(clr);
clr++;
circle(getmaxx()/2,getmaxy()/2,rad);
}
getch();
}

Output:

WAP to make a pie chart


Program:

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#define MAX 5

void main()
{
int gd=DETECT,gm,total=0;
int ea,sa,p[MAX],mul[MAX];
float div;
printf("\nEnter Data:");
for(int i=0;i<MAX;i++)
{
scanf("%d",&p[i]);
total=total+p[i];
}
div=360.0/total*1.0;
for(i=0;i<MAX;i++)
{
mul[i]=p[i]*div;
}
sa=0;
ea=mul[0];
initgraph(&gd , &gm , " ");
setcolor(0);
for(i=0;i<MAX;i++)
{

setfillstyle(SOLID_FILL,i+1);
pieslice(getmaxx()/2,getmaxy()/2,sa,ea,200);
sa=ea;
ea=ea+mul[i+1];
}
getch();
}

Output:

WAP to Show the working of a Clock


#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<conio.h>
#include<stdio.h>
main()
{
int gd=DETECT,gm,i,p;
char str[10];
struct time t;
int orx=290,ory=30;
int lsec=150,lmin=145,lhr=120;
double hang=0,mang=0,sang=0;
initgraph(&gd,&gm,"d:\\turboc3");
circle(300,200,195);
for(i=1;i<=12;i++)
{
setlinestyle(SOLID_FILL,0,3);

line(300+170*cos(2*M_PI/12*i),200+170*sin(2*M_PI/12*i),300+165*cos(2*M_PI/12*i),200+165*sin(2
*M_PI/12*i));
}
while(!kbhit())
{
setcolor(getbkcolor());

setlinestyle(SOLID_LINE,0,3);
line(300,200,300+lsec*cos(sang),200+lsec*sin(sang));
line(300,200,300+lmin*cos(mang),200+lmin*sin(mang));
line(300,200,300+lhr*cos(hang),200+lhr*sin(hang));
gettime(&t);
sang=(double)t.ti_sec*6;
mang=(double)t.ti_min*6+t.ti_sec/10;
hang=(double)t.ti_hour*30+t.ti_min/2;
sang=(sang-90)/180*M_PI;
mang=(mang-90)/180*M_PI;
hang=(hang-90)/180*M_PI;
setcolor(WHITE);
setlinestyle(SOLID_LINE,0,3);
line(300,200,300+lsec*cos(sang),200+lsec*sin(sang));
line(300,200,300+lmin*cos(mang),200+lmin*sin(mang));
line(300,200,300+lhr*cos(hang),200+lhr*sin(hang));
fillellipse(300,200,5,5);
sleep(1);
}
getch();
closegraph();
restorecrtmode();
}

Output:

WAP to draw an animated fish


Program:
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>

void main()
{
int gd=DETECT,gm,i,j,p,q,x,y,flag;
struct arccoordstype arcinfo;
initgraph(&gd,&gm,"c:\\tc");
y=getmaxy()/2;
i=0;
p=0;
q=0;
j=0;

flag=0;
setcolor(WHITE);
for(int k=0;k<500;k++)
putpixel(random(620),random(419),15);
while(!kbhit())
{
line(0+i,y+20,0+i,y-20); /*triangle*/
line(0+i,y+20,20+i,y);
line(0+i,y-20,20+i,y);

ellipse(80+i,y,10-p,350+p,60,25);
line(140+i,y+3,130+i,y+8);
line(130+i,y+8,140+i,y-5);

if(flag==0)
p=p+2;
if(flag==1)
p=p-2;

circle(120+i,y-6,5);

line(60+i,y-25,30+i,y-45+j);
line(30+i,y-45+j,100+i,y-25);
line(60+i,y+25,30+i,y+45+q);

line(30+i,y+45+q,100+i,y+25);
i=i+3;
if(i>=638)
i=0;

if(flag==0)
j=j-2,q=q+2;

if(flag==1)
j=j+2,q=q-2;

if(j<=-10)
flag=1;
else if(j>=0)
flag=0;

delay(100);
cleardevice();
}

getch();
closegraph();
}
Output:

PROGRAM TO DRAW A CIRCLE USING BRESENHAMS


METHOD (8-WAY SYMMETRY)
Program:
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int x=0,y,xc,yc,r,p;
int gd=DETECT,gm;
y=r;
initgraph(&gd,&gm,"");
printf("Enter the value of x,y,xc,yc,r");
scanf("%d%d%d%d%d",&x,&y,&xc,&yc,&r);
putpixel(x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,YELLOW);
putpixel(-y+xc,x+yc,YELLOW);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(-x+xc,-y+yc,YELLOW);
putpixel(-y+xc,-x+yc,YELLOW);
putpixel(y+xc,-x+yc,YELLOW);

//0,100,250,250,100

putpixel(x+xc,-y+yc,YELLOW);
p=3-(2*r);
while(x<=y)
{
if(p<0)
{
x=x+1;
putpixel(x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,YELLOW);
putpixel(-y+xc,x+yc,YELLOW);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(-x+xc,-y+yc,YELLOW);
putpixel(-y+xc,-x+yc,YELLOW);
putpixel(y+xc,-x+yc,YELLOW);
putpixel(x+xc,-y+yc,YELLOW);
p=p+(4*x)+6;
}
else
{
x=x+1;
y=y-1;

putpixel(x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,YELLOW);
putpixel(-y+xc,x+yc,YELLOW);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(-x+xc,-y+yc,YELLOW);
putpixel(-y+xc,-x+yc,YELLOW);
putpixel(y+xc,-x+yc,YELLOW);
putpixel(x+xc,-y+yc,YELLOW);
p=p+(4*x)-(4*y)+10;
}
}
getch();
}

Output:

PROGRAM TO DRAW A CIRCLE USING MID-POINT METHOD


Program:
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int x=0,y,xc,yc,r,p;
int gd=DETECT,gm;
y=r;
initgraph(&gd,&gm,"");
printf("Enter the value of x,y,xc,yc,r");
scanf("%d%d%d%d%d",&x,&y,&xc,&yc,&r);

putpixel(x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,YELLOW);
putpixel(-y+xc,x+yc,YELLOW);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(-x+xc,-y+yc,YELLOW);
putpixel(-y+xc,-x+yc,YELLOW);
putpixel(y+xc,-x+yc,YELLOW);
putpixel(x+xc,-y+yc,YELLOW);
p=1-r;
while(x<=y)
{
if(p<0)
{
x=x+1;
putpixel(x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,YELLOW);
putpixel(-y+xc,x+yc,YELLOW);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(-x+xc,-y+yc,YELLOW);
putpixel(-y+xc,-x+yc,YELLOW);
putpixel(y+xc,-x+yc,YELLOW);
putpixel(x+xc,-y+yc,YELLOW);

//0,100,250,250,100

p=p+(2*x)+1;
}
else
{
x=x+1;
y=y-1;
putpixel(x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,YELLOW);
putpixel(-y+xc,x+yc,YELLOW);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(-x+xc,-y+yc,YELLOW);
putpixel(-y+xc,-x+yc,YELLOW);
putpixel(y+xc,-x+yc,YELLOW);
putpixel(x+xc,-y+yc,YELLOW);
p=p+(2*x)-(2*y)+1;
}
}
getch();
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy