/***********************************
 * Thomas Randall
 * Assignment 3
 * CPSC 101 Section 001
 * DUE: November 6, 2015 at midnight
 **********************************/

#include <stdio.h>
#include <stdbool.h>
#include <math.h>

//pixel structure
struct pixel
{
	int r; //red amount
	int g; //green amount
	int b; //blue amount
};

//struct for header
struct size
{
	int width;
	int height;
};

void make_pixel(struct pixel color)
{
	fprintf(stdout, "%c%c%c", color.r, color.g, color.b);
}

void make_ppm_header(struct size header)
{
	fprintf(stdout, "P6\n");
	fprintf(stdout, "%d %d %d\n", header.width, header.height, 255);
}

void circle(struct size header)
{
	make_ppm_header(header);

	struct pixel color1 = {.r = 255, .g = 255, .b = 255}, color2 = {.r = 0, .g = 0, .b = 0};
	//color1 is in circle, color2 is out of circle

	for(int row = 1; row <= header.height; row ++) //loop through the rows
	{
		for(int column = 1; column <= header.width; column ++) //loop through the columns
		{
			float result1 = sqrt(pow((column - 150), 2) + pow((row - 200), 2));
			float result2 = sqrt(pow((column - 350), 2) + pow((row - 200), 2));
			float result3 = sqrt(pow((column - 500), 2) + pow((row - 200), 2));
			/* first circle centered at (200,150) with radius 100 (result1)
			   second circle centered at (200,300) with raidus 75 (result2)
			   third circle centered at (200,500) with raidus 50 (resutl3)
			   within circle is if the square root of ((x - xcenter)squared + (y - ycenter)squared) is less than the radius */
			
			if(result1 < 100 || result2 < 75 || result3 < 50) //in a circle
			{
				make_pixel(color1);
			}
			else //not in a circle
			{
				make_pixel(color2);
			}
		}
	}
}

void diagonal(struct size header)
{
	make_ppm_header(header);
	
	struct pixel color1 = {.r = 255, .g = 0, .b = 0}, color2 = {.r = 0, .g = 0, .b = 255};
	//color1 is the left/top side, color2 is the right/bottom side
	
	for(int row = 1; row <= header.height; row++) //loop through the rows
	{
		for(int column = 1; column <= header.width; column++) //loop through the columns
		{
			if(400 - column > row) //left/top side
			{
				make_pixel(color1);
			}
			else //right/bottom side
			{
				make_pixel(color2);
			}
		}
	}
}

void gradient(struct size header)
{
	make_ppm_header(header);
	
	struct pixel color1 = {.r = 0, .g = 255, .b = 0}, color2 = {.r = 0, .g = 0, .b = 255};
	//color 1 is left extreme, color2 is right extreme
	
	for(int row = 1; row <= header.height; row++) //loop through the rows
	{
		for(int column = 1; column <= header.width; column++) //loop through the columns
		{
			if(column < 250) //left side
			{
				make_pixel(color1);
				color1.b += 1;
				color1.g -= 1;
			}
			else
			{
				make_pixel(color2);
				color2.r +=1;
				color2.b -=1;
			}
		}
		color1.g = 255;
		color1.b = 0;
		color2.r = 0;
		color2.b = 255;
	}
	
}

int main()
{
	bool madePicture = 0;
	struct size header;
	char choiceChar; //YOU NEED TO REPLACE THIS WITH BELOW EVENTUALLY!!!
	//char choiceString[8]; //YOU NEED TO USE THIS INSTEAD EVENTUALLY!!!

	void make_pixel (struct pixel color);
	void make_ppm_header (struct size header);
	void circle (struct size header);


	while(madePicture != 1)
	{
		fprintf(stderr, "Choose a type of picture to print.\nType your choice exactly as it is displayed excluding quotation marks:\nType 'c' for circle\nType 'd' for diagonal\nType 'g' for gradient\n");
		fscanf(stdin, " %c", &choiceChar);
		
	switch(choiceChar)
		{
			case 'c':
				header.width = 600;
				header.height = 400;
				circle(header);
				madePicture = 1;
				break;
			case 'd':
				header.width = 400;
				header.height = 400;
				diagonal(header);
				madePicture = 1;
				break;
			case 'g':
				header.width = 500;
				header.height = 500;
				gradient(header);
				madePicture = 1;
				break;
			default:
				fprintf(stderr, "ERROR! You did not enter a valid choice. Please try again!\n");
				madePicture = 0;
		} //terminates switch
		} //terminates while
	
	return 0;
}