2013-02-11 07:12:05 +00:00
|
|
|
|
#region Using Statements
|
2011-10-31 05:36:24 +00:00
|
|
|
|
using System;
|
2012-09-30 11:14:44 +00:00
|
|
|
|
using ANX.Framework.NonXNA.Development;
|
2011-10-31 05:36:24 +00:00
|
|
|
|
|
|
|
|
|
#endregion // Using Statements
|
|
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
2011-10-31 05:36:24 +00:00
|
|
|
|
|
|
|
|
|
#region Based on work by the Mono.Xna Team
|
|
|
|
|
/*
|
|
|
|
|
MIT License
|
|
|
|
|
Copyright © 2006 The Mono.Xna Team
|
|
|
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
#endregion License
|
|
|
|
|
|
|
|
|
|
namespace ANX.Framework
|
|
|
|
|
{
|
2012-09-30 11:14:44 +00:00
|
|
|
|
[PercentageComplete(100)]
|
2012-10-13 21:36:46 +00:00
|
|
|
|
[Developer("Glatzemann, rene87")]
|
2012-09-30 11:14:44 +00:00
|
|
|
|
[TestState(TestStateAttribute.TestState.Tested)]
|
2011-10-31 05:36:24 +00:00
|
|
|
|
public static class MathHelper
|
|
|
|
|
{
|
|
|
|
|
public const float E = (float)Math.E;
|
|
|
|
|
public const float Log10E = 0.4342945f;
|
|
|
|
|
public const float Log2E = 1.442695f;
|
|
|
|
|
public const float Pi = (float)Math.PI;
|
2011-11-17 10:05:09 +00:00
|
|
|
|
public const float PiOver2 = (float)(Math.PI * 0.5);
|
|
|
|
|
public const float PiOver4 = (float)(Math.PI * 0.25);
|
2013-02-11 07:12:05 +00:00
|
|
|
|
public const float TwoPi = (float)(Math.PI + Math.PI);
|
2011-11-17 10:05:09 +00:00
|
|
|
|
|
|
|
|
|
private const double OneEightyOverPi = 180.0 / Math.PI;
|
|
|
|
|
private const double PiOverOneEighty = Math.PI / 180.0;
|
2011-10-31 05:36:24 +00:00
|
|
|
|
|
|
|
|
|
public static float Barycentric(float value1, float value2, float value3, float amount1, float amount2)
|
|
|
|
|
{
|
|
|
|
|
return value1 + (value2 - value1) * amount1 + (value3 - value1) * amount2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float CatmullRom(float value1, float value2, float value3, float value4, float amount)
|
|
|
|
|
{
|
|
|
|
|
// Using formula from http://www.mvps.org/directx/articles/catmull/
|
|
|
|
|
// Internally using doubles not to lose precission
|
|
|
|
|
double amountSquared = amount * amount;
|
|
|
|
|
double amountCubed = amountSquared * amount;
|
|
|
|
|
return (float)(0.5 * (2.0 * value2 +
|
|
|
|
|
(value3 - value1) * amount +
|
|
|
|
|
(2.0 * value1 - 5.0 * value2 + 4.0 * value3 - value4) * amountSquared +
|
|
|
|
|
(3.0 * value2 - value1 - 3.0 * value3 + value4) * amountCubed));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Clamp(float value, float min, float max)
|
|
|
|
|
{
|
|
|
|
|
value = (value > max) ? max : value;
|
|
|
|
|
value = (value < min) ? min : value;
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Distance(float value1, float value2)
|
|
|
|
|
{
|
|
|
|
|
return Math.Abs(value1 - value2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Hermite(float value1, float tangent1, float value2, float tangent2, float amount)
|
|
|
|
|
{
|
2011-11-17 10:19:00 +00:00
|
|
|
|
float a2 = amount * amount;
|
|
|
|
|
float asqr3 = amount * a2;
|
|
|
|
|
float a3 = a2 + a2 + a2;
|
|
|
|
|
|
|
|
|
|
return (value1 * (((asqr3 + asqr3) - a3) + 1f)) +
|
|
|
|
|
(value2 * ((-2f * asqr3) + a3)) +
|
|
|
|
|
(tangent1 * ((asqr3 - (a2 + a2)) + amount)) +
|
|
|
|
|
(tangent2 * (asqr3 - a2));
|
2011-10-31 05:36:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static float Lerp(float value1, float value2, float amount)
|
|
|
|
|
{
|
|
|
|
|
return value1 + (value2 - value1) * amount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Max(float value1, float value2)
|
|
|
|
|
{
|
|
|
|
|
return Math.Max(value1, value2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Min(float value1, float value2)
|
|
|
|
|
{
|
|
|
|
|
return Math.Min(value1, value2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float SmoothStep(float value1, float value2, float amount)
|
|
|
|
|
{
|
|
|
|
|
// It is expected that 0 < amount < 1
|
|
|
|
|
// If amount < 0, return value1
|
|
|
|
|
// If amount > 1, return value2
|
2012-09-30 11:14:44 +00:00
|
|
|
|
float result = Clamp(amount, 0f, 1f);
|
|
|
|
|
result = Hermite(value1, 0f, value2, 0f, result);
|
2011-10-31 05:36:24 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float ToDegrees(float radians)
|
|
|
|
|
{
|
|
|
|
|
// This method uses double precission internally,
|
|
|
|
|
// though it returns single float
|
2011-11-17 10:05:09 +00:00
|
|
|
|
return (float)(radians * OneEightyOverPi);
|
2011-10-31 05:36:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float ToRadians(float degrees)
|
|
|
|
|
{
|
|
|
|
|
// This method uses double precission internally,
|
|
|
|
|
// though it returns single float
|
2011-11-17 10:05:09 +00:00
|
|
|
|
return (float)(degrees * PiOverOneEighty);
|
2011-10-31 05:36:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static float WrapAngle(float angle)
|
|
|
|
|
{
|
|
|
|
|
angle = (float)Math.IEEERemainder((double)angle, 6.2831854820251465); //2xPi precission is double
|
2011-11-17 14:34:49 +00:00
|
|
|
|
//if (angle <= -3.141593f)
|
|
|
|
|
//{
|
|
|
|
|
// angle += 6.283185f;
|
|
|
|
|
// return angle;
|
|
|
|
|
//}
|
|
|
|
|
//if (angle > 3.141593f)
|
|
|
|
|
//{
|
|
|
|
|
// angle -= 6.283185f;
|
|
|
|
|
//}
|
2011-10-31 05:36:24 +00:00
|
|
|
|
return angle;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 14:50:03 +02:00
|
|
|
|
|
|
|
|
|
public static int Multiple(int number, int multiplier)
|
|
|
|
|
{
|
|
|
|
|
return (number + multiplier - 1) / multiplier * multiplier;
|
|
|
|
|
}
|
2011-10-31 05:36:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|