Friday, December 4, 2009

Pie Chart

#include
#include
#include
#include
#include"math.h"
#define round(a) (int)(a+0.5)
void pie(int stangle,int endangle,int radius,int z)
{
int i,x,y,x1,y1;
for(radius=0;radius<100;radius++)
{
for(i=stangle;i{
x=getmaxx()/2+round(radius*cos((3.142*i)/180));
y=getmaxy()/2+round(radius*sin((3.142*i)/(180)));
putpixel(x,y,z);
}
}
}

int main(void)
{
/* request auto detection */
int gdriver, gmode, errorcode,sum=0,i,n,data[10],z=1;
detectgraph(&gdriver,&gmode);
int stangle = 0, endangle = 0, radius = 100;
printf("enter the no. of data");
scanf("%d",&n);
one:
sum=0;
for(i=0;i { printf("\nenter the %age of data");
scanf("%d",&data[i]);
}
for(i=0;i sum=sum+data[i];

if(sum!=100)
{
printf("error in data entry ");
getch();
clrscr();
goto one;
}
/* initialize graphics and local variables */
initgraph(&gdriver,&gmode, "..\\bgi");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

/* set fill style and draw a pie slice */
outtextxy(100,100,"PIE CHART");
for(i=0;i {
stangle=endangle;
endangle=stangle+round((data[i]*360)/100);
pie(stangle, endangle, radius,z);
z++;
}
/* clean up */
getch();
closegraph();
return 0;
}

Fan with Regulator

#include
#include
#include
#include
#include
#define pi 3.142857
#define ROUND(a) (int)(a+0.5)
void pie(int sa,int ea,int xc,int yc,int r)
{ double a1,a2;int x,y;
a1=(pi*sa)/180;
x=xc+r*cos(a1);
y=yc+r*sin(a1);
line(xc,yc,x,y);
a2=-(pi*ea)/180;
x=xc+r*cos(a2);
y=yc+r*sin(a2);
line(xc,yc,x,y);
}



void runfan(int d)
{ int i=0,sa=0,ea=1;

while(!kbhit())
{delay(d);
cleardevice();
circle(100,100,50);
line(90,150,90,250);
line(110,150,110,250);
line(40,250,40,300);
line(160,250,160,300);
line(40,300,160,300);
line(40,250,160, 250);
outtextxy(45,275,"0");
outtextxy(75,275,"1");
outtextxy(105,275,"2");
outtextxy(135,275,"3");

pie(sa,ea,100,100,50);
pie(sa+150,ea+150,100,100,50);
pie(sa+300,ea+300,100,100,50);

i++;
sa=sa-10;ea=ea+10;
}
}

void main()
{int d=0,cho;

int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"..\\bgi");
runfan(0);
do{

printf("\n enter speed of regulator");
scanf("%d",&d);
switch(d)
{case 0: runfan(d);
break;
case 1:
runfan(70);break;
case 2: runfan(50);break;
case 3: runfan(20);break;
}
printf("\n press 1 to continue");
scanf("%d",&cho);
}while(cho==1);


getch();
}

Bezier Curve

#include
#include

void circl(int x, int y, int r)
{
int x1,y1,p;
x1=0;
y1=r;
p=3-2*r;

while(x1 {
plotcircle(x,y,x1,y1);
if(p<0)
p=p+4*x1+6;
else
{
p=p+4*(x1-y1)+10;
y1=y1-1;
}
x1=x1+1;
}

if(x1==y1)
plotcircle(x,y,x1,y1);
}

plotcircle(int x,int y, int x1, int y1)
{
putpixel(x+x1,y+y1,RED);
putpixel(x-x1,y+y1,RED);
putpixel(x+x1,y-y1,RED);
putpixel(x-x1,y-y1,RED);
putpixel(x+y1,y+x1,RED);
putpixel(x-y1,y+x1,RED);
putpixel(x+y1,y-x1,RED);
putpixel(x-y1,y-x1,RED);
return 0;
}

void cubic(int x1,int y1,int x2, int y2, int x3, int y3, int x4, int y4, int color)
{
int Cx[4],Cy[4];
int ax,ay,bx,by,cx,cy,dx,dy,x,y;
int i=0;
double stepsize,t;
Cx[0]=x1;
Cy[0]=y1;
Cx[1]=x2;
Cy[1]=y2;
Cx[2]=x3;
Cy[2]=y3;
Cx[3]=x4;
Cy[3]=y4;
for(i=0;i<4;i++)
{
setcolor(color);
circl(Cx[i],Cy[i],3);
}
setcolor(color);
ax=-Cx[0]+3*Cx[1]+(-3*Cx[2])+Cx[3];
ay=-Cy[0]+3*Cy[1]+(-3*Cy[2])+Cy[3];
bx=3*Cx[0]+(-6*Cx[1])+3*Cx[2];
by=3*Cy[0]+(-6*Cy[1])+3*Cy[2];
cx=-3*Cx[0]+3*Cx[1];
cy=-3*Cy[0]+3*Cy[1];
dx=Cx[0];
dy=Cy[0];
for(i=0;i<4;i++)
{
putpixel(Cx[i],Cy[i],RED);
}
setcolor(WHITE);
for(i=0;i<3;i++)
{
line(Cx[i],Cy[i],Cx[(i+1)%4],Cy[(i+1)%4]);
}
setcolor(RED);
stepsize=1.0/1000.0;
for(i=0;i<1000;i++)
{
t=stepsize*(double)i;
x=ax*(t*t*t)+bx*(t*t)+cx*t+dx;
y=ay*(t*t*t)+by*(t*t)+cy*t+dy;
putpixel(x,y,RED);
}
}

main()
{
int gd,gm,x,y;
detectgraphI&gd,&gm);
initgraph(&gd,&gm,"..\\bgi ");

cleardevice();
cubic(50,300,100,50,250,50,350,300,6);
getch();
cleardevice();
cubic(50,300,350,300,100,50,250,50,6);
getch();
cleardevice();
cubic(50,300,100,50,350,300,250,50,6);
getch();
cleardevice();
cubic(350,300,50,300,100,50,250,50,6);
getch();

return 0;
}

Incircle of a triangle

#include
#include
#include
#include
# define pi 3.142857
# define round(a)((int)(a+0.5))
float xc,yc;
//))))))))))))))))
void ddaline(int x1,int y1,int x2,int y2,int col)
{ int steps,dx,dy,i;float xinc,yinc,x,y;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;y=y1;
putpixel(x,y,col);
for(i=0;i {
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),col);
}
}
//)))))))))))))))
void parcir(int xc,int yc,float r,int sang,int eang,int col)
{ int k;
float x,y;

for(k=sang;k { x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);
}
}

void drawcir(int x,int y,float r,int sang)
{ int k=0;
int eang;
eang=sang+120;
while(k!=3)
{
parcir(x,y,r,sang,eang,(k+1));
sang=eang;
eang+=120;
k++;
}
}

//888888888888888888888888

void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int x1,y1,x2,y2,x3,y3;float a,b,c,s,area,r;
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the co ordinates of the triangle");
scanf("%d",&x1);
scanf("%d",&y1);

printf("\n enter the co ordinates of the triangle");
scanf("%d",&x2);
scanf("%d",&y2);
printf("\n enter the co ordinates of the triangle");
scanf("%d",&x3);
scanf("%d",&y3);
cleardevice();
ddaline(x1,y1,x2,y2,YELLOW);
ddaline(x2,y2,x3,y3,YELLOW);
ddaline(x3,y3,x1,y1,YELLOW);

a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

b=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
c=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
r=area/s;
xc=(x1*b+x2*c+x3*a)/(a+b+c);
yc=(y1*b+y2*c+y3*a)/(a+b+c);


while(!kbhit())
{ drawcir(round(xc),round(yc),r,i);
i++;
delay(1);
}


getch();

}


motion of saturn n jupitor around sun!!

#include
#include
#include
#include
#define pi 3.14285
#define round(a)((int)(a+0.5))
int i;
void drawellipse(int xc,int yc,int a,int b)
{ float x,y;int i;
for(i=0;i<360;i++)
{ x=xc+a*cos(i*pi/180);
y=yc+b*sin(i*pi/180);
putpixel(round(x),round(y),11);
}
}
void parcircle(int ,int ,float,int ) ;
void parcir(int xc,int yc,float r,int sang,int eang,int col)
{ int k;
float x,y;

for(k=sang;k { x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);
}
}

void drawcir(int x,int y,float r,int sang)
{ int k=0;
int eang;
eang=sang+120;
while(k!=3)
{
parcir(x,y,r,sang,eang,(k+1));
sang=eang;
eang+=120;
k++;
}
}
void parc(int xc,int yc,float r1,int i)
{ int k=0,r,m; float x,y;
r=r1+60;m=r1+120;
for(k=0;k<360;k++)
{ x=xc+2*r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
delay(10);
cleardevice();
parcircle(xc,yc,r1,11);
i+=5;
drawcir(round(x),round(y),10,i);
x=xc+2*m*cos(pi*k/180);
y=yc+m*sin(pi*k/180);

drawcir(round(x),round(y),20,i);
drawellipse(round(x),round(y),30,10);

}

}
void parcircle(int xc,int yc,float r,int col)
{ int k=0; float x,y;
for(k=0;k<360;k++)
{ x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);

}
}
//========================
void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int x,y,xc,yc; float r,r1;
i=0;

