Cercle discret (point par point) en c++

Contenu du snippet

Ce programme permet de dessiner un cercle discret (point par point) et d?arrondis les cordonnées réels

Source / Exemple :


int xd=0,yd=0;
int n=0;
int s=0;
/* Plot generates a point of the circle */
void plot(x,y)
int x,y;
{
  s++;
  cout<<"Val Of X= "<<x+xd<< " and Val of y= "<<y+yd;
}
//------------------------------------------------------void main(void)
{
  s=0;
  int ri,x,y; /* Integer part of radius point */

 float a,rph,rpxo,rmxo,rpyo,rmyo; /* constants */
 float xoph,xomh,yoph,yomh;
 float r,xo,yo; /* radius and center */
 double d; /* control value */
 float tmp,tmp1; /* temp. value */
 n=0;
//Provide the radius and the center:R,xo,yo
 
 cout<<" val of R= ";
 sin>>r;
 
 xo=0;yo=0 ;
 ri=(int)r; /* integer part of the radius */
 r=r-0.5; /* circle of radius r'=r-0.5 is generated */
 a=r-ri; /* corresponding fractional part of radius */
/* CIRCLE WITH SMALL RADIUS */
if (ri<=1)
{
    tmp=2*r+1;
    for(x=-2;x<=2;x++) /* the generation is brute force */
    for(y=-2;y<=2;y++)
    {
       tmp1=x*x+y*y-r*r;
       if ((tmp1>=0) && (tmp1<tmp))
       plot(x,y);
    }
}
else
{
   // ri>1
   /* GENERAL CASE */
   /* Initialization of loop constants */
   rph=r+0.5;
   rpxo=r; rmxo=r-xo; rpyo=r+yo; rmyo=r-yo;
   xoph=xo+0.5; xomh=xo-0.5; yoph=yo+0.5; yomh=yo-0.5;
/* STARTING POINT COMPUTATION */
    y=0;
    d=((ri-rpxo)*(ri+rmxo)+yo*yo)/2.0; /* Initial value of d for (ri,0) */
   if (d<(a+xo)) /* does (ri+1,0) belong to the circle ? */
{ plot(ri+1,0); /* (ri+1,0) does belong to the circle */
if (d>=0) { plot(ri,0); x=ri;} /* (ri,0) belongs also to the circle */
else { x=ri+1; d+=ri-xomh;} /* (ri,0) doesn't belong to the circle */
}
else if ((d>=0) && (d<rph)) /* does (ri,0) belong to the circle ? */
{ plot(ri,0);
  x=ri;
}
else /* if neither (ri+1,0) nor (ri,0) belong to the
circle then (ri-1,0) does */
 {
   x=ri-1; plot(x,0);
   d+=xoph-ri;
 }
/* END OF STARTING POINT PHASE */
/* GENERATION OF QUADRANT 1 */
while (x>0)
 {
   if (d<(rpyo-y)) /* Type (a) ? */
 {  d+=y-yomh;
 y++;
 plot(x,y);
}
else
{ d+=xoph-x;
x--;
if (d>=0) plot(x,y); /* Type (b) ? */
else
{ d+=y-yomh; /* Type (c) !*/
y++;
plot(x,y);
}
}
}
/* END OF GENERATION OF QUADRANT 1 */
/* LIMIT POINTS BETWEEN QUADRANT #1 AND QUADRANT #2 */
if (d<(rpyo-y)) /* Does (0,y+1) belong to the circle ? */
{ d+=xoph; plot(0,y+1); x--; plot(-1,y); }
/* GENERATION OF QUADRANT 2 */
while (y>0 )
{ if (d<(rmxo+x))
{
d+=xoph-x;
x--;
plot(x,y);
}
else
{ d+=yoph-y;
y--;
if (d>=0) plot(x,y);
else
{ d+=xoph-x;
  x--;
plot(x,y);
}
}
}
/* END OF GENERATION OF QUADRANT #2 */
/* LIMIT POINTS BETWEEN QUADRANT #2 AND QUADRANT #3 */
if (d<(rpxo+x))
{ d+=yoph; plot(x-1,0); y--; plot(x,-1); }
/* GENERATION OF QUADRANT #3 */
while (x<0)
{ if (d<(rmyo+y))
{ d+=yoph-y;

y--;
plot(x,y);
}
else
{ d+=x-xomh;
x++;
if (d>=0) plot(x,y);
else
 { d+=yoph-y;
   y--;
   plot(x,y);
 }
}
}
/* END OF GENERATION OF QUADRANT #3 */
/* LIMIT POINTS BETWEEN QUADRANT #3 AND QUADRANT #4 */
if (d<(rmyo+y))
{ d-=xomh; plot(0,y-1); x++; plot(1,y); }
/* GENERATION OF QUADRANT #4 */
while (y<0)
{ if (d<(rpxo-x))
  { d+=x-xomh;
    x++;
    plot(x,y);
  }
else
{  d+=y-yomh;
   y++;
   if ((d>=0)&&(y)) plot(x,y);
   else
{  d+=x-xomh;
   x++;
   if (y) plot(x,y);
}
}
}
/* END OF GENERATION OF QUADRANT #4 */
} /* END OF THE GENERAL CASE */
}
//------------------------------------------------------
  GO and test

Conclusion :


pour plus de détailles contactez moi à l'adresse suivante: bob_carlos2006@yahoo.fr

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.