/* hw4p1.c - sample solution problem 1 of homework 4
 *    - draws a diamond with the radius (i.e. half-width) specified
 *      by the user out of #'s
 *    - 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
 */

#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();
  }
  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)
       *
       *    These can be checked literally with 4 inequalities
       *    connected by &&, or with some algebra can be boiled
       *    down to the single test given below
       */
      if (abs(x) + abs(y) <= r){
        printf("#");
      } else {
        printf(" ");
      }
    }
    printf("\n");
  }
}

