Sunday 11 November 2012

2D Reflection


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
struct s
{ int x,y ;
};
void main()
{ int n;
int gd= DETECT ,gm;
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 int xmax,ymax;

 xmax=getmaxx();
 ymax=getmaxy();
// cout<<xmax<<" "<<ymax;
 line(0,240,639,240);
 line(320,0,320,479);

 struct s inp[10],out[10];
 printf ("\n enter the no of vertices");
 scanf ("%d",&n);
 for(int i=0;i<n;i++)
   { scanf ("%d%d",&inp[i].x,&inp[i].y);
   }
   for(i=0;i<n;i++)
   {
   inp[i].x=inp[i].x+320;
   }


   printf("\n enter your choice for reflection along different axes");
   printf("\n 1  along x - axis");
   printf("\n 2 along y axis");
   printf("\n 3 along x=y axis");
   printf("\n 4 along x= -y axis");
   printf("\n 5 along origin");
   int ch;
   scanf ("%d",&ch);
   switch(ch)
   { case 1:  
for(i=0;i<n;i++)
                              {
                              out[i].x=inp[i].x;
                               out[i].y=(240-(inp[i].y))+240;
                                }
                             break;
     case 2: for( i=0;i<n;i++)
                                { out[i].x= 320-(inp[i].x-320);
                               out[i].y= inp[i].y;
                                }
                             break;
                           default :
 printf("\n wrong choice");
                           }

      inp[n]= inp[0];
      out[n] = out[0];
      for(i=0;i<n;i++)
       { setcolor(7);
                          line(inp[i].x,inp[i].y,inp[i+1].x,inp[i+1].y);
                          }
                          for(i=0;i<n;i++)
                          {
                          setcolor(5);
                          line(out[i].x,out[i].y,out[i+1].x,out[i+1].y);
                          }

                          getch();
                          for(i=0;i<n;i++)
                          {
 printf("%d%d",out[i].x,out[i].y);
                         }
                          getch();
                       
                          for(i=0;i<n;i++)
                          {
                          setcolor(5);
                          line(out[i].x,out[i].y,out[i+1].x,out[i+1].y);
                          }
                          getch();
                          closegraph();

                          }

2D Polygon Rotation


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
struct s
{ int x,y ;
};
void main()
{ int n;
int gd= DETECT ,gm;
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 struct s inp[10],out[10];
 printf("\n Enter the no of vertices");
 scanf("%d",&n);
 for(int i=0;i<n;i++)
   { 
scanf ("%d%d",&inp[i].x&inp[i].y);
   }
   int a;
   printf("\n Enter the rotational angle");
   scanf ("%d",&a);
   a= a* (3.14/180);
   for(i=0;i<n;i++)
    { out[i].x= (inp[i].x * cos(a)) - (inp[i].y*sin(a));
      out[i].y = (inp[i].x * sin(a)) + (inp[i].y* cos(a));
      }
      inp[n]= inp[0];
      out[n] = out[0];
      for(i=0;i<n;i++)
       { setcolor(7);
           line(inp[i].x,inp[i].y,inp[i+1].x,inp[i+1].y);
           }

           getch();
           for(i=0;i<n;i++)
           { printf ("%d%d",out[i].x,out[i].y);
           }
           getch();
           for(i=0;i<n;i++)
           {
           setcolor(5);
           line(out[i].x,out[i].y,out[i+1].x,out[i+1].y);
           }
           getch();
           closegraph();
           }

Scaling a 2D Polygon


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
struct s
{ int x,y ;
};
void main()
{ int n;
int gd= DETECT ,gm;
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 struct s inp[10],out[10];
 printf("\n Enter the no of vertices");
 scanf ("%d",&n);
 for(int i=0;i<n;i++)
   { scanf ("%d%d",&inp[i].x,&inp[i].y);
   }
   int sx,sy;
   printf("\n Enter the scaling value");
   scanf ("%d%d",&sx,&sy);
   for(i=0;i<n;i++)
    { out[i].x= inp[i].x * sx;
      out[i].y = inp[i].y * sy;
      }
      inp[n]= inp[0];
      out[n] = out[0];
      for(i=0;i<n;i++)
       { setcolor(7);
                                   line(inp[i].x,inp[i].y,inp[i+1].x,inp[i+1].y);
                                   }

                                   getch();
                                   for(i=0;i<n;i++)
                                   { 
printf ("%d%d",out[i].x,out[i].y);
                                   }
                                   getch();
                                  
                                   for(i=0;i<n;i++)
                                   {
                                   setcolor(5);
                                   line(out[i].x,out[i].y,out[i+1].x,out[i+1].y);
                                   }
                                   getch();
                                   closegraph();
                                   }

