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

Tuesday, April 13, 2010

Bezier Curve

#include
#include
#include
int gd,gm,maxx,maxy;
float xxx[1][2]={0.0,0.0,
         };
void line1(float x2,float y2)
{
int d1,d2,d3,d4;
d1=maxx*xxx[0][0];
d2=maxy*xxx[0][1];
d3=maxx*x2;
d4=maxy*y2;
line(d1,d2,d3,d4);
xxx[0][0]=x2;
xxx[0][1]=y2;
}
bezier(float xb,float yb,float xc,float yc,float xd,float yd,int n)
{
float xab,yab,xbc,ybc,xcd,ycd;
float xabc,yabc,xbcd,ybcd;
float xabcd,yabcd;
if (n==0)
    {
     line1(xb,yb);
     line1(xc,yc);
     line1(xd,yd);
    }
else
    {
     xab = (xxx[0][0]+xb)/2;
     yab = (xxx[0][1]+yb)/2;
     xbc = (xb+xc)/2;
     ybc = (yb+yc)/2;
     xcd = (xc+xd)/2;
     ycd = (yc+yd)/2;
     xabc = (xab+xbc)/2;
     yabc = (yab+ybc)/2;
     xbcd = (xbc+xcd)/2;
     ybcd = (ybc+ycd)/2;
     xabcd = (xabc+xbcd)/2;
     yabcd = (yabc+ybcd)/2;
     n=n-1;
     bezier(xab,yab,xabc,yabc,xabcd,yabcd,n);
     bezier(xbcd,ybcd,xcd,ycd,xd,yd,n);
    }
}
void igraph()
    {
    detectgraph(&gd,&gm);
    if(gd<0)
        {
        puts("CANNOT DETECT A GRAPHICS CARD");
        exit(1);
        }
    initgraph(&gd,&gm,"c:\\tc\\bgi");
    }
main()
    {
    igraph();
    maxx = getmaxx();
    maxy = getmaxy();
    xxx[0][0] = 0.5;
    xxx[0][1] = 0.5;
    setcolor(12);
    bezier(0.15,0.5,0.25,0.2,0.5,0.5,8);
    setfillstyle(1,12);
    floodfill(0.45*maxx,0.45*maxy,12);
    xxx[0][0] = 0.5;
    xxx[0][1] = 0.5;
    setcolor(12);
    bezier(0.35,0.3,0.6,0,0.5,0.5,4);
    setfillstyle(1,12);
    floodfill(0.5*maxx,0.45*maxy,12);
    xxx[0][0] = 0.5;
    xxx[0][1] = 0.5;
    setcolor(12);
    bezier(0.90,0.45,0.64,0.2,0.5,0.5,4);
    setfillstyle(1,12);
    floodfill(0.55*maxx,0.45*maxy,12);
    xxx[0][0] = 0.5;
    xxx[0][1] = 0.5;
    setcolor(12);
    bezier(0.15,0.6,0.25,0.87,0.5,0.5,4);
    setfillstyle(1,12);
    floodfill(0.45*maxx,0.55*maxy,12);
    xxx[0][0] = 0.5;
    xxx[0][1] = 0.5;
    setcolor(12);
    bezier(0.85,0.75,0.57,0.8,0.5,0.5,4);
    setfillstyle(1,12);
    floodfill(0.55*maxx,0.55*maxy,12);
    xxx[0][0] = 0.5;
    xxx[0][1] = 0.5;
    setcolor(2);
    bezier(0.5,0.75,0.57,0.8,0.5,1,4);
    setcolor(14);
    circle(0.5*maxx,0.5*maxy,10);
    setfillstyle(11,14);
    floodfill(0.5*maxx,0.5*maxy,14);
    xxx[0][0] = 0.534;
    xxx[0][1] = 0.9;
    setcolor(2);
    bezier(0.85,0.75,0.57,0.78,0.534,0.9,4);
    setfillstyle(2,2);
    floodfill(0.60*maxx,0.85*maxy,2);
    getch();
    closegraph();
    }

Midpoint Ellipse

