/* hw4p2.c - sample solution problem 2 of homework 4
 *    - draws a smoother diamond with the radius (i.e. half-width) specified
 *      by the user
 *    - this solution requires that you remember a little about how to
 *      graph inequalites from high school geometry class; it is far
 *      from the only way to do it
 *    - this solution is based on the sample solution to hw4p1
 */

#include <stdio.h>
#include <math.h>
#include "simpio.h"

void main(){
  int x,y,r=-1;

  while (r<=0){
    printf("How big a diamond do you want: ");
    r = GetInteger();
  }
  r++; /* compensate for deleting the center row and column; see below */
  printf("Here you go:\n");
  /* use nested for-loops to generate a 2-dimensional coordinate
   * system that runs from -r to +r on the y-axis and -r to +r
   * on the x-axis
   */
  for (y = r ; y >= -r ; y--){
    for (x = -r ; x <= r ; x++){
      /*
       * calculate whether the given coordinates are in the diamond
       * or not - print a '#' if yes, a space if no. This works
       * by checking that the following inequalities all hold:
       *    y <=  x + r  (the region below the line y =  x + r)
       *    y <= -x + r  (the region below the line y = -x + r)
       *    y >= -x + r  (the region above the line y = -x + r)
       *    y >=  x - r  (the region above the line y =  x - r)
       */
      if (x!=0 && y!=0){ /* Suppress printing anything for x or y = 0
                          * in order to get a diamond composed of an
                          * even number of characters. In other words,
                          * don't print the center row and column that
                          * was printed in the diamond made in problem
                          * 1 */
        if (abs(x) + abs(y) == r){
          if((y>0 && x<0) || (y<0 && x>0)){ /* print a '/' in the upper left
                                             * and lower right quadrants */
            printf("/");
          } else {                          /* print a '\' in the upper right
                                             * and lower left quadrants */
            printf("\\");
          }
        } else {
          if (y!=r && y!=-r){ /* no part of the diamond will be on the top
                               * or bottom lines, so don't bother printing
                               * spaces */
            printf(" ");
          }
        }
      }
    }
    if (y!=0 && y!=r && y!=-r) { /* don't print newline on the lines we're
                                  * leaving out */
      printf("\n");
    }
  }
}

