/****************************************************************************
Copyright (c) 2011-2013,WebJet Business Division,CYOU
http://www.genesis-3d.com.cn
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.
****************************************************************************/
namespace ScriptRuntime
{
using System;
///
/// 数学运算类
///
public static class MathHelper
{
///
/// 数学常数 e
///
public const float E = 2.718282f;
///
/// log10 e.
///
public const float Log10E = 0.4342945f;
///
/// log2 e.
///
public const float Log2E = 1.442695f;
///
/// 圆周率pi
///
public const float Pi = 3.141593f;
///
/// 圆周率pi/2
///
public const float PiOver2 = 1.570796f;
///
/// 圆周率pi/4
///
public const float PiOver4 = 0.7853982f;
///
/// 圆周率pi*2
///
public const float TwoPi = 6.283185f;
///
/// 角度转换为弧度所用常量
///
public const float Deg2Rad = 0.01745329f;
///
/// 弧度转换为角度所用常量
///
public const float Rad2Deg = 57.29578f;
///
/// 一个很小的浮点值
///
public const float Epsilon = float.Epsilon;
///
/// 代表正无穷
///
public const float Infinity = float.PositiveInfinity;
///
/// 代表负无穷
///
public const float NegativeInfinity = float.NegativeInfinity;
///
/// 在规定的范围内限定一个值
///
/// 需要限定的值
/// 最小值
/// 最大值
/// 返回值
public static float Clamp(float value, float min, float max)
{
value = (value > max) ? max : value;
value = (value < min) ? min : value;
return value;
}
///
/// 在规定的范围内限定一个值
///
/// 需要限定的值
/// 最小值
/// 最大值
/// 返回值
public static int Clamp(int value, int min, int max)
{
value = (value > max) ? max : value;
value = (value < min) ? min : value;
return value;
}
///
/// Hermite样条插值
///
/// 源值
/// 源切线
/// 源值
/// 源切线
/// 权重
/// Hermite样条插值的结果
public static float Hermite(float value1, float tangent1, float value2, float tangent2, float amount)
{
float num3 = amount;
float num = num3 * num3;
float num2 = num3 * num;
float num7 = ((2f * num2) - (3f * num)) + 1f;
float num6 = (-2f * num2) + (3f * num);
float num5 = (num2 - (2f * num)) + num3;
float num4 = num2 - num;
return ((((value1 * num7) + (value2 * num6)) + (tangent1 * num5)) + (tangent2 * num4));
}
///
/// 两个向量之间的线性插值
///
/// 源值
/// 源值
/// 表明value2的权重,数字0到1之间
/// 结果所得值
public static float Lerp(float value1, float value2, float amount)
{
return (value1 + ((value2 - value1) * amount));
}
///
/// 返回较大的值
///
/// 源值
/// 源值
/// 较大的值
public static float Max(float a, float b)
{
return ((a <= b) ? b : a);
}
///
/// 返回较小的值
///
/// 源值
/// 源值
/// 较小的值
public static float Min(float a, float b)
{
return ((a >= b) ? b : a);
}
///
/// 返回较大的值
///
/// 源值
/// 源值
/// 较大的值
public static int Max(int a, int b)
{
return ((a <= b) ? b : a);
}
///
/// 返回较小的值
///
/// 源值
/// 源值
/// 较小
public static int Min(int a, int b)
{
return ((a >= b) ? b : a);
}
///
/// 用立方等式在两个值之间插值
///
/// 源值
/// 源值
/// 权重
/// 插值结果
public static float SmoothStep(float value1, float value2, float amount)
{
float num = Clamp(amount, 0f, 1f);
return Lerp(value1, value2, (num * num) * (3f - (2f * num)));
}
///
/// 将弧度转换为角度
///
/// 角的弧度值
/// 角的角度值
public static float RadiansToDegrees(float radians)
{
return (radians * 57.29578f);
}
///
/// 将弧度转换为角度
///
/// 角的角度值
/// 角的弧度值
public static float DegreesToRadians(float degrees)
{
return (degrees * 0.01745329f);
}
///
/// 指定角的正弦
///
/// 一个角的弧度表示
/// 角的正弦值,如果角为NaN或者是正无穷或负无穷,返回NaN
public static float Sin(float f)
{
return (float)Math.Sin((double)f);
}
///
/// 指定角的余弦
///
/// 一个角的弧度表示
/// 角的余弦值
public static float Cos(float f)
{
return (float)Math.Cos((double)f);
}
///
/// 指定角的正切
///
/// 一个角的弧度表示
/// 角的正切值,如果角为NaN或者是正无穷或负无穷,返回NaN
public static float Tan(float f)
{
return (float)Math.Tan((double)f);
}
///
/// 指定值的反正弦
///
/// 正弦值
/// 一个角的弧度表示
public static float ASin(float f)
{
return (float)Math.Asin((double)f);
}
///
/// 指定值的反余弦
///
/// 余弦值
/// 一个角的弧度表示
public static float ACos(float f)
{
return (float)Math.Acos((double)f);
}
///
/// 指定值的反正切
///
/// 正切值
/// 一个角的弧度表示
public static float ATan(float f)
{
return (float)Math.Atan((double)f);
}
///
/// Returns the angle whose tangent is the quotient of two specified numbers.
///
/// 点的X坐标
/// 点的Y坐标
/// 一个角的弧度表示, 例如 -π≤θ≤π, 和 tan(θ) = y / x, 点(x, y)在笛卡尔坐标系中.遵守如下规律:
///(x, y)在第一象限 ,0 < θ < π/2.
///(x, y)在第二象限 , π/2 < θ≤π.
///(x, y)在第三象限 , -π < θ < -π/2.
///(x, y)在第四象限 , -π/2 < θ < 0.
///如果点在象限边界,值如下:
///如果X为0,Y为非负, θ = 0.
///如果X为0,Y为负, θ = π.
///如果X为负,Y为0, θ = π/2.
///如果X为正,Y为0, θ = -π/2.
///
public static float ATan2(float a, float b)
{
return (float)Math.Atan2((double)a, (double)b);
}
///
/// 求指定数的平方根
///
/// 指定数值
/// 零或者正数返回结果,负数返回NaN
public static float Sqrt(float f)
{
return (float)Math.Sqrt((double)f);
}
///
/// 求指定数的绝对值
///
/// 指定数
/// 绝对值
public static float Abs(float f)
{
return Math.Abs(f);
}
///
/// 求指定数的绝对值
///
/// 指定数
/// 绝对值
public static int Abs(int value)
{
return Math.Abs(value);
}
///
/// 求指定数的指定次方
///
/// 底数
/// 指数
/// 结果值
public static float Pow(float fBase, float fExponent)
{
return (float)Math.Pow((double)fBase, (double)fExponent);
}
///
/// 数学常量e的指定次方
///
/// 指数
/// 结果值
public static float Exp(float power)
{
return (float)Math.Exp((double)power);
}
///
/// 求指定数的对数
///
/// 指定数
/// 结果
public static float Log(float f)
{
return (float)Math.Log((double)f);
}
///
/// 求指定数的以10为底对数
///
/// 指定数
/// 结果
public static float Log10(float f)
{
return (float)Math.Log10((double)f);
}
///
/// 求比指定数大或相等的最小整数
///
/// 一个数
/// 比指定数大或相等的最小整数
public static float Ceil(float f)
{
return (float)Math.Ceiling((double)f);
}
///
/// 求比指定数小或相等的最大整数
///
/// 一个数
/// 比指定数小或相等的最大整数
public static float Floor(float f)
{
return (float)Math.Floor((double)f);
}
///
/// 求距指定数最近的一个整数
///
/// 一个数.
/// 结果
public static float Round(float f)
{
return (float)Math.Round((double)f);
}
///
/// 求比指定数大或相等的最小整数
///
/// 一个数
/// 比指定数大或相等的最小整数
public static int ICeil(float f)
{
return (int)Math.Ceiling((double)f);
}
///
/// 求比指定数小或相等的最大整数
///
/// 一个数
/// 比指定数小或相等的最大整数
public static int IFloor(float f)
{
return (int)Math.Floor((double)f);
}
///
/// 求距指定数最近的一个整数
///
/// 一个数
/// 距指定数最近的一个整数
public static int IRound(float f)
{
return (int)Math.Round((double)f);
}
///
/// 检测一个数是否是2的次幂
///
/// 一个数
/// 是返回true,不是false
public static bool IsPowerOfTwo(int value)
{
return ((value & (value - 1)) == 0);
}
///
/// 求最接近较大的2的次幂
///
/// 一个数
/// 最接近较大的2的次幂
public static int NextPowerOfTwo(int v)
{
v -= 1;
v |= v >> 16;
v |= v >> 8;
v |= v >> 4;
v |= v >> 2;
v |= v >> 1;
return v + 1;
}
}
}