#include
#include
#include
#include
int xc,yc;
void plotpoints(int,int,int,int);
void plotpoints(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,1);
}
void main()
{
int p,px,x,y,rx2,ry2,a,b,py,tworx2,twory2,py2,k;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\TC\\BGI");
clrscr();
printf("Enter the centre co-ordinates: ");
scanf("%d%d",&xc,&yc);
printf("Enter the midpoint: ");
scanf("%d%d",&a,&b);
ry2=b*b;
rx2=a*a;
tworx2=2*rx2;
twory2=2*ry2;
x=0;
y=b;
plotpoints(xc,yc,k,y);
p=ceil(ry2-rx2*b+(0.25*rx2));
px=0;
py=tworx2*y;
while(px
{
x=x+1;
px=px+twory2;
if(p>=0)
{
y=y-1;
py=py-tworx2;
}
if(p<0)
{
p=p+ry2+px;
}
else
{
p=p+ry2+px-py;
}
plotpoints(xc,yc,x,y);
}
p=ceil(ry2+(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2+ry2);
while(y>0)
{
y=y-1;
py=py-tworx2;
if(p<=0)
{
x=x+1;
px=px+twory2;
}
if(p>0)
{
p=p+rx2-py;
}
else
{
p=p+rx2-py+px;
}
plotpoints(xc,yc,x,y);
}
getch();
}

2D Transformation

#include
#include
#include
#include
#define gx getmaxx
void main()
{
 int x1,y1,x2,y2,i,gd=DETECT,gm;
 char d;
 void translation(int,int,int,int);
 void scaling(int,int,int,int);
 void rotation(int,int,int,int);
 clrscr();
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 printf("Enter the end-points of the line:\n");
 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
 line(x1,y1,x2,y2);

  do
  {
   printf("Enter the type of transfromation:\n");
 printf("\t\t\t1--->Translation\n\t\t\t2--->Scaling\n\t\t\t3--->Rotation\n");
  scanf("%d",&i);
   switch(i)
   {
    case 1: translation(x1,y1,x2,y2);
    break;
    case 2: scaling(x1,y1,x2,y2);
    break;
    case 3: rotation(x1,y1,x2,y2);
    break;
     default : break;
    }
    printf("Do u wanna continue???  'Y' or 'N'\n");
    scanf("%s",d);
  }
  while(d!='n'||d!='N');
   getch();
}


  void translation(int x1,int y1,int x2,int y2)
  {
   int tx,ty;
   printf("Enter the translation factors:\n");
   scanf("%d%d",&tx,&ty);
   x1+=tx;
   y1+=ty;
   x2+=tx;
   y2+=ty;
   printf("The new translated line is:\n");
   line(x1,y1,x2,y2);
  }


  void scaling(int x1,int y1,int x2,int y2)
  {
   int sx,sy;
   printf("Enter the scaling factors:\n");
   scanf("%d%d",&sx,&sy);
   x1*=sx;
   y1*=sy;
   x2*=sx;
   y2*=sy;
   printf("The new scaling line is:\n");
   line(x1,y1,x2,y2);
  }


 void rotation(int x1,int y1,int x2,int y2)
  {
   int a1,a2,b1,b2,th;
   printf("Enter the value for theta:\n");
   scanf("%d",&th);
   a2=(x2*cos(th))-(y2*sin(th));
   b2=(y2*cos(th))+(x2*sin(th));
   printf("The new rotated line is:\n");
   line(x1,y1,a2,b2);
  }

BoundaryFill Algo

#include
#include
#include
void main()
{
int gd,gm;
/*initialize graphics mode*/
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(50,50,100,100);
flood(55,55,1,15);
getch();
closegraph();
}

flood(seed_x,seed_y,foreground_col,background_col)
{
if(getpixel(seed_x,seed_y)!=background_col&&getpixel(seed_x,seed_y)!=foreground_col)
{
putpixel(seed_x,seed_y,foreground_col);
flood(seed_x+1,seed_y,foreground_col,background_col);
flood(seed_x-1,seed_y,foreground_col,background_col);
flood(seed_x,seed_y+1,foreground_col,background_col);
flood(seed_x-1,seed_y-1,foreground_col,background_col);
flood(seed_x+1,seed_y+1,foreground_col,background_col);
flood(seed_x-1,seed_y-1,foreground_col,background_col);
flood(seed_x+1,seed_y-1,foreground_col,background_col);
flood(seed_x-1,seed_y+1,foreground_col,background_col);
}
}

FloodFill Algo

#include
#include
#include
#include
void main()
{
 int l,t,r,b,gd=DETECT,x,y,gm,fc;
  void floodfill2(int,int,int,int);
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 printf("Enter the left top and right bottom of the rectanlge:\n");
 scanf("%d%d%d%d",&l,&t,&r,&b);
 rectangle(l,t,r,b);
 printf("Enter the inside pixel");
 scanf("%d%d",&x,&y);
 floodfill2(x,y,6,0);
 getch();
}

 void floodfill2(int x,int y,int fc,int oc)
 {
  int c=getpixel(x,y);
  if(c==oc)
  {
  putpixel(x,y,fc);
   floodfill2(x+1,y,fc,oc);
   floodfill2(x-1,y,fc,oc);
   floodfill2(x,y+1,fc,oc);
   floodfill2(x,y-1,fc,oc);
  }
 }

Sutherland Hodgeman(polygon Clipping Algo)


    /* PROGRAM TO IMPLEMENT POLYGON CLIPPING ALGORITHM */

#include
#include
#include
#include
#include
#include

void intersection(float x1i,float y1i,float x2i,float y2i);
float xx1,yy1,xx2,yy2;
int xwmin=100,ywmax=100,xwmax=400,ywmin=400;
char chr;
void main()
    {
    do
        {
        int gd=DETECT,gm,get;
        initgraph(&gd,&gm,"c:\\tc\\bgi");
        //clrscr();
        cleardevice();
        void clip_line(float x1,float y1,float x2,float y2);
        int x1,x2,y1,y2,i,n,a[4],b[4],c[4],x[15],y[15];
        char ch[5][7]={"first","second","third","fourth","fifth"};
        gotoxy(10,4);
        printf("Give no. of vertices of polygon");
        scanf("%d",&n);
        for(i=0;i
            {
            printf("\nGive %s vertex of polygon:",ch[i]);
            scanf("%d%d",&x[i],&y[i]);
            }
        x[i]=x[0];
        y[i]=y[0];
        rectangle(xwmin,ywmax,xwmax,ywmin);
        settextstyle(8,0,1);
        outtextxy(50,40,"Before clipping");
        for(i=0;i
            line(x[i],y[i],x[i+1],y[i+1]);
        getch();
        cleardevice();
        outtextxy(50,40,"After clipping");
        for(i=0;i
            clip_line(x[i],y[i],x[i+1],y[i+1]);
        clip_line(x[i],y[i],x[0],y[0]);
        getch();
        cleardevice();
        printf("\n\t\tdo u want 2 continue? y/n:");
        chr=getch();
        }while(chr=='y' || chr=='Y');
        getch();
        closegraph();
    }
void clip_line(float x1,float y1,float x2,float y2)
    {
    int temp1,temp2,temp3,temp4;
    float xc1,yc1,xc2,yc2;
    xc1=x1;
    xc2=x2;
    yc1=y1;
    yc2=y2;
    rectangle(xwmin,ywmax,xwmax,ywmin);
    if(xwmin<=x1 && x1<=xwmax)
    if(ywmax<=y1 && y1<=ywmin)
    if(xwmin<=x2 && x2<=xwmax)
    if(ywmax<=y2 && y2<=ywmin)
        {
        line(x1,y1,x2,y2);
        return;
        }
    if((xwmin<=x1 && x1<=xwmax) &&(ywmax<=y1 && y1<=ywmin))
        {

         temp1=x1;
         temp2=y1;
         while(1)
            {
             intersection(x1,y1,x2,y2);
             if((xx1==float(xwmin)) || (xx1==float(xwmax))
             || (yy1==float(ywmax)) || (yy1==float(ywmin)))
                {
                 line(xx1,yy1,temp1,temp2);
                 return;
                }
             if((xx1>xwmin && xx2ywmax && yy2
                {
                x1=xx1;
                y1=yy1;
                }
             else
                {
                x2=xx1;
                y2=yy1;
                }
            }
        }
        if((xwmin<=x2 && x2<=xwmax) && (ywmax<=y2 && y2<=ywmin))
            {
            temp1=x2;
            temp2=y2;
            while(1)
                {
                intersection(x1,y1,x2,y2);
                if((xx1==float(xwmin)) || (xx1==float(xwmax))
                || (yy1==float(ywmax)) || (yy1==float(ywmin)))
                    {
                    line(xx1,yy1,temp1,temp2);
                    return;
                    }
                if((xx1>xwmin && xx2ywmax && yy2
                    {
                    x2=xx1;
                    y2=yy1;
                    }
                else
                    {
                    x1=xx1;
                    y1=yy1;
                    }
                }
            }
            while(1)
                {
                intersection(x1,y1,x2,y2);
                if(xx1==x1 || xx1==x2 || yy1==y1 || yy1==y2)
                    return;
                if((xx1>xwmin && xx2ywmax && yy2
                    {
                    up1:
                    temp1=xx1;
                    temp2=yy2;
                while(1)
                {
                intersection(x1,y1,x2,y2);
                if((xx1==float(xwmin)) || (xx1==float(xwmax))
                || (yy1==float(ywmax)) || (yy1==float(ywmin)))
                    {
                    line(xx1,yy1,temp1,temp2);
                    goto jump1;
                    }
                if((xx1>xwmin && xx2ywmax && yy2
                    {
                    x1=xx1;
                    y1=yy1;
                    }
                else
                    {
                    x2=xx1;
                    y2=yy1;
                    }
                }
            jump1:
                while(1)
                {
                intersection(xc1,yc1,xc2,yc2);
                if((xx1==float(xwmin)) || (xx1==float(xwmax))
                || (yy1==float(ywmax)) || (yy1==float(ywmin)))
                    {
                    line(xx1,yy1,temp1,temp2);
                    return;
                    }
                if((xx1>xwmin && xx2ywmax && yy2
                    {
                    xc2=xx1;
                    yc2=yy1;
                    }
                else
                    {
                    xc1=xx1;
                    yc1=yy1;
                    }
                }
            }
            else
            {
            while(1)
                {
                intersection(xc1,yc1,xc2,yc2);
                if((xx1==float(xwmin)) || (xx1==float(xwmax))
                || (yy1==float(ywmax)) || (yy1==float(ywmin)))
                {
                    goto up1;
                }
                if(xx1==xc1 || xx1==xc2 || yy1==yc1 || yy1==yc2)
                    break;
                else
                {
                    xc1=xx1;
                    yc1=yy1;
                }
            }
            while(1)
            {
                intersection(xc1,yc1,xc2,yc2);
                if((xx1>xwmin && xc1>xwmax && yy1>ywmin &&  yc1>ywmax ))
                {
                    goto up1;
                }
                if(xx1==x1 || xx1==x2 || yy1==y1 || yy1==y2)
                    return;
                else
                {
                    xc1=xx1;
                    yc1=yy1;
                }
            }
        }
    }
}

void intersection(float x1i,float y1i,float x2i,float y2i)
{
    xx1=(x1i+x2i)/2;
    yy1=(y1i+y2i)/2;
}

Liang Barsky(Line Clipping Algo)

#include
#include
#include
#include
void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
clrscr();
printf("Enter the windows left xmin , top boundry ymin\n");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry ymax\n");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinate\n");
scanf("%d%d",&x2,&y2);
printf("liang barsky express these 4 inequalities using lpk<=qpk\n");
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
printf("p1=0 line is parallel to left clipping\n");
printf("p2=0 line is parallel to right clipping\n");
printf("p3=0 line is parallel to bottom clipping\n");
printf("p4=0 line is parallel to top clipping\n");

if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) ||
( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) ||
( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||
( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )
{
printf("Line is rejected\n");
getch();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(RED);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(BLUE);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
}
else
{
if( p1 != 0.0 )
{
r1 =(float) q1 /p1 ;
if( p1 < 0 )
u1 = max(r1 , u1 );
else
u2 = min(r1 , u2 );
}
if( p2 != 0.0 )
{
r2 = (float ) q2 /p2 ;
if( p2 < 0 )
u1 = max(r2 , u1 );
else
u2 = min(r2 , u2 );

}
if( p3 != 0.0 )
{
r3 = (float )q3 /p3 ;
if( p3 < 0 )
u1 = max(r3 , u1 );
else
u2 = min(r3 , u2 );
}
if( p4 != 0.0 )
{
r4 = (float )q4 /p4 ;
if( p4 < 0 )
u1 = max(r4 , u1 );
else
u2 = min(r4 , u2 );
}

if( u1 > u2 )
printf("line rejected\n");
else
{
x11 = x1 + u1 * ( x2 - x1 ) ;
y11 = y1 + u1 * ( y2 - y1 ) ;

x22 = x1 + u2 * ( x2 - x1 );
y22 = y1 + u2 * ( y2 - y1 );

printf("Original line cordinates\n");
printf("x1 = %d , y1 = %d, x2 = %d, y2 = %d\n",x1,y1,x2,y2);
printf("Windows coordiante are \n");
printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d ",wxmin,wymin,wxmax,wymax);

printf("New coordinates are \n");
printf("x1 = %d, y1 = %d,x2 = %d , y2 = %d\n",x11,y11,x22,y22);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(2);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(1);
line(x1,y1,x2,y2);
getch();
setcolor(0);
line(x1,y1,x2,y2);
setcolor(3);
line(x11,y11,x22,y22);
getch();
}
}
}

Cohen Sutherland(Line Clipping Algo)

#include
#include
#include
#include
void main()
{ float xmin,ymin,xmax,ymax,x1,x2,y1,y2;
int gd=DETECT,gm;
void clip(float,float,float,float,float,float,float,float);
printf("Enter the window co-ordinate:");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
printf("Enter the co-ordinate:");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(15);
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
setcolor(0);
line(x1,y1,x2,y2);
clip(xmin,ymin,xmax,ymax,x1,y1,x2,y2);
}
void clip(float xmin,float ymin,float xmax,float ymax,float x1,float y1,float x2,float y2)
{ static int c[4],c1[4],c2[4],flag1,flag2,i;
 float x,y;
 void code(float,float,float,float,float,float,int*);
 code(x1,y1,xmin,ymin,xmax,ymax,c1);
 code(x2,y2,xmin,ymin,xmax,ymax,c2);
 flag1=c1[0]||c1[1]||c1[2]||c1[3];
 flag2=c2[0]||c2[1]||c2[2]||c2[3];
 while(flag1||flag2)
 {
  if(c1[0]&&c2[0]||c1[1]&&c2[1]||c1[2]&&c2[2]||c1[3]&&c2[3])
  {
   closegraph();
   printf("Line is outside the window:");
   goto abc;
  }
 if(flag1==0)
 for(i=0;i<4;i++)
 c[i]=c2[i];
 else
  for(i=0;i<4;i++)
 c[i]=c1[i];
 if(c[0]==1)
 {
  y=y1+(y2-y1)*(xmin-x1)/(x2-x1);
  x=xmin;
 }
 else
 if(c[1]==1)
 {
  y=y1+(y2-y1)*(xmax-x1)/(x2-x1);
  x=xmax;
 }
 else if(c[2]==1)
 {
  x=x1+(x2-x1)*(y1-ymax)/(y2-y1);
  x=ymax;
 }
 else if(c[3]==1)
 {
  x=x1+(x2-x1)*(ymin-y1)/(y2-y1);
  x=ymin;
 }
 if(flag1==0)
 {
  x2=x;
  y2=y;
  code(x2,y2,xmin,ymin,xmax,ymax,c2);
  flag2=c2[0]||c2[1]||c2[2]||c2[3];
 }
  else
  {
  x1=x;
  y1=y;
  code(x1,y1,xmin,ymin,xmax,ymax,c1);
  flag1=c1[0]||c1[1]||c1[2]||c1[3];
  }
 }
 setcolor(15);
 line(x1,y1,x2,y2);
 abc:
 getch();
}void code(float x,float y,float xmin,float ymin,float xmax,float ymax,int* c)
{
 if(x
 {
  c[0]=1;
  c[1]=0;
 }
 else if(x>xmax)
 {
  c[0]=0;
  c[1]=1;
 }
else c[0]=c[1]=0;
if(y>ymax)
 {
  c[2]=1;
  c[3]=0;
 }
else if(y
 {
  c[2]=0;
  c[3]=1;
 }
else c[2]=c[3]=0;

}

Character Generation

#include
#include
void main()
{
int a[5][7],i,j;
clrscr();
cout<<"Enter the array";
for(i=0;i<7;i++)
{
for(j=0;j<5;j++)
{
cin>>a[i][j];
}
cout<
}
for(i=0;i<7;i++)
{
for(j=0;j<5;j++)
{
if(a[i][j]==0)
{
cout<<" ";
}
else
{
cout<
}
}
cout<
}
getch();
}
/*Output

Enter the array
0 0 0 0 0                                                                      
0 0 1 0 0                                                                      
0 1 0 1 0                                                                      
1 0 0 0 1                                                                      
1 1 1 1 1                                                                      
1 0 0 0 1
1 0 0 0 1


    1
 1    1
1      1
11111
1      1
1      1 */

Midpoint Circle Algo

#include
#include
#include
#include
void main()
{
 int xc,yc,r,gd=DETECT,gm;
 void mid_cir(int,int,int);
  printf("Enter the midpoints of circle and radius as xc,yc,r :\n");
 scanf("%d%d%d",&xc,&yc,&r);
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 mid_cir(xc,yc,r);
 getch();
 }
 void mid_cir(int xc,int yc,int r)
 {
  int x=0,y=r,p;
  void display(int,int,int,int);
  p=1-r;
  while(x<=y)
  {
   display(xc,yc,x,y);
   if(p<0)
   p+=2*x+3;
   else
   {
    p+=2*(x-y)+5;
    y--;
   }
  x++;
 }
}
 void display(int xc,int yc,int x,int y)
 {
  putpixel(xc+x,yc+y,14);
  putpixel(xc+x,yc-y,14);
  putpixel(xc-x,yc+y,14);
  putpixel(xc-x,yc-y,14);
  putpixel(xc+y,yc+x,14);
  putpixel(xc+y,yc-x,14);
  putpixel(xc-y,yc+x,14);
  putpixel(xc-y,yc-x,14);
 }

Bressenham's_Circle Algo

#include
#include
#include
#include
void main()
{
 int xc,yc,r,gd=DETECT,gm;
 void bres_cir(int,int,int);
  printf("Enter the midpoints of circle and radius as xc,yc,r :\n");
 scanf("%d%d%d",&xc,&yc,&r);
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 bres_cir(xc,yc,r);
 getch();
 }


 void bres_cir(int xc,int yc,int r)
 {
  int x=0,y=r,p;
  void display(int,int,int,int);
  p=3-2*r;
  while(x<=y)
  {
   display(xc,yc,x,y);
   if(p<0)
   p+=4*x+6;
   else
   {
    p+=4*(x-y)+10;
    y--;
   }
  x++;
 }
}
 void display(int xc,int yc,int x,int y)
 {
  putpixel(xc+x,yc+y,14);
  putpixel(xc+x,yc-y,14);
  putpixel(xc-x,yc+y,14);
  putpixel(xc-x,yc-y,14);
  putpixel(xc+y,yc+x,14);
  putpixel(xc+y,yc-x,14);
  putpixel(xc-y,yc+x,14);
  putpixel(xc-y,yc-x,14);
 }

DDA Algorithm

#include
#include
#include
#include
#define gx getmaxx()
#define gy getmaxy()
void main()
{
 int x1,y1,x2,y2,i,chx,chy,steps,gd=DETECT,gm;
 float x,y,xinc,yinc;
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 //line(gx/2,0,gx/2,gy);
 //line(0,gy/2,gx,gy/2);
 printf("Enter the end ponits of line as x1,y1,x2,y2:\n");
 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
 chx=x2-x1;
 chy=y2-y1;
 if(abs(chx)>abs(chy))
 steps=abs(chx);
 else
 steps=abs(chy);
 xinc=(float)chx/steps;
 yinc=(float)chy/steps;
 x=x1;
 y=y1;
 putpixel(x+gx/2,-y+gy/2,14);
 for(i=0;i
 {
  x=x+xinc;
  y=y+yinc;
  delay(50);
  putpixel(x+gx/2,-y+gy/2,i);
 }
getch();
}

Bressenham's_Line drawing Algo

#include
#include
#include
#include
#define gx getmaxx()
#define gy getmaxy()
void main()
{
 int gd=DETECT,gm,i,x,y,x1,y1,x2,y2,xinc,yinc,dx,dy,p;
 printf("Enter the endpoints of line:\n");
 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
 initgraph(&gd,&gm,"c:\\tc\\bgi ");
 x=x1;
 y=y1;
 putpixel(x,y,14);

 dx=abs(x2-x1);
 dy=abs(y2-y1);

 if(x1
 xinc=1;
 else
 xinc=-1;
 if(y1
 yinc=1;
 else
 yinc=-1;

 if(dx>=dy)
 {
  p+=2*dy-dx;
  while(x!=x2)
  {
   x+=xinc;
   if(p<0)
   p+=2*dy;
   else
   {
    p+=2*(dy-dx);
    y+=yinc;
   }
  putpixel(x,y,14);
 }
}
 else
   {
  p+=2*dx-dy;
  while(y!=y2)
  {
   y+=yinc;
   if(p<0)
   p+=2*dx;
   else
   {
    p+=2*(dx-dy);
    x+=xinc;
   }
  putpixel(x,y,14);
 }
}
/*closegraph();*/
getch();
}
 
ShareThis

Visitor

Website counter
Copyright 2009 Code's. Powered by Blogger
Blogger Templates created by Deluxe Templates
Wordpress by Wpthemescreator
Blogger Showcase