view annotationCalculatorSrc/Histogram.cpp @ 45:d23685b9e766

Fixed the alignment error caluculations and added histogram plotting
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Tue, 08 May 2012 21:53:11 +0100
parents
children ba36a1721538
line wrap: on
line source
/*
 *  Histogram.cpp
 *  annotationResultCalculator
 *
 *  Created by Andrew on 08/05/2012.
 *  Copyright 2012 QMUL. All rights reserved.
 *
 */

#include "Histogram.h"

Histogram::Histogram(){
screenHeight = ofGetHeight();
}

void Histogram::createHistogram(const int& binWidthIn, const int& numberofBinsIn, DoubleVector& data){
	numberofBins = numberofBinsIn;
	binWidth = binWidthIn;
	histogram.clear();
	histogram.assign(numberofBins, 0);
	maximum = 0;
	double binPoint;
	int bin = 0;
	for (int i = 0;i < data.size();i++){
		//find which bin it falls into.
		//5 bins, width is 10
		//then we start around zero for the 5+1/2 th, i.e. 3rd bin
		//zero is the 5/2 th pt
		
		bin = 0;
		binPoint = -1.0 * binWidth * ((double)numberofBins/2);
		
		if (data[i] >= binPoint){//i.e. falls inside
			while (data[i] > (binPoint + binWidth) && bin < numberofBins) {
				//printf("data pt %f bin %i binPt %.1f\n", data[i], bin, binPoint);
				binPoint += binWidth;
				bin++;				
			}
			
			printf("data pt %f bin %i binPt %.1f\n", data[i], bin, binPoint);
			
			if (data[i] <= binPoint + binWidth){//in case outside range
				histogram[bin]++;
			}
		}
	}
	
	for (int k = 0; k < histogram.size();k++){
		
		if (histogram[k] > maximum)
			maximum = histogram[k];
		
		printf("HISTO[%i] = %i\n", k, histogram[k]);
	}
	
	
}

void Histogram::plotHistogram(){
	plotHistogram(maximum);
}

void Histogram::plotHistogram(const double& maxHeight){
	
	double width = ofGetWidth();
	double height = ofGetHeight();
	screenHeight = ofGetHeight();
	if (numberofBins > 0 && maxHeight > 0){
		width /= numberofBins;
		height /= maxHeight;
		
		for (int i = 0;i < numberofBins;i++){
			ofRect(i * width, getY(histogram[i]*height), width, histogram[i]*height);
		}
		
	}
	
}


int Histogram::getY(const int& y){
	return screenHeight - y;
}