Showing posts with label C Graphics. Show all posts
Showing posts with label C Graphics. Show all posts

Monday, October 17, 2011

C Graphics : Line clipping of right boundary only using Liang Barsky algo

#include<graphics.h>
int main()
{
int x1,y1,x2,y2,x,y,dx,dy;
int xmin,xmax,ymin,ymax;
float u,p,q;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
xmin=ymin=100;
xmax=ymax=300;
x1=150;y1=150; x2=350; y2=200;
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
u=(float)(xmax-x1)/(x2-x1);
dx=x2-x1;
dy=y2-y1;
x=x1+(int)(dx*u);
y=y1+(int)(dy*u);
getch();
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x,y);
getch();
return 0;
}

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

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