//********************************************************************
// This source file is part of Ninfa3D Engine
// 
// Copyright (C) 2008-2009 Viz_fuerte. All rights reserved.
// 
// LIMITED WARRANTY
// This software is provided "as-is", without any express or implied warranty.
// In no event shall the author be held liable for any damages arising from the use of this software.
// This is a contract. By accepting the Ninfa3D Engine (the SOFTWARE), 
// you (either an individual or a single entity, the LICENSEE) accept the terms and conditions of this licensing agreement.
// 
// If the LICENSEE does not accept the terms and conditions, then LICENSEE must not accept the SOFTWARE.
// If at a later time the LICENSEE decides not to continue licensing the SOFTWARE under these terms and 
// conditions, then the LICENSEE must delete permanently all files constituting the SOFTWARE.
//
// File: Maths.cpp
//********************************************************************

/*--------------------------------------------------------------------
Includos*/

	#include "..\..\Header Files\Ninfa3D.h"

/*--------------------------------------------------------------------
Declaracion global de funciones*/

extern "C"
{

	/*====================================================================
	Crea una luz*/

	DLL_EXPORT float DLL_APIENTRY CurveValue(float nvalue,float ovalue,float increment)
	{
		if(increment > 1)
			ovalue = ovalue - (ovalue - nvalue) / increment;
		if(increment <= 1)
			ovalue = nvalue;
		return ovalue;
	}

	/*====================================================================
	Crea una luz*/

	DLL_EXPORT float DLL_APIENTRY WrapValue(float value,float min,float max)
	{
		if(max - min == 0)
			return min;
		else
		{
			while((value > max)&(value < min))
			{
				if(value < min)
					value = value + (max - min);
				if(value > max)
					value = value - (max - min);
			}
		}

		return value;
	}

	/*====================================================================
	Crea una luz*/

	DLL_EXPORT float DLL_APIENTRY ClampValue(float value,float low,float high)
	{
		if(value < low)
			return low;
		if(value > high)
			return high;

		return value;
	}

	/*====================================================================
	Crea una luz*/

	DLL_EXPORT float DLL_APIENTRY Min(float value1,float value2)
	{
		if(value1 < value2)
			value2 = value1;
		return value2;
	}

	/*====================================================================
	Crea una luz*/

	DLL_EXPORT float DLL_APIENTRY Max(float value1,float value2)
	{
		if(value1 > value2)
			value2 = value1;
		return value2;
	}

	/*====================================================================
	Crea una luz*/

	DLL_EXPORT float DLL_APIENTRY Distance2D(float x1,float y1,float x2,float y2)
	{
		f32 distance;
		f32 dx;
		f32 dy;
		dx = x2 - x1;
		dy = y2 - y1;
		dx = (dx * dx);
		dy = (dy * dy);
		distance = sqrt(dx + dy);
		return distance;
	}

	/*====================================================================
	Crea una luz*/

	DLL_EXPORT float DLL_APIENTRY Distance3D(float x1,float y1,float z1,float x2,float y2,float z2)
	{
		float distance;
		float dx,dy,dz;

		dx = x2 - x1;
		dy = y2 - y1;
		dz = z2 - z1;

		dx = (dx * dx);
		dy = (dy * dy);
		dz = (dz * dz);

		distance = sqrt(dx + dy + dz);

		return distance;
	}

	/*====================================================================
	Crea una luz*/

	DLL_EXPORT int DLL_APIENTRY Rand(int min,int max)
	{
		return min + rand() % (max - min + 1);
	}

}