#include "VHistogram.h"
#include <algorithm>
#include <vector>
#include <math.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <iostream>

using namespace std;

VHistogram::VHistogram(int * hist,int length,int x,int y,int width,int height)
{
	this->hist = hist;
	this->length = length;
	this->width = width;
	this->height = height;
	this->x = x;
	this->y = y;
	int k = (int)ceil((double)this->length/this->width);
	for(int i=1; i<this->length-k+1; i+=k) {//start on 1 to avoid outliers
		int sum = 0;
		for(int j=i; j<i+k; j++) {
			sum+=this->hist[j];
		}
		this->clamped_hist.push_back(sum);
	}
	this->peak = *max_element(this->clamped_hist.begin(),this->clamped_hist.end());
	for(vector<int>::iterator it = this->clamped_hist.begin(); it!=this->clamped_hist.end(); it++) {
		(*it) = (int)ceil(((double)(*it)*this->height/this->peak));
	}
}

void VHistogram::draw() {
	glBegin(GL_LINES);
		int i = 0;
		for(vector<int>::iterator it = this->clamped_hist.begin(); it!=this->clamped_hist.end(); it++) {
			glVertex2f(x+i,y+this->height);
			glVertex2f(x+i,y+this->height-(*it));
			i++;
		}
	glEnd();
}

VHistogram::~VHistogram(void)
{
	delete this->hist;
	this->clamped_hist.clear();
}
