206 lines
8.8 KiB
C#
206 lines
8.8 KiB
C#
#region Using Statements
|
|
using System;
|
|
|
|
#endregion // Using Statements
|
|
|
|
#region License
|
|
|
|
//
|
|
// This file is part of the ANX.Framework created by the "ANX.Framework developer group".
|
|
//
|
|
// This file is released under the Ms-PL license.
|
|
//
|
|
//
|
|
//
|
|
// Microsoft Public License (Ms-PL)
|
|
//
|
|
// This license governs use of the accompanying software. If you use the software, you accept this license.
|
|
// If you do not accept the license, do not use the software.
|
|
//
|
|
// 1.Definitions
|
|
// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning
|
|
// here as under U.S. copyright law.
|
|
// A "contribution" is the original software, or any additions or changes to the software.
|
|
// A "contributor" is any person that distributes its contribution under this license.
|
|
// "Licensed patents" are a contributor's patent claims that read directly on its contribution.
|
|
//
|
|
// 2.Grant of Rights
|
|
// (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations
|
|
// in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to
|
|
// reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution
|
|
// or any derivative works that you create.
|
|
// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in
|
|
// section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed
|
|
// patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution
|
|
// in the software or derivative works of the contribution in the software.
|
|
//
|
|
// 3.Conditions and Limitations
|
|
// (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
|
|
// (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your
|
|
// patent license from such contributor to the software ends automatically.
|
|
// (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution
|
|
// notices that are present in the software.
|
|
// (D) If you distribute any portion of the software in source code form, you may do so only under this license by including
|
|
// a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or
|
|
// object code form, you may only do so under a license that complies with this license.
|
|
// (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees,
|
|
// or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the
|
|
// extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a
|
|
// particular purpose and non-infringement.
|
|
|
|
#endregion // License
|
|
|
|
#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
|
|
{
|
|
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;
|
|
public const float PiOver2 = (float)(Math.PI / 2.0);
|
|
public const float PiOver4 = (float)(Math.PI / 4.0);
|
|
public const float TwoPi = (float)(Math.PI * 2.0);
|
|
|
|
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)
|
|
{
|
|
// First we check to see if we're greater than the max
|
|
value = (value > max) ? max : value;
|
|
|
|
// Then we check to see if we're less than the min.
|
|
value = (value < min) ? min : value;
|
|
|
|
// There's no check to see if min > max.
|
|
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)
|
|
{
|
|
// All transformed to double not to lose precission
|
|
// Otherwise, for high numbers of param:amount the result is NaN instead of Infinity
|
|
double v1 = value1, v2 = value2, t1 = tangent1, t2 = tangent2, s = amount, result;
|
|
double sCubed = s * s * s;
|
|
double sSquared = s * s;
|
|
|
|
if (amount == 0f)
|
|
result = value1;
|
|
else if (amount == 1f)
|
|
result = value2;
|
|
else
|
|
result = (2 * v1 - 2 * v2 + t2 + t1) * sCubed +
|
|
(3 * v2 - 3 * v1 - 2 * t1 - t2) * sSquared +
|
|
t1 * s +
|
|
v1;
|
|
return (float)result;
|
|
}
|
|
|
|
|
|
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
|
|
float result = MathHelper.Clamp(amount, 0f, 1f);
|
|
result = MathHelper.Hermite(value1, 0f, value2, 0f, result);
|
|
return result;
|
|
}
|
|
|
|
public static float ToDegrees(float radians)
|
|
{
|
|
// This method uses double precission internally,
|
|
// though it returns single float
|
|
// Factor = 180 / pi
|
|
return (float)(radians * 57.295779513082320876798154814105);
|
|
}
|
|
|
|
public static float ToRadians(float degrees)
|
|
{
|
|
// This method uses double precission internally,
|
|
// though it returns single float
|
|
// Factor = pi / 180
|
|
return (float)(degrees * 0.017453292519943295769236907684886);
|
|
}
|
|
|
|
|
|
public static float WrapAngle(float angle)
|
|
{
|
|
angle = (float)Math.IEEERemainder((double)angle, 6.2831854820251465); //2xPi precission is double
|
|
if (angle <= -3.141593f)
|
|
{
|
|
angle += 6.283185f;
|
|
return angle;
|
|
}
|
|
if (angle > 3.141593f)
|
|
{
|
|
angle -= 6.283185f;
|
|
}
|
|
return angle;
|
|
}
|
|
|
|
}
|
|
}
|