2014-04-12 18:33:35 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2020-02-05 20:19:48 +13:00
|
|
|
|
Copyright (c) 2007-2020 ShareX Team
|
2014-04-12 18:33:35 +12:00
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-10-02 02:11:26 +13:00
|
|
|
|
using System.Security.Cryptography;
|
2014-04-12 18:33:35 +12:00
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2014-04-12 18:33:35 +12:00
|
|
|
|
{
|
|
|
|
|
public static class MathHelpers
|
|
|
|
|
{
|
|
|
|
|
public const float RadianPI = 57.29578f; // 180.0 / Math.PI
|
|
|
|
|
public const float DegreePI = 0.01745329f; // Math.PI / 180.0
|
|
|
|
|
public const float TwoPI = 6.28319f; // Math.PI * 2
|
|
|
|
|
|
2014-07-09 10:32:36 +12:00
|
|
|
|
private static readonly object randomLock = new object();
|
|
|
|
|
private static readonly Random random = new Random();
|
2018-10-02 02:11:26 +13:00
|
|
|
|
private static readonly object cryptoRandomLock = new object();
|
|
|
|
|
private static readonly RNGCryptoServiceProvider cryptoRandom = new RNGCryptoServiceProvider();
|
|
|
|
|
private static byte[] rngBuf = new byte[4];
|
|
|
|
|
|
2019-05-24 08:45:11 +12:00
|
|
|
|
public static T Min<T>(T num, T min) where T : IComparable<T>
|
|
|
|
|
{
|
|
|
|
|
if (num.CompareTo(min) > 0) return min;
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T Max<T>(T num, T max) where T : IComparable<T>
|
|
|
|
|
{
|
|
|
|
|
if (num.CompareTo(max) < 0) return max;
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T Clamp<T>(T num, T min, T max) where T : IComparable<T>
|
|
|
|
|
{
|
|
|
|
|
if (num.CompareTo(min) <= 0) return min;
|
|
|
|
|
if (num.CompareTo(max) >= 0) return max;
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsBetween<T>(T num, T min, T max) where T : IComparable<T>
|
|
|
|
|
{
|
|
|
|
|
return num.CompareTo(min) >= 0 && num.CompareTo(max) <= 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T BetweenOrDefault<T>(T num, T min, T max, T defaultValue = default(T)) where T : IComparable<T>
|
|
|
|
|
{
|
|
|
|
|
if (num.CompareTo(min) >= 0 && num.CompareTo(max) <= 0) return num;
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Remap(float value, float from1, float to1, float from2, float to2)
|
|
|
|
|
{
|
|
|
|
|
return ((value - from1) / (to1 - from1) * (to2 - from2)) + from2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsEvenNumber(int num)
|
|
|
|
|
{
|
|
|
|
|
return num % 2 == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsOddNumber(int num)
|
|
|
|
|
{
|
|
|
|
|
return num % 2 != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Lerp(float value1, float value2, float amount)
|
|
|
|
|
{
|
|
|
|
|
return value1 + ((value2 - value1) * amount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector2 Lerp(Vector2 pos1, Vector2 pos2, float amount)
|
|
|
|
|
{
|
|
|
|
|
float x = Lerp(pos1.X, pos2.X, amount);
|
|
|
|
|
float y = Lerp(pos1.Y, pos2.Y, amount);
|
|
|
|
|
return new Vector2(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 02:11:26 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a random number between 0 and <c>max</c> (inclusive).
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This uses <c>System.Random()</c>, which does not provide safe random numbers. This function
|
|
|
|
|
/// should not be used to generate things that should be unique, like random file names.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="max">The upper limit of the number (inclusive).</param>
|
|
|
|
|
/// <returns>A random number.</returns>
|
2014-07-09 10:32:36 +12:00
|
|
|
|
public static int Random(int max)
|
|
|
|
|
{
|
|
|
|
|
lock (randomLock)
|
|
|
|
|
{
|
|
|
|
|
return random.Next(max + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 23:53:47 +12:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a random number between <c>min</c> and <c>max</c> (inclusive).
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This uses <c>System.Random()</c>, which does not provide safe random numbers. This function
|
|
|
|
|
/// should not be used to generate things that should be unique, like random file names.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="min">The lower limit of the number (inclusive).</param>
|
|
|
|
|
/// <param name="max">The upper limit of the number (inclusive).</param>
|
|
|
|
|
/// <returns>A random number.</returns>
|
2014-07-09 10:32:36 +12:00
|
|
|
|
public static int Random(int min, int max)
|
|
|
|
|
{
|
|
|
|
|
lock (randomLock)
|
|
|
|
|
{
|
|
|
|
|
return random.Next(min, max + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-26 02:40:54 +12:00
|
|
|
|
public static int RandomAdd(int num, int min, int max)
|
2018-12-25 05:57:32 +13:00
|
|
|
|
{
|
2019-05-26 02:40:54 +12:00
|
|
|
|
return num + Random(min, max);
|
2018-12-25 05:57:32 +13:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 01:28:12 +12:00
|
|
|
|
public static T RandomPick<T>(params T[] array)
|
2019-05-24 23:53:47 +12:00
|
|
|
|
{
|
2019-09-02 01:28:12 +12:00
|
|
|
|
return array[Random(array.Length - 1)];
|
2019-05-24 23:53:47 +12:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 02:11:26 +13:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a random number between 0 and <c>max</c> (inclusive) generated with a cryptographic PRNG.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="max">The upper limit of the number (inclusive).</param>
|
|
|
|
|
/// <returns>A cryptographically random number.</returns>
|
|
|
|
|
public static int CryptoRandom(int max)
|
|
|
|
|
{
|
|
|
|
|
return CryptoRandom(0, max);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a random number between <c>min</c> and <c>max</c> (inclusive) generated with a cryptographic PRNG.
|
|
|
|
|
/// </summary>
|
2019-05-24 23:53:47 +12:00
|
|
|
|
/// <param name="min">The lower limit of the number (inclusive).</param>
|
2018-10-02 02:11:26 +13:00
|
|
|
|
/// <param name="max">The upper limit of the number (inclusive).</param>
|
|
|
|
|
/// <returns>A cryptographically random number.</returns>
|
|
|
|
|
public static int CryptoRandom(int min, int max)
|
|
|
|
|
{
|
|
|
|
|
// this code avoids bias in random number generation, which is important when generating random filenames, etc.
|
|
|
|
|
// adapted from https://web.archive.org/web/20150114085328/http://msdn.microsoft.com:80/en-us/magazine/cc163367.aspx
|
|
|
|
|
if (min > max)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException("min");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (min == max)
|
|
|
|
|
{
|
|
|
|
|
return min;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock (cryptoRandomLock)
|
|
|
|
|
{
|
2018-10-18 01:32:03 +13:00
|
|
|
|
long diff = (long)max - min;
|
|
|
|
|
long ceiling = 1 + (long)uint.MaxValue;
|
2018-10-02 02:11:26 +13:00
|
|
|
|
long remainder = ceiling % diff;
|
|
|
|
|
// this should only iterate once unless we generate really large numbers
|
|
|
|
|
uint r;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
cryptoRandom.GetBytes(rngBuf);
|
|
|
|
|
r = BitConverter.ToUInt32(rngBuf, 0);
|
|
|
|
|
} while (r >= ceiling - remainder);
|
|
|
|
|
|
|
|
|
|
return (int)(min + (r % diff));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 01:28:12 +12:00
|
|
|
|
public static T CryptoRandomPick<T>(params T[] array)
|
|
|
|
|
{
|
|
|
|
|
return array[CryptoRandom(array.Length - 1)];
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-12 18:33:35 +12:00
|
|
|
|
public static float RadianToDegree(float radian)
|
|
|
|
|
{
|
|
|
|
|
return radian * RadianPI;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float DegreeToRadian(float degree)
|
|
|
|
|
{
|
|
|
|
|
return degree * DegreePI;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector2 RadianToVector2(float radian)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2((float)Math.Cos(radian), (float)Math.Sin(radian));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector2 RadianToVector2(float radian, float length)
|
|
|
|
|
{
|
|
|
|
|
return RadianToVector2(radian) * length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector2 DegreeToVector2(float degree)
|
|
|
|
|
{
|
|
|
|
|
return RadianToVector2(DegreeToRadian(degree));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector2 DegreeToVector2(float degree, float length)
|
|
|
|
|
{
|
|
|
|
|
return RadianToVector2(DegreeToRadian(degree), length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Vector2ToRadian(Vector2 direction)
|
|
|
|
|
{
|
|
|
|
|
return (float)Math.Atan2(direction.Y, direction.X);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Vector2ToDegree(Vector2 direction)
|
|
|
|
|
{
|
|
|
|
|
return RadianToDegree(Vector2ToRadian(direction));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float LookAtRadian(Vector2 pos1, Vector2 pos2)
|
|
|
|
|
{
|
|
|
|
|
return (float)Math.Atan2(pos2.Y - pos1.Y, pos2.X - pos1.X);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Vector2 LookAtVector2(Vector2 pos1, Vector2 pos2)
|
|
|
|
|
{
|
|
|
|
|
return RadianToVector2(LookAtRadian(pos1, pos2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float LookAtDegree(Vector2 pos1, Vector2 pos2)
|
|
|
|
|
{
|
|
|
|
|
return RadianToDegree(LookAtRadian(pos1, pos2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Distance(Vector2 pos1, Vector2 pos2)
|
|
|
|
|
{
|
|
|
|
|
return (float)Math.Sqrt(Math.Pow(pos2.X - pos1.X, 2) + Math.Pow(pos2.Y - pos1.Y, 2));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|