From 0b4ef1820d8789df56abbc95e2be54863066d481 Mon Sep 17 00:00:00 2001 From: Gericom Date: Tue, 4 Nov 2014 18:44:52 +0100 Subject: [PATCH] Added MathUtil --- 3DS/DSP/ADPCM.cs | 12 +++------ LibEveryFileExplorer/3D/Triangle.cs | 2 +- LibEveryFileExplorer/Collections/Vector2.cs | 2 +- LibEveryFileExplorer/Collections/Vector3.cs | 2 +- LibEveryFileExplorer/Collections/Vector4.cs | 2 +- .../LibEveryFileExplorer.csproj | 1 + LibEveryFileExplorer/Math/MathUtil.cs | 27 +++++++++++++++++++ MarioKart/MKDS/NKMD.cs | 3 ++- NDS/SND/ADPCM.cs | 12 +++------ 9 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 LibEveryFileExplorer/Math/MathUtil.cs diff --git a/3DS/DSP/ADPCM.cs b/3DS/DSP/ADPCM.cs index c036945..5e1f930 100644 --- a/3DS/DSP/ADPCM.cs +++ b/3DS/DSP/ADPCM.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using LibEveryFileExplorer.Math; namespace _3DS.DSP { @@ -39,12 +40,12 @@ namespace _3DS.DSP if (high >= 8) high -= 16; if (low >= 8) low -= 16; double val = (((high * Scale) << 11) + 1024.0 + (Coef1 * Last1 + Coef2 * Last2)) / 2048.0; //>> 11; - short samp = Clamp((int)val, short.MinValue, short.MaxValue); + short samp = (short)MathUtil.Clamp((int)val, short.MinValue, short.MaxValue); DataOut.Add(samp); Last2 = Last1; Last1 = val; val = (((low * Scale) << 11) + 1024.0 + (Coef1 * Last1 + Coef2 * Last2)) / 2048.0;//>> 11; - samp = Clamp((int)val, short.MinValue, short.MaxValue); + samp = (short)MathUtil.Clamp((int)val, short.MinValue, short.MaxValue); DataOut.Add(samp); Last2 = Last1; Last1 = val; @@ -52,12 +53,5 @@ namespace _3DS.DSP } return DataOut.ToArray(); } - - private static short Clamp(int value, int min, int max) - { - if (value < min) value = min; - if (value > max) value = max; - return (short)value; - } } } diff --git a/LibEveryFileExplorer/3D/Triangle.cs b/LibEveryFileExplorer/3D/Triangle.cs index 18e23bf..c1c14ce 100644 --- a/LibEveryFileExplorer/3D/Triangle.cs +++ b/LibEveryFileExplorer/3D/Triangle.cs @@ -24,7 +24,7 @@ namespace LibEveryFileExplorer._3D get { Vector3 a = (PointB - PointA).Cross(PointC - PointA); - return a / (float)Math.Sqrt(a.X * a.X + a.Y * a.Y + a.Z * a.Z); + return a / (float)System.Math.Sqrt(a.X * a.X + a.Y * a.Y + a.Z * a.Z); } } } diff --git a/LibEveryFileExplorer/Collections/Vector2.cs b/LibEveryFileExplorer/Collections/Vector2.cs index 1f483f6..f2a8f63 100644 --- a/LibEveryFileExplorer/Collections/Vector2.cs +++ b/LibEveryFileExplorer/Collections/Vector2.cs @@ -45,7 +45,7 @@ namespace LibEveryFileExplorer.Collections public float Length { - get { return (float)Math.Sqrt(X * X + Y * Y); } + get { return (float)System.Math.Sqrt(X * X + Y * Y); } } public void Normalize() diff --git a/LibEveryFileExplorer/Collections/Vector3.cs b/LibEveryFileExplorer/Collections/Vector3.cs index 180693e..1c99e13 100644 --- a/LibEveryFileExplorer/Collections/Vector3.cs +++ b/LibEveryFileExplorer/Collections/Vector3.cs @@ -52,7 +52,7 @@ namespace LibEveryFileExplorer.Collections public float Length { - get { return (float)Math.Sqrt(X * X + Y * Y + Z * Z); } + get { return (float)System.Math.Sqrt(X * X + Y * Y + Z * Z); } } public void Normalize() diff --git a/LibEveryFileExplorer/Collections/Vector4.cs b/LibEveryFileExplorer/Collections/Vector4.cs index 68bc424..87a3486 100644 --- a/LibEveryFileExplorer/Collections/Vector4.cs +++ b/LibEveryFileExplorer/Collections/Vector4.cs @@ -62,7 +62,7 @@ namespace LibEveryFileExplorer.Collections public float Length { - get { return (float)Math.Sqrt(X * X + Y * Y + Z * Z + W * W); } + get { return (float)System.Math.Sqrt(X * X + Y * Y + Z * Z + W * W); } } public void Normalize() diff --git a/LibEveryFileExplorer/LibEveryFileExplorer.csproj b/LibEveryFileExplorer/LibEveryFileExplorer.csproj index 3acd5b0..cc29c3f 100644 --- a/LibEveryFileExplorer/LibEveryFileExplorer.csproj +++ b/LibEveryFileExplorer/LibEveryFileExplorer.csproj @@ -76,6 +76,7 @@ + True diff --git a/LibEveryFileExplorer/Math/MathUtil.cs b/LibEveryFileExplorer/Math/MathUtil.cs new file mode 100644 index 0000000..d8ce550 --- /dev/null +++ b/LibEveryFileExplorer/Math/MathUtil.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace LibEveryFileExplorer.Math +{ + public class MathUtil + { + public static int Clamp(int value, int min, int max) + { + if (value < min) value = min; + if (value > max) value = max; + return (short)value; + } + + public static float RadToDeg(float Radians) + { + return Radians * (180f / (float)System.Math.PI); + } + + public static double RadToDeg(double Radians) + { + return Radians * (180.0 / System.Math.PI); + } + } +} diff --git a/MarioKart/MKDS/NKMD.cs b/MarioKart/MKDS/NKMD.cs index a3e5df7..b3f7782 100644 --- a/MarioKart/MKDS/NKMD.cs +++ b/MarioKart/MKDS/NKMD.cs @@ -11,6 +11,7 @@ using LibEveryFileExplorer.Collections; using Tao.OpenGl; using MarioKart.UI; using LibEveryFileExplorer.GFX; +using LibEveryFileExplorer.Math; namespace MarioKart.MKDS { @@ -433,7 +434,7 @@ namespace MarioKart.MKDS if (Version <= 34) { float yangle = (float)Math.Atan2(Rotation.X, Rotation.Z); - Rotation = new Vector3(0, /*MathHelper.RadiansToDegrees(yangle)*/ yangle * (180f / (float)Math.PI), 0); + Rotation = new Vector3(0, MathUtil.RadToDeg(yangle), 0); } EnemyPositionID = er.ReadInt16(); ItemPositionID = er.ReadInt16(); diff --git a/NDS/SND/ADPCM.cs b/NDS/SND/ADPCM.cs index 7b77d97..c252e71 100644 --- a/NDS/SND/ADPCM.cs +++ b/NDS/SND/ADPCM.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using LibEveryFileExplorer.IO; +using LibEveryFileExplorer.Math; namespace NDS.SND { @@ -57,19 +58,12 @@ namespace NDS.SND StepTable[Index] * ((val >> 2) & 1); int samp = Last + diff * ((((val >> 3) & 1) == 1) ? -1 : 1); - Last = Clamp(samp, short.MinValue, short.MaxValue); - Index = Clamp(Index + IndexTable[val & 7], 0, 88); + Last = (short)MathUtil.Clamp(samp, short.MinValue, short.MaxValue); + Index = (short)MathUtil.Clamp(Index + IndexTable[val & 7], 0, 88); DataOut.Add((short)Last); } } return DataOut.ToArray(); } - - private static short Clamp(int value, int min, int max) - { - if (value < min) value = min; - if (value > max) value = max; - return (short)value; - } } }