// midterm_structure.cpp - midterm structure
#include <iostream>
#include <fstream>
#include <math.h> // you will need this to use the abs() function in your
                  // line function.
using namespace std;

// Determines the output image's dimensions. 
// Make this as large as you like.
const int WIDTH = 200;
const int HEIGHT = 200;

void initArray(unsigned char col, unsigned char img[]);
int iround(double x);
void drawHLine(int xp, int yp, int length, unsigned char col, unsigned char img[]);

int main() {
  char filename[20];

  // This array will serve as an "image buffer." This holds all 
  // the pixel data that you will eventually write to your RAW file.
  // All drawing functions will change pixel data within this
  // array.
  unsigned char imgArray[WIDTH*HEIGHT];
  
  // Allowing the user to enter the RAW file that will be
  // output after we draw to it.
  cout << "Enter a filename (e.g. pic.raw):";
  cin >> filename;
  ofstream fout(filename);
  
  // Calling a function to initialize our array. Pass
  // in a color, and it will fill the array with that color.
  // Like setting a background color.
  initArray(0,imgArray);

  drawHLine(50, 25, 100, 255, imgArray);   

  // THIS IS WHERE ALL YOU WILL CALL YOUR FUNCTIONS THAT WILL
  // DRAW YOUR SHAPES. NOTE THAT THE ORDER YOU CALL THESE
  // FUNCTIONS WILL AFFECT WHAT DRAWS OVER WHAT.


  // Taking your "image buffer" and writing it to your 
  // RAW file.
  for(int i=0;i<WIDTH*HEIGHT;i++) {
    fout << imgArray[i];
  }
  
  fout.close();

}

//********************************************************
// initArray: this function fills your array with 'col',
//   a value the user passes in.
//********************************************************
void initArray(unsigned char col, unsigned char img[]) {

  for(int i=0;i<WIDTH*HEIGHT;i++) {
    img[i] = col;
  }

}

//********************************************************
// iround: this takes in a decimal, and rounds it to the 
//   closest integer. You will need this in your line
//   function.
//********************************************************
int iround(double x)
{
  return (int)floor(x + 0.5);
}

//********************************************************
// drawHLine: 
//********************************************************
void drawHLine(int xp, int yp, int length, unsigned char col, unsigned char img[]) {

  for(int x=xp;x<xp+length;x++) {
    img[yp*WIDTH + x] = col;
  }

}

