Sunday, October 16, 2011

C Graphics : Shearing in 2D transformation


void shearing(int *x,int *y,double shx,double shy)

{

double trans[3][3]={1,0,0,0,1,0,0,0,1};

double a[3][1]={0,0,1};

double c[3][1];

int i,j,k;



trans[0][1]=shx;

trans[1][0]=shy;

a[0][0]=*x;

a[0][1]=*y;



for(i=0;i<3;i++)

{

for(j=0;j<1;j++)

{

c[i][j]=0;

for(k=0;k<3;k++)

{

c[i][j]+=trans[i][k]*a[k][j];

}

}



}



*x=c[0][0];

*y=c[1][0];



}

C Graphics : Scaling in 2D transformation


void scale(int *x,int *y,double sx,double sy)

{

double trans[3][3]={1,0,0,0,1,0,0,0,1};

double a[3][1]={0,0,1};

double c[3][1];

int i,j,k;

trans[0][0]=sx;

trans[1][1]=sy;

a[0][0]=*x;

a[0][1]=*y;



for(i=0;i<3;i++)

{

for(j=0;j<1;j++)

{

c[i][j]=0;

for(k=0;k<3;k++)

{

c[i][j]+=trans[i][k]*a[k][j];

}

}

}



*x=c[0][0];

*y=c[1][0];



}

C Graphics : Rotation in 2D transformation


void rotate(int *x,int *y,double th)

{

double trans[3][3]={1,0,0,0,1,0,0,0,1};

double a[3][1]={0,0,1};

double c[3][1];

int i,j,k;





th=th*M_PI/180;

trans[0][0]=cos(th);

trans[0][1]=-sin(th);

trans[1][0]=sin(th);

trans[1][1]=cos(th);

a[0][0]=*x;

a[1][0]=*y;



for(i=0;i<3;i++)

{

for(j=0;j<1;j++)

{

c[i][j]=0;

for(k=0;k<3;k++)

{

c[i][j]+=trans[i][k]*a[k][j];

}

}

}







*x=abs(c[0][0]);

*y=abs(c[1][0]);





}

C Graphics : Translation in 2D transformation


void transalate(int *x,int *y,int tx,int ty)

{

int trans[3][3]={1,0,0,0,1,0,0,0,1};

int a[3][1]={0,0,1};

int c[3][1];

int i,j,k;

trans[0][2]=tx;

trans[1][2]=ty;

a[0][0]=*x;

a[0][1]=*y;



for(i=0;i<3;i++)

{

for(j=0;j<1;j++)

{

c[i][j]=0;

for(k=0;k<3;k++)

{

c[i][j]+=trans[i][k]*a[k][j];

}

}

}



*x=c[0][0];

*y=c[1][0];



}

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