Monday, September 12, 2011

C Graphics : Flood Fill Function

void flood_fill(int x,int y,int fc,int bc)
{
         int color=getpixel(x,y);
         if(color==bc)
         {
                  putpixel(x,y,12);
                  flood_fill(x,y+1,fc,bc);
                  flood_fill(x+1,y,fc,bc);
                  flood_fill(x,y-1,fc,bc);
                  flood_fill(x-1,y,fc,bc);
          }
}
               
               

C Graphics : Ellipse Generation


void midpoint_ellipse(int xaxis,int yaxis,int a,int b)
{
int x=0,y=b;
int aa=a*a,bb=b*b;
int aa2=2*a*a,bb2=2*b*b;

float p=b*b-a*a*b+0.25*a*a;

draw_ellipse(xaxis,yaxis,x,y);
while(bb2*x<=aa2*y)
{

x++;
if(p<=0)
{
p=p+bb2*x+bb;
}
else
{
y--;
p=p+bb2*x+bb-aa2*y;
}
draw_ellipse(xaxis,yaxis,x,y);
x=-x;
draw_ellipse(xaxis,yaxis,x,y);
x=-x;

}//end while


x=a;
y=0;
draw_ellipse(xaxis,yaxis,x,y);
p=aa+bb2*a+bb*0.25;
//p=bb*(x+0.5)*(x+0.5)+aa*(y-1)*(y-1)-aa*bb;

while(bb2*x>aa2*y) {
y++;
//fy=fy-aa2;
if(p>0)
{
p=p+aa-aa2*y;
}
else
{
x--;
p=p+bb2*x-aa2*y+aa;
}
draw_ellipse(xaxis,yaxis,x,y);
y=-y;
draw_ellipse(xaxis,yaxis,x,y);
y=-y;

}

}

void draw_ellipse(int xaxis,int yaxis,int x,int y)
{
putpixel(xaxis+x,yaxis+y,4);
putpixel(xaxis-x,yaxis+y,4);
putpixel(xaxis+x,yaxis-y,4);
putpixel(xaxis-x,yaxis-y,4);
}

Saturday, September 10, 2011

C Graphics : DDA Line Draw

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

void dda_line(int x1,int y1,int x2,int y2);

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */
initgraph(&gdriver, &gmode, "c:\\tc");

/* 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); /* return with error code */
}

getch();
return(0);
}

void dda_line(int x1,int y1,int x2,int y2)
{
int dx,dy,step;
float x,y,ix,iy;
dx=x2-x1;
dy=y2-y1;

if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);

ix=dx/(float)step;
iy=dy/(float)step;

x=x1;
y=y1;
putpixel(x,y,1);
while(step-->0)
{
x=x+ix;
y=y+iy;

putpixel(x,y,1);
}

}

C Graphics : Mid-Point Circle Draw

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



void draw_circle(int,int,int);
void main()
{
int gd=DETECT,gm;
int r;
int xcen,ycen;
initgraph(&gd,&gm,"c:\\tc\\");

//clrscr();
printf("enter radius :");
scanf("%d",&r);
printf("enter circle centre coordinate :");
scanf("%d%d",&xcen,&ycen);

draw_circle(xcen,ycen,r);

getch();
}
void draw_circle(int xcen,int ycen,int r)
{
int x,y,p;
x=0;
y=r;
p=1-r;
putpixel(xcen+x,ycen+y,12);
putpixel(xcen-x,ycen+y,12);
putpixel(xcen+x,ycen-y,12);
putpixel(xcen-x,ycen-y,12);
putpixel(xcen+y,ycen+x,12);
putpixel(xcen+y,ycen-x,12);
putpixel(xcen-y,ycen+x,12);
putpixel(xcen-y,ycen+x,12);
while(x<=y)
{
if(p<0)
p=p+2*x+1;
else
{
p=p+2*(x-y)+1;
y--;
}
x++;
putpixel(xcen+x,ycen+y,10);
putpixel(xcen-x,ycen+y,11);
putpixel(xcen+x,ycen-y,12);
putpixel(xcen-x,ycen-y,11);
putpixel(xcen+y,ycen+x,14);
putpixel(xcen+y,ycen-x,11);
putpixel(xcen-y,ycen-x,13);
putpixel(xcen-y,ycen+x,11);
}
}