//	CircleDetector.cpp - Class used to detect when the player's drawing a circle
//					   with the mouse.
//  ----------------------------------------------------------------------------
//	This file is part of 'NiallsAVLib', base code for any kind of audiovisual
//	apps.
//	Copyright (C) 2012  Niall Moody
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#include "CircleDetector.h"

//------------------------------------------------------------------------------
CircleDetector::CircleDetector()
{
	
}

//------------------------------------------------------------------------------
CircleDetector::~CircleDetector()
{
	
}

//------------------------------------------------------------------------------
void CircleDetector::setMousePos(const TwoFloats& mousePos)
{
	points[0] = mousePos;
}

//------------------------------------------------------------------------------
bool CircleDetector::update()
{
	int i;
	TwoFloats centre;
	TwoFloats min, max;
	float minDist, maxDist;
	float distances[NumPoints];

	bool retval = false;

	//Update points.
	for(i=(NumPoints-1);i>0;--i)
		points[i] = points[i-1];

	//Find min and max coords.
	min.x = 1280.0f;
	min.y = 720.0f;
	max = 0.0f;
	for(i=0;i<NumPoints;++i)
	{
		if(points[i].x < min.x)
			min.x = points[i].x;
		if(points[i].y < min.y)
			min.y = points[i].y;
		if(points[i].x > max.x)
			max.x = points[i].x;
		if(points[i].y > max.y)
			max.y = points[i].y;
	}

	//Temp.
	gestureWidth = max.x - min.x;
	gestureHeight = max.y - min.y;

	//Find centre point.
	centre = min + ((max-min) * 0.5f);

	//Get distance of points to the centre.
	for(i=0;i<NumPoints;++i)
		distances[i] = points[i].getDistance(centre);

	//Work out variance of distances.
	minDist = 1280.0f;
	maxDist = 0.0f;
	for(i=0;i<NumPoints;++i)
	{
		if(distances[i] < minDist)
			minDist = distances[i];
		if(distances[i] > maxDist)
			maxDist = distances[i];
	}

	//Really small distances don't match.
	if(minDist > 10.0f)
	{
		//Variance of less than 64px is a match.
		if((maxDist-minDist) < (0.5f * gestureWidth))
			retval = true;
	}

	return retval;
}
