#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <gd.h>
#include "gdfonts.h"
#include "gdfontt.h"
#include "gdfontl.h"
#include "gdfontl.h"
#include "gdfontmb.h"


float stock[10][3]  = {{2200, 112, 46.8}, {2500, 122, 57.9}, {2800, 132, 70.4}, {3100, 141, 83.4},
					{3400, 149, 96.1}, {3700, 145, 102}, {4000, 143, 109}, {4300, 140, 114},
					{4600, 136, 119}, {4900, 132, 124}};
/*
2200 RPM -112 brake tq, 46.8 brake HP
2500 RPM - 122, 57.9
2800 RPM - 132, 70.4
3100 RPM - 141, 83.4
3400 RPM - 149, 96.1
3700 RPM - 145, 102
4000 RPM - 143, 109
4300 RPM - 140, 114
4600 RPM - 136, 119
4900 RPM - 132, 124
22R, EB 261 Crawler Cam
222/222 Duration, .410/.410 Lift, 110 lobe centerline
Header & Exhaust
1500 RPM - 68.3, brake tq, 19.5 brake HP
1900 RPM - 94.6, 34.2
2300 RPM - 113, 49.5
2700 RPM - 126, 64.9
3100 RPM - 140, 82.6
3500 RPM - 150, 100
3900 RPM - 144, 107
4300 RPM - 140, 115
4700 RPM - 136, 121
5100 RPM - 131, 127
22R, EB 268 Cam
2182/218 Duration, .425/.425 Lift, 110 lobe centerline
Header & Exhaust
1500 RPM - 75.0, brake tq, 21.4 brake HP
1900 RPM - 100, 36.3
2300 RPM - 118, 51.6
2700 RPM - 130, 67.0
3100 RPM - 143, 84.2
3500 RPM - 150, 99.8
3900 RPM - 144, 107
4300 RPM - 140, 115
4700 RPM - 136, 121
5100 RPM - 131, 127
22R, CC252S Cam
204/204 Duration, .409/.409 Lift, 110 lobe centerline
Header & Exhaust
1500 RPM - 98.8, brake tq, 28.2 brake HP
1900 RPM - 117, 42.4
2200 RPM - 131, 57.3
2700 RPM - 140, 71.9
3100 RPM - 145, 85.8
3500 RPM - 147, 97.8
3900 RPM - 144, 106
4300 RPM - 138, 113
4700 RPM - 132, 118
5100 RPM - 120, 117

*/


float eb261[10][3]  = {{1500, 68.3, 19.5}, {1900, 94.6, 34.2}, {2300, 113, 49.5}, {2700, 126, 64.9},
					{3100, 140, 82.6}, {3500, 150, 100}, {3900, 144, 107}, {4300, 140, 115}, {4700, 136, 121}, {5100, 131, 127}};
float eb268[10][3]  = {{1500, 75.0, 21.4}, {1900, 100, 36.3}, {2300, 118, 51.6}, {2700, 130, 67.0}, {3100, 143, 84.2},
					{3500, 150, 99.8}, {3900, 144, 107}, {4300, 140, 115}, {4700, 136, 121}, {5100, 131, 127}};
float cc252s[10][3] = {{1500, 98.8, 28.2}, {1900, 117, 42.4}, {2200, 131, 57.3}, {2700, 140, 71.9}, {3100, 145, 85.8},
						{3500, 147, 97.8}, {3900, 144, 106}, {4300, 138, 113}, {4700, 132, 118}, {5100, 120, 117}};


gdPoint points1[10];
gdPoint points2[10];
gdPoint points3[10];
gdPoint points4[10];

gdPoint points1b[10];
gdPoint points2b[10];
gdPoint points3b[10];
gdPoint points4b[10];
//gdPoint is int pair



//gdPointPtr points
//gdPoint points[3];
///gdImageOpenPolygon