initgraph(&gd,&gm,"..\\bgi");

printf("\n enter the centre of the sun");
scanf("%d",&x);
scanf("%d",&y);
printf("\n enter the radius of the sun");
scanf("%f",&r);
xc=x;yc=y;

while(!kbhit())
{

parc(xc,yc,r,i);

}




getch();
}

Sirpinsky Triangle

#include
#include
#include
#include
# define pi 3.142857
# define round(a)((int)(a+0.5))
int max=6;
//))))))))))))))))
void ddaline(int x1,int y1,int x2,int y2,int col)
{ int steps,dx,dy,i;float xinc,yinc,x,y;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;y=y1;
putpixel(x,y,col);
for(i=0;i {
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),col);
}
}
float minside(int x1,int y1,int x2,int y2,int x3,int y3)
{float a,b,c,s;
a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

b=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
c=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
if(a s=a;
else
s=b;
if(s>c)
s=c;
return s;
}
//--------------------------------------
void triangle(int x1,int y1,int x2,int y2,int x3,int y3,int i)
{ float mx1,my1,mx2,my2,mx3,my3;
i++;
if(i==max)
return;
else
{
ddaline(x1,y1,x2,y2,YELLOW);
ddaline(x2,y2,x3,y3,YELLOW);
ddaline(x3,y3,x1,y1,YELLOW);
mx1=(x1+x2)/2 ;
my1=(y1+y2)/2;
mx2=(x2+x3)/2 ;
my2=(y2+y3)/2;
mx3=(x1+x3)/2 ;
my3=(y1+y3)/2;
triangle(x1,y1,mx1,my1,mx3,my3,i);
triangle(x2,y2,mx1,my1,mx2,my2,i);
triangle(x3,y3,mx2,my2,mx3,my3,i);
}
}

//)))))))))))))))
void main()
{ int gd,gm; int x1,y1,x2,y2,x3,y3;float small;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the co ordinates of the triangle");
scanf("%d",&x1);
scanf("%d",&y1);

printf("\n enter the co ordinates of the triangle");
scanf("%d",&x2);
scanf("%d",&y2);
printf("\n enter the co ordinates of the triangle");
scanf("%d",&x3);
scanf("%d",&y3);
small=minside(x1,y1,x2,y2,x3,y3);

max=2+(int)small/20;
cleardevice();
triangle(x1,y1,x2,y2,x3,y3,0);

getch();
}

graphs of sin(x)/x,sin(x),&sin(x)+cos(x)

#include
#include
#include
#include
#include
# define pi 3.142857
# define round(a)((int)(a+0.5))
float xc,yc;
//))))))))))))))))
void ddaline(int x1,int y1,int x2,int y2,int col)
{ int dx,dy,k,steps;
float xinc,yinc,x,y;
dx=x2-x1;dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/(float)steps;
yinc=(float)dy/(float)steps;
x=x1;y=y1;
for(k=0;k { putpixel(round(x),round(y),col);
x+=xinc;
y+=yinc;
}
}
//)))))))))))))))
void main()
{ int gd,gm;
detectgraph(&gd,&gm);
float x=0,y;
initgraph(&gd,&gm,"..\\bgi");
ddaline(0,120,600,120,11);
while(x<600)
{ x++;
y=120+50*(sin(-pi*x/180)/(pi*x/180));
putpixel(round(x),round(y),12);delay(10);
}
getch();
ddaline(0,240,600,240,11);
x=0;
while(x<600)
{ x++;
y=240+50*(sin(-pi*x/180));
putpixel(round(x),round(y),12);delay(10);
}
getch();
ddaline(0,360,600,360,11);
x=0;
while(x<600)
{ x++;
y=320+10*(sin(-pi*x/180)+cos(-pi*x/180));
putpixel(round(x),round(y),12); delay(10);
}
getch();
}