Translate a 2D polygon


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<iostream.h>
struct  s
{
 int x,y;
};
void main()
{
            int n;
            int gd=DETECT,gm;
            initgraph(&gd,&gm,"c:\\tc\\bgi");
            struct s inp[10],out[10];
            printf(" Enter no of vertices");
            scanf ("%d",&n);
            for(int i=0;i<n;i++)
                        {
                        scanf ("%d",&inp[i].x);
                        scanf ("%d",&inp[i].y);
                        }
            int tx,ty;
            printf("\n Enter the Transformation value");
            scanf ("%d%d",&tx&ty);
            for(i=0;i<n;i++)
                        {
                        out[i].x=inp[i].x+tx;
                        out[i].y=inp[i].y+ty;
                        }
       inp[n]=inp[0] ;
       out[n]=out[0] ;

       for(i=0;i<n;i++)
                         { 
                            setcolor(7);
                           line(inp[i].x,inp[i].y,inp[i+1].x,inp[i+1].y);
}
for(i=0;i<n;i++)
{
printf ("%d%d", out[i].x,out[i].y);
}
for(i=0;i<n;i++)
{
                           setcolor(4);
                           line(out[i].x,out[i].y,out[i+1].x,out[i+1].y);
                         }
           
            getch();
            closegraph();
}

Mid Point Algorithm (circle Drawing )

#include<graphics.h>
void main ()
{
int x.y.xc,yc,r,pk;
int gd =DETECT,gm;
initgraph (&gd,&gm,"c:\\tc\\bgi");
printf ("\nEnter the center point ");
scanf ("%d%d",&xc,&yc);
printf ("\n Enter the radius");
scanf ("%d",&r);
x=0;
y=r;
pk=(1-r);
while (x<=y)
{
if (pk<0)
{
x++;
pk=(pk+(2*(x+1)+1));
}
else
{
x++;
y--;
pk=pk+(2*(x+1))+1-2*(y+1);
}
putpixel (xc+x,yc+y,5);
putpixel (xc-x,yc+y,4);
putpixel (xc+x,yc-y,7);
putpixel (xc-x,yc-y,8);
putpixel (xc+y,yc+x,5);
putpixel (xc-y,yc+x,6);
putpixel (xc+y,yc-x,5);
putpixel (xc-y,yc-x,8);
getch();
closegraph();
}
}

Bresenhams Algorithm (Line Drawing )

#include<graphics.h>
void main ()
{
int x,y,x0,y0,x1,y1,m,n,p;
int gd=DETECT,gm;
initgraph (&gd,&gm,"c:\\tc\\bgi");
printf ("\n Enter the co-ordinate of starting point");
scanf ("%d%d",&x0,&y0);
printf ("\n Enter the co-ordinate of end point");
scanf ("%d%d",&x1,&y1);
x=x0;
y=y0;
m=(x1-x0);
n=(y1-y0);
p=2*n-m;
while (x!=x1)
{
if (p<0)
{
x=x+1;
p=p+2*n;
putpixel (x,y,5);
}
else
{
x++;
y++;
p=p+2*(n-m);
putpixel (x,y,5);
}
}
getch();
closegraph();
}

Digital Difference algorithm (DDA)


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main ()
{
int i,x,y,x0,y0,x1,y1;
float m;
int gd=DETECT,gm;
initgraph (&gd,&gm,"c:\\tc\\bgi");
printf ("\n Enter the starting point");
scanf ("%d%d",&x0,&y0);
printf ("\n Enter the end point");
scanf ("%d%d",&x1,&y1);
putpixel (x0,y0,5);
m=((y1-y0)/(x1-x0));
x=x0;
y=y0;
if (m<=1)
{
while (x<=x1)
{
x=x+1;
y=y+m;
putpixel (x,floor (y+0.5),6);
}
}
else 
{
while (y<=y1)
{
y=y+1;
x=y*m;
putpixel (floor(x+0.5),y,5);
}
}
getch ();
closegraph();
}