Thursday, 9 May 2013

Boundary and flood fill ( Recursive and non recursive)


#include
#include
#include
#include
#define round(a)((int)(a+0.5))
struct point{ int xc;int yc;}curpix, stk [9999];
int top=-1;
void push(int x,int y)
{ top++;
stk[top].xc=x;
stk[top].yc=y;
}
void pop()
{ curpix.xc=stk[top].xc;
curpix.yc=stk[top].yc;
top--;
}

void ddaline(int x1,int y1,int x2,int y2,int col)
{
int dx,dy,steps,k;
float x,y,xinc,yinc;
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(round(x),round(y),col);
for(k=0;k { x+=xinc;
y+=yinc;
putpixel(round(x),round(y),col);
}
}
void bfill(int x,int y,int bclr,int fclr)
{ int curclr;
curclr=getpixel(x,y);
if(curclr!=fclr && curclr!=bclr)
{
putpixel(x,y,fclr);
bfill(x+1,y,bclr,fclr);
bfill(x,y+1,bclr,fclr);
bfill(x-1,y,bclr,fclr);
bfill(x,y-1,bclr,fclr);
}
}

void ffill(int x,int y,int oclr,int fclr)
{ int curclr;
curclr=getpixel(x,y);
if(curclr==oclr)
{
putpixel(x,y,fclr);
ffill(x+1,y,oclr,fclr);
ffill(x,y+1,oclr,fclr);
ffill(x-1,y,oclr,fclr);
ffill(x,y-1,oclr,fclr);
}
}

void rff(int x,int y,int oclr,int fclr)
{ int curclr;
while(top!=-1)
{ curclr=getpixel(x,y);
if(curclr==oclr)
{
putpixel(x,y,fclr);
push(x+1,y);
push(x,y+1);
push(x-1,y);
push(x,y-1);
}
pop();
x=curpix.xc;
y=curpix.yc;

}
}
void rbf(int x,int y,int bclr,int fclr)
{ int curclr;
while(top!=-1)
{ curclr=getpixel(x,y);
if(curclr!=bclr && curclr!=fclr)
{
putpixel(x,y,fclr);
push(x+1,y);
push(x,y+1);
push(x-1,y);
push(x,y-1);
}
pop();
x=curpix.xc;
y=curpix.yc;

}
}
//*********************************************
void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int x[10],y[10],n,i,sumx=0,sumy=0,seedx,seedy,old;
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the no of sides of polygon\n");
scanf("%d",&n);
printf("\n enter the end points of polygon\n");
for(i=0;i {
scanf("%d",&x[i]);
scanf("%d",&y[i]);
sumx+=x[i];sumy+=y[i];
}
x[i]=x[0];y[i]=y[0];
for(i=0;i {
ddaline(x[i],y[i],x[i+1],y[i+1],2);
}
seedx=sumx/n;
seedy=sumy/n;
old=getpixel(seedx,seedy);
push(seedx,seedy);
rbf(seedx,seedy,2,RED);
getch();
}

Ball Moving on sphere


#include
#include
#include
#include
#define pi 3.14285
#define round(a)((int)(a+0.5))
int i;
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; float x,y;
r=r1+20;
for(k=0;k<360;k++)
{ x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);

cleardevice();
parcircle(xc,yc,r1,11);
i+=5;
drawcir(round(x),round(y),20,i);

}

}
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");
scanf("%d",&x);
scanf("%d",&y);
printf("\n enter the radius");
scanf("%f",&r);
xc=x;yc=y;

while(!kbhit())
{

parc(xc,yc,r,i);


}

}

revolution of saturn and jupiter around the 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();
}

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;
}