/* Midpoint circle translation in homogeneous coordinate form - Subhranath Chunder */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void circleMidPoint(int,int,int,int,int);
int equ(float,float,int);
void setOthers(int,int,int,int,int,int);
void translatePoint(int,int,int,int,int*,int*);
void setPixel(int,int);
void main()
{
  int x,y,radius,driver=DETECT,mode,xt,yt;
  clrscr();
  /* Mid-point and radius of the circle are accepted */
  printf("Enter the co-ordinate of the centre: ");
  scanf("%d %d",&x,&y);
  printf("Enter the radius of the circle: ");
  scanf("%d",&radius);
  printf("<Press any key to continue>");
  /* Graphics mode initialized */
  initgraph(&driver,&mode,"F:\\tc\\bgi");
  /* Circle drawn with zero translation */
  circleMidPoint(x,y,radius,0,0);
  /* Amount of translation */
  printf("Enter the amount of translation in x-direction: ");
  scanf("%d",&xt);
  printf("Enter the amount of translation in y-direction: ");
  scanf("%d",&yt);
  /* Translated circle is redrawn */
  circleMidPoint(x,y,radius,xt,yt);
  getch();
  closegraph();
}
void circleMidPoint(int xCenter,int yCenter,int radius,int xt,int yt)
{
  int x=0,y=radius;
  /* Sets the points for all octants */
  setOthers(x,y,xCenter,yCenter,xt,yt);
  /* First octant is drawn */
  while(x<y)
  {
     ++x;
     /* Checking whether y or y-1 is closer to the circle, by taking their mid-point */
     if( equ(x,y-(float)1/2,radius) >=0 )
        --y;
     setOthers(x,y,xCenter,yCenter,xt,yt);
  }
}
/* A function to find whether a given point is inside, outside, or on the circle */
int equ(float x,float y,int r)
{
  int res;
  if( pow(x,2)+pow(y,2)-pow(r,2) == 0)
     res=0;
  else if( pow(x,2)+pow(y,2)-pow(r,2) < 0)
     res=-1;
  else if( pow(x,2)+pow(y,2)-pow(r,2) > 0)
     res=1;
  return res;
}
/* A function to set all other points symmetric to the point (x,y) in all the octants */
void setOthers(int x,int y,int xCenter,int yCenter,int xt,int yt)
{
  int xd,yd;
  /* If xt==0 and yt==0 then no translation is required. Otherwise, translate each point. */
  if(xt==0 && yt==0)
  {
     setPixel(xCenter+x,yCenter+y);
     setPixel(xCenter-x,yCenter+y);
     setPixel(xCenter+x,yCenter-y);
     setPixel(xCenter-x,yCenter-y);
     setPixel(xCenter+y,yCenter+x);
     setPixel(xCenter-y,yCenter+x);
     setPixel(xCenter+y,yCenter-x);
     setPixel(xCenter-y,yCenter-x);
  }
  else
  {
     translatePoint(xCenter+x,yCenter+y,xt,yt,&xd,&yd);
     setPixel(xd,yd);
     translatePoint(xCenter-x,yCenter+y,xt,yt,&xd,&yd);
     setPixel(xd,yd);
     translatePoint(xCenter+x,yCenter-y,xt,yt,&xd,&yd);
     setPixel(xd,yd);
     translatePoint(xCenter-x,yCenter-y,xt,yt,&xd,&yd);
     setPixel(xd,yd);
     translatePoint(xCenter+y,yCenter+x,xt,yt,&xd,&yd);
     setPixel(xd,yd);
     translatePoint(xCenter-y,yCenter+x,xt,yt,&xd,&yd);
     setPixel(xd,yd);
     translatePoint(xCenter+y,yCenter-x,xt,yt,&xd,&yd);
     setPixel(xd,yd);
     translatePoint(xCenter-y,yCenter-x,xt,yt,&xd,&yd);
     setPixel(xd,yd);
  }
}
/* Translates the point (x,y) by xt and yt amount, and stores the new point in (xd,yd) */
void translatePoint(int x,int y,int xt,int yt,int *xd,int *yd)
{
  int i,j,a[3][3],b[3],c[3];
  a[0][0]=1; a[0][1]=0; a[0][2]=xt;
  a[1][0]=0; a[1][1]=1; a[1][2]=yt;
  a[2][0]=0; a[2][1]=0; a[2][2]=1;
  b[0]=x;
  b[1]=y;
  b[2]=1;
  for(i=0;i<3;++i)
  {
     c[i]=0;
     for(j=0;j<3;++j)
     {
        c[i]+=a[i][j]*b[j];
     }
  }
  /* Translated point */
  *xd=c[0]/c[2];
  *yd=c[1]/c[2];
}
void setPixel(int x,int y)
{
  putpixel(x,y,2);
}
Friday, August 17, 2007
Mid-point Circle Translation
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment