// circle.cpp - Printing out a circle. Since rows and columns take
//              different space on the screen, the user can specify 
//              different radiuses on the x (xMax) and y (yMax) axis.
//              The user can also specify a displacement xdelta from 
//              the left margin and a limit maxCol on the rightmost
//              column.

#include <iostream>
extern "C" {             // This is needed since math.h is for a C library
#include <math.h>
}

int round(float x){      // I could not find a round function so I made one up
   if ((x - (int)x) > 0.5)
      return ((int)x + 1);
   else
      return ((int)x);
}

// Print out n copies of character c
void printnc(char c, int n){
    for (int lcv=0; lcv < n; lcv++)
        cout << c;
}


// print a circle according to quantities xMax, yMax, xDelta, maxCol.
void printCircle(int xMax, int yMax, int xDelta, int maxCol){
   double dy;  // Proportion of y axis already done (from 0 to 1.0)
               // relative to yMax
   double dx;  // Proportion of x axis of current row relative to xMax
               // dx**2 + dy**2 = 1.0
   int left;   // # of spaces before leftmost character of circle
   int inCircle; // # of characters in current line of circle

   for (int lcv = 1; lcv <= 2*yMax; lcv++){
      dy = fabs(1.0 - ((float)lcv / yMax));
      dx = sqrt(1.0 - dy*dy);
      left = xDelta + round(xMax*(1.0 - dx));
      printnc(' ', left);
      inCircle = round(2 * xMax * dx);
      if ((left+inCircle) > maxCol)
         inCircle = maxCol - left;
      printnc('X', inCircle);
      cout << endl;
   }
}

void main(void){
   int xMax, yMax, xDelta, maxCol;
   cout << "Enter value of xMax: ";
   cin >> xMax;
   cout << "Enter value of yMax: ";
   cin >> yMax;
   cout << "Enter value of xDelta: ";
   cin >> xDelta;
   cout << "Enter value of maxCol: ";
   cin >> maxCol;
   printCircle(xMax, yMax, xDelta, maxCol);
}