int main(int argc, char **argv){
	gdImagePtr im;
	FILE *out;
	int num1 = 0;
	int num2 = 0;
	int width = 1080;
	int height = 1080;
	im = gdImageCreate(width, height);
	int white = gdImageColorAllocate(im, 255, 255, 255);
	int black = gdImageColorAllocate(im, 0, 0, 0);
	int red = gdImageColorAllocate(im, 255, 0, 0);
	int green = gdImageColorAllocate(im, 0, 255, 0);

	int blue = gdImageColorAllocate(im, 0, 0, 255);
	int blue2 = gdImageColorAllocate(im, 0, 0, 225);

	int brown = gdImageColorAllocate(im, 148, 74, 0);
	int brown2 = gdImageColorAllocate(im, 128, 64, 0);

	int grey = gdImageColorAllocate(im, 148, 148, 148);
	int grey2 = gdImageColorAllocate(im, 128, 128, 128);

	int orange = gdImageColorAllocate(im, 255, 128, 0);
	int orange2 = gdImageColorAllocate(im, 225, 108, 0);

	int purple = gdImageColorAllocate(im, 255, 0, 255);
	int yellow = gdImageColorAllocate(im, 255, 255, 0);
	int styleDashed[8];
    styleDashed[0] = yellow;
    styleDashed[1] = gdTransparent;
    styleDashed[2] = yellow;
    styleDashed[3] = gdTransparent;
    styleDashed[4] = black;
    styleDashed[5] = gdTransparent;
    styleDashed[6] = black;
    styleDashed[7] = gdTransparent;

        gdImageSetStyle(im, styleDashed, 8);

	//gdImageFilledRectangle(im, 1, 1, union_width, union_height, blue);
	int emptyvarstr;
	char labelstring[10];
	float tmp = 0;
	float highest_rpm = 5100;
	float highest_tq = 150;
	float width_area = (float)width-200;
	float height_area = (float)height-200;
	emptyvarstr = sprintf( labelstring, "%d", num1);

	gdImageString(im, gdFontLarge,   (int)floor(width/2)-100   , 50        , (unsigned char *)"22R CAM Comparison" , black);
	gdImageLine(im, 100, 100, 100, height-100, black);
	gdImageLine(im, 100, height-100, width-100, height-100, black);
	gdImageString(im, gdFontMediumBold,   (int)floor(width/2)   ,  height-25        , (unsigned char *)"RPM" , red);
	gdImageString(im, gdFontMediumBold,   25   ,   (int)floor(height/2)+25        , (unsigned char *)"HP/TQ" , green);


	//print rpm labels
	num2 = 0;
	for(num1=0; num1<11; num1++){

		emptyvarstr = sprintf( labelstring, "%d", num2);
		tmp = (float)num1/10;
		tmp *= width_area;
		tmp = (float)tmp+100;

		gdImageString(im, gdFontSmall,   (int)floor(tmp)   ,   height-50 , (unsigned char *)labelstring , red);
		num2+=highest_rpm/10;
	}
	//printf hp/tq labels
	num2 = 0;
	for(num1=0; num1<11; num1++){
		emptyvarstr = sprintf( labelstring, "%d", num2);
		tmp = (float)num1/10;
		tmp *= (float)height-200;
		tmp = height-100-tmp;
		gdImageLine(im, 100, (int)floor(tmp), width-100, (int)floor(tmp), gdStyled);
		gdImageString(im, gdFontSmall,   50, (int)floor(tmp), (unsigned char *)labelstring , green);
		num2+=15;
	}


	for(num1=0; num1<10; num1++){

		tmp = stock[num1][0]/highest_rpm;
		tmp *= width_area;
		points1[num1].x  = (int)floor(tmp);
		points1[num1].x += 100;
		tmp = stock[num1][1]/highest_tq;
		tmp *= height_area;
		tmp = height-100-tmp;
		points1[num1].y  = (int)floor(tmp);
		//points1[num1].y += height-100;

		tmp = stock[num1][0]/highest_rpm;
		tmp *= width_area;
        points1b[num1].x = (int)floor(tmp);
		points1b[num1].x += 100;
		tmp = stock[num1][2]/highest_tq;
		tmp *= height_area;
		tmp = height-100-tmp;
        points1b[num1].y = (int)floor(tmp);
		//points1b[num1].y += height-100;
		tmp = eb261[num1][0]/highest_rpm;
		tmp *= width_area;
        points2[num1].x  = (int)floor(tmp);
		points2[num1].x += 100;
		tmp = eb261[num1][1]/highest_tq;
		tmp *= height_area;
		tmp = height-100-tmp;
        points2[num1].y  = (int)floor(tmp);
		//points2[num1].y += height-100;
		tmp = eb261[num1][0]/highest_rpm;
		tmp *= width_area;
        points2b[num1].x = (int)floor(tmp);
		points2b[num1].x += 100;
		tmp = eb261[num1][2]/highest_tq;
		tmp *= height_area;
		tmp = height-100-tmp;
        points2b[num1].y = (int)floor(tmp);
		//points2b[num1].y += height-100;
		tmp = eb268[num1][0]/highest_rpm;
		tmp *= width_area;
        points3[num1].x  = (int)floor(tmp);
		points3[num1].x += 100;
		tmp = eb268[num1][1]/highest_tq;
		tmp *= height_area;
		tmp = height-100-tmp;
        points3[num1].y  = (int)floor(tmp);
		//points3[num1].y += height-100;
		tmp = eb268[num1][0]/highest_rpm;
		tmp *= width_area;
        points3b[num1].x = (int)floor(tmp);
		points3b[num1].x += 100;
		tmp = eb268[num1][2]/highest_tq;
		tmp *= height_area;
		tmp = height-100-tmp;
        points3b[num1].y = (int)floor(tmp);
		//points3b[num1].y += height-100;
		tmp = cc252s[num1][0]/highest_rpm;
		tmp *= width_area;
        points4[num1].x  = (int)floor(tmp);
		points4[num1].x += 100;
		tmp = cc252s[num1][1]/highest_tq;
		tmp *= height_area;
		tmp = height-100-tmp;
        points4[num1].y  = (int)floor(tmp);
		//points4[num1].y += height-100;
		tmp = cc252s[num1][0]/highest_rpm;
		tmp *= width_area;
        points4b[num1].x = (int)floor(tmp);
		points4b[num1].x += 100;
		tmp = cc252s[num1][2]/highest_tq;
		tmp *= height_area;
		tmp = height-100-tmp;
        points4b[num1].y = (int)floor(tmp);
		//points4b[num1].y += height-100;


	}
		//stock tq   gdPointPtr
		gdImageOpenPolygon(im, points1, 10, blue);
		//gdImageFilledRectangle(im, stock[num1][0]/10+100, stock[num1][1]+100, 1, 1, blue);
		//stock hp
		gdImageOpenPolygon(im, points1b, 10, blue2);
		//gdImageFilledRectangle(im, stock[num1][0]/10+100, stock[num1][2]+100, 1, 1, blue2);

		//eb261 tq
		gdImageOpenPolygon(im, points2, 10, brown);
		//gdImageFilledRectangle(im, eb261[num1][0]/10+100, eb261[num1][1]+100, 1, 1, brown);
		//eb261 hp
		gdImageOpenPolygon(im, points2b, 10, brown2);
		//gdImageFilledRectangle(im, eb261[num1][0]/10+100, eb261[num1][2]+100, 1, 1, brown2);
		//eb268 tq
		gdImageOpenPolygon(im, points3, 10, grey);
		//gdImageFilledRectangle(im, eb268[num1][0]/10+100, eb268[num1][1]+100, 1, 1, grey);
		//eb268 hp
		gdImageOpenPolygon(im, points3b, 10, grey2);
		//gdImageFilledRectangle(im, eb268[num1][0]/10+100, eb268[num1][2]+100, 1, 1, grey2);
		//cc252s tq
		gdImageOpenPolygon(im, points4, 10, orange);
		//gdImageFilledRectangle(im, cc252s[num1][0]/10+100, cc252s[num1][1]+100, 1, 1, orange);
		//cc252s hp
		gdImageOpenPolygon(im, points4b, 10, orange2);
		//gdImageFilledRectangle(im, cc252s[num1][0]/10+100, cc252s[num1][2]+100, 1, 1, orange2);

	

	//key/legend

	gdImageFilledRectangle(im,       width-25,    100+height/10*1, width-25+5, 100+height/10*1+5, blue);
	gdImageString(im, gdFontSmall,   width-100,   100+height/10*1  , (unsigned char *)"stock tq" , blue);
    gdImageFilledRectangle(im,       width-25,    100+height/10*2, width-25+5, 100+height/10*2+5, blue2);
    gdImageString(im, gdFontSmall,   width-100,   100+height/10*2  , (unsigned char *)"stock hp" , blue2);
    gdImageFilledRectangle(im,       width-25,    100+height/10*3, width-25+5, 100+height/10*3+5, brown);
    gdImageString(im, gdFontSmall,   width-100,   100+height/10*3  , (unsigned char *)"eb261 tq" , brown);
    gdImageFilledRectangle(im,       width-25,    100+height/10*4, width-25+5, 100+height/10*4+5, brown2);
    gdImageString(im, gdFontSmall,   width-100,   100+height/10*4  , (unsigned char *)"eb261 hp" , brown2);
    gdImageFilledRectangle(im,       width-25,    100+height/10*5, width-25+5, 100+height/10*5+5, grey);
    gdImageString(im, gdFontSmall,   width-100,   100+height/10*5  , (unsigned char *)"eb268 tq" , grey);
    gdImageFilledRectangle(im,       width-25,    100+height/10*6, width-25+5, 100+height/10*6+5, grey2);
    gdImageString(im, gdFontSmall,   width-100,   100+height/10*6  , (unsigned char *)"eb268 hp" , grey2);
    gdImageFilledRectangle(im,       width-25,    100+height/10*7, width-25+5, 100+height/10*7+5, orange);
    gdImageString(im, gdFontSmall,   width-100,   100+height/10*7  , (unsigned char *)"cc252s tq" , orange);
    gdImageFilledRectangle(im,       width-25,    100+height/10*8, width-25+5, 100+height/10*8+5, orange2);
    gdImageString(im, gdFontSmall,   width-100,   100+height/10*8  , (unsigned char *)"cc252s hp" , orange2);


    out = fopen("cams.png", "w");
    gdImageInterlace(im, 1);
    gdImagePng(im, out);
    fclose(out);
    gdImageDestroy(im);
	return 0;
}