Files

1143 lines
27 KiB
C#
Raw Permalink Normal View History

2022-11-07 13:09:55 +02:00
using System;
using System.IO;
using System.Runtime.CompilerServices;
/// <summary>
/// Represents a Q31.32 fixed-point number.
/// </summary>
[System.Serializable]
2023-07-02 23:17:37 +03:00
public partial struct DemonFloat : IEquatable<DemonFloat>, IComparable<DemonFloat>
2022-11-07 13:09:55 +02:00
{
readonly long m_rawValue;
2023-07-02 23:17:37 +03:00
public static readonly decimal Precision = (decimal)(new DemonFloat(1L));
public static readonly DemonFloat One = new DemonFloat(ONE);
public static readonly DemonFloat Zero = new DemonFloat();
public static readonly DemonFloat PI = new DemonFloat(Pi);
public static readonly DemonFloat PITimes2 = new DemonFloat(PiTimes2);
public static readonly DemonFloat PIOver180 = new DemonFloat((long)72);
public static readonly DemonFloat Rad2Deg = DemonFloat.Pi * (DemonFloat)2 / (DemonFloat)360;
public static readonly DemonFloat Deg2Rad = (DemonFloat)360 / (DemonFloat.Pi * (DemonFloat)2);
2022-11-07 13:09:55 +02:00
const long Pi = 12868;
const long PiTimes2 = 25736;
public const int FRACTIONAL_PLACES = 12;
const long ONE = 1L << FRACTIONAL_PLACES;
2023-07-02 23:17:37 +03:00
public static int Sign(DemonFloat value)
2022-11-07 13:09:55 +02:00
{
return
value.m_rawValue < 0 ? -1 :
value.m_rawValue > 0 ? 1 :
0;
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Abs(DemonFloat value)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat(value.m_rawValue > 0 ? value.m_rawValue : -value.m_rawValue);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Floor(DemonFloat value)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat((long)((ulong)value.m_rawValue & 0xFFFFFFFFFFFFF000));
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Ceiling(DemonFloat value)
2022-11-07 13:09:55 +02:00
{
var hasFractionalPart = (value.m_rawValue & 0x0000000000000FFF) != 0;
return hasFractionalPart ? Floor(value) + One : value;
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator +(DemonFloat x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat(x.m_rawValue + y.m_rawValue);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator +(DemonFloat x, int y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x + (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator +(int x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return (DemonFloat)x + y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator +(DemonFloat x, float y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x + (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator +(float x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return (DemonFloat)x + y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator +(DemonFloat x, double y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x + (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator +(double x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return (DemonFloat)x + y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator -(DemonFloat x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat(x.m_rawValue - y.m_rawValue);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator -(DemonFloat x, int y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x - (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator -(int x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return (DemonFloat)x - y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator -(DemonFloat x, float y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x - (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator -(float x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return (DemonFloat)x + y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator -(DemonFloat x, double y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x - (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator -(double x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return (DemonFloat)x - y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator *(DemonFloat x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat((x.m_rawValue * y.m_rawValue) >> FRACTIONAL_PLACES);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator *(DemonFloat x, int y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x * (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator *(int x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return (DemonFloat)x * y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator *(DemonFloat x, float y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x * (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator *(float x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return (DemonFloat)x * y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator *(DemonFloat x, double y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x * (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator *(double x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return (DemonFloat)x * y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator /(DemonFloat x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat((x.m_rawValue << FRACTIONAL_PLACES) / y.m_rawValue);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator /(DemonFloat x, int y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x / (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator /(int x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return (DemonFloat)x / y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator /(DemonFloat x, float y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x / (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator /(float x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return (DemonFloat)x / y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator /(double x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return (DemonFloat)x / y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator /(DemonFloat x, double y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x / (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator %(DemonFloat x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat(x.m_rawValue % y.m_rawValue);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator -(DemonFloat x)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat(-x.m_rawValue);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static bool operator ==(DemonFloat x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
return x.m_rawValue == y.m_rawValue;
}
2023-07-02 23:17:37 +03:00
public static bool operator !=(DemonFloat x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
return x.m_rawValue != y.m_rawValue;
}
2023-07-02 23:17:37 +03:00
public static bool operator >(DemonFloat x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
return x.m_rawValue > y.m_rawValue;
}
2023-07-02 23:17:37 +03:00
public static bool operator >(DemonFloat x, int y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x > (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static bool operator <(DemonFloat x, int y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x < (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static bool operator >(DemonFloat x, float y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x > (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static bool operator <(DemonFloat x, float y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return x < (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static bool operator <(DemonFloat x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
return x.m_rawValue < y.m_rawValue;
}
2023-07-02 23:17:37 +03:00
public static bool operator >=(DemonFloat x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
return x.m_rawValue >= y.m_rawValue;
}
2023-07-02 23:17:37 +03:00
public static bool operator <=(DemonFloat x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
return x.m_rawValue <= y.m_rawValue;
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator >>(DemonFloat x, int amount)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat(x.RawValue >> amount);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat operator <<(DemonFloat x, int amount)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat(x.RawValue << amount);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static explicit operator DemonFloat(long value)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat(value * ONE);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static explicit operator long(DemonFloat value)
2022-11-07 13:09:55 +02:00
{
return value.m_rawValue >> FRACTIONAL_PLACES;
}
2023-07-02 23:17:37 +03:00
public static explicit operator DemonFloat(float value)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat((long)(value * ONE));
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static explicit operator float(DemonFloat value)
2022-11-07 13:09:55 +02:00
{
return (float)value.m_rawValue / ONE;
}
2023-07-02 23:17:37 +03:00
public static explicit operator int(DemonFloat value)
2022-11-07 13:09:55 +02:00
{
return (int)((float)value);
}
2023-07-02 23:17:37 +03:00
public static explicit operator DemonFloat(double value)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat((long)(value * ONE));
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static explicit operator double(DemonFloat value)
2022-11-07 13:09:55 +02:00
{
return (double)value.m_rawValue / ONE;
}
2023-07-02 23:17:37 +03:00
public static explicit operator DemonFloat(decimal value)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat((long)(value * ONE));
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static explicit operator decimal(DemonFloat value)
2022-11-07 13:09:55 +02:00
{
return (decimal)value.m_rawValue / ONE;
}
public override bool Equals(object obj)
{
2023-07-02 23:17:37 +03:00
return obj is DemonFloat && ((DemonFloat)obj).m_rawValue == m_rawValue;
2022-11-07 13:09:55 +02:00
}
public override int GetHashCode()
{
return m_rawValue.GetHashCode();
}
2023-07-02 23:17:37 +03:00
public bool Equals(DemonFloat other)
2022-11-07 13:09:55 +02:00
{
return m_rawValue == other.m_rawValue;
}
2023-07-02 23:17:37 +03:00
public int CompareTo(DemonFloat other)
2022-11-07 13:09:55 +02:00
{
return m_rawValue.CompareTo(other.m_rawValue);
}
public override string ToString()
{
return ((decimal)this).ToString();
}
public string ToStringRound(int round = 2)
{
return (float)Math.Round((float)this, round) + "";
}
2023-07-02 23:17:37 +03:00
public static DemonFloat FromRaw(long rawValue)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return new DemonFloat(rawValue);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Pow(DemonFloat x, int y)
2022-11-07 13:09:55 +02:00
{
if (y == 1) return x;
2023-07-02 23:17:37 +03:00
DemonFloat result = DemonFloat.Zero;
DemonFloat tmp = Pow(x, y / 2);
2022-11-07 13:09:55 +02:00
if ((y & 1) != 0) //奇数
{
result = x * tmp * tmp;
}
else
{
result = tmp * tmp;
}
return result;
}
public long RawValue { get { return m_rawValue; } }
2023-07-02 23:17:37 +03:00
DemonFloat(long rawValue)
2022-11-07 13:09:55 +02:00
{
m_rawValue = rawValue;
}
2023-07-02 23:17:37 +03:00
public DemonFloat(int value)
2022-11-07 13:09:55 +02:00
{
m_rawValue = value * ONE;
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Sqrt(DemonFloat f, int numberIterations)
2022-11-07 13:09:55 +02:00
{
if (f.RawValue < 0)
{
throw new ArithmeticException("sqrt error");
}
if (f.RawValue == 0)
2023-07-02 23:17:37 +03:00
return DemonFloat.Zero;
2022-11-07 13:09:55 +02:00
2023-07-02 23:17:37 +03:00
DemonFloat k = f + DemonFloat.One >> 1;
2022-11-07 13:09:55 +02:00
for (int i = 0; i < numberIterations; i++)
k = (k + (f / k)) >> 1;
if (k.RawValue < 0)
throw new ArithmeticException("Overflow");
else
return k;
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Sqrt(DemonFloat f)
2022-11-07 13:09:55 +02:00
{
byte numberOfIterations = 8;
if (f.RawValue > 0x64000)
numberOfIterations = 12;
if (f.RawValue > 0x3e8000)
numberOfIterations = 16;
return Sqrt(f, numberOfIterations);
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Lerp(DemonFloat from, DemonFloat to, DemonFloat factor)
2022-11-12 16:14:22 +02:00
{
return from * (1 - factor) + to * factor;
}
2022-11-07 13:09:55 +02:00
#region Sin
2023-07-02 23:17:37 +03:00
public static DemonFloat Sin(DemonFloat i)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat j = (DemonFloat)0;
for (; i < DemonFloat.Zero; i += DemonFloat.FromRaw(PiTimes2)) ;
if (i > DemonFloat.FromRaw(PiTimes2))
i %= DemonFloat.FromRaw(PiTimes2);
2022-11-07 13:09:55 +02:00
2023-07-02 23:17:37 +03:00
DemonFloat k = (i * DemonFloat.FromRaw(100000000)) / DemonFloat.FromRaw(7145244444);
if (i != DemonFloat.Zero && i != DemonFloat.FromRaw(6434) && i != DemonFloat.FromRaw(Pi) &&
i != DemonFloat.FromRaw(19302) && i != DemonFloat.FromRaw(PiTimes2))
j = (i * DemonFloat.FromRaw(100000000)) / DemonFloat.FromRaw(7145244444) - k * DemonFloat.FromRaw(10);
if (k <= DemonFloat.FromRaw(90))
2022-11-07 13:09:55 +02:00
return sin_lookup(k, j);
2023-07-02 23:17:37 +03:00
if (k <= DemonFloat.FromRaw(180))
return sin_lookup(DemonFloat.FromRaw(180) - k, j);
if (k <= DemonFloat.FromRaw(270))
return -sin_lookup(k - DemonFloat.FromRaw(180), j);
2022-11-07 13:09:55 +02:00
else
2023-07-02 23:17:37 +03:00
return -sin_lookup(DemonFloat.FromRaw(360) - k, j);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
private static DemonFloat sin_lookup(DemonFloat i, DemonFloat j)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
if (j > 0 && j < DemonFloat.FromRaw(10) && i < DemonFloat.FromRaw(90))
return DemonFloat.FromRaw(SIN_TABLE[i.RawValue]) +
((DemonFloat.FromRaw(SIN_TABLE[i.RawValue + 1]) - DemonFloat.FromRaw(SIN_TABLE[i.RawValue])) /
DemonFloat.FromRaw(10)) * j;
2022-11-07 13:09:55 +02:00
else
2023-07-02 23:17:37 +03:00
return DemonFloat.FromRaw(SIN_TABLE[i.RawValue]);
2022-11-07 13:09:55 +02:00
}
private static int[] SIN_TABLE = {
0, 71, 142, 214, 285, 357, 428, 499, 570, 641,
711, 781, 851, 921, 990, 1060, 1128, 1197, 1265, 1333,
1400, 1468, 1534, 1600, 1665, 1730, 1795, 1859, 1922, 1985,
2048, 2109, 2170, 2230, 2290, 2349, 2407, 2464, 2521, 2577,
2632, 2686, 2740, 2793, 2845, 2896, 2946, 2995, 3043, 3091,
3137, 3183, 3227, 3271, 3313, 3355, 3395, 3434, 3473, 3510,
3547, 3582, 3616, 3649, 3681, 3712, 3741, 3770, 3797, 3823,
3849, 3872, 3895, 3917, 3937, 3956, 3974, 3991, 4006, 4020,
4033, 4045, 4056, 4065, 4073, 4080, 4086, 4090, 4093, 4095,
4096
};
#endregion
#region Cos, Tan, Asin
2023-07-02 23:17:37 +03:00
public static DemonFloat Cos(DemonFloat i)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return Sin(i + DemonFloat.FromRaw(6435));
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Tan(DemonFloat i)
2022-11-07 13:09:55 +02:00
{
return Sin(i) / Cos(i);
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Asin(DemonFloat F)
2022-11-07 13:09:55 +02:00
{
bool isNegative = F < 0;
F = Abs(F);
2023-07-02 23:17:37 +03:00
if (F > DemonFloat.One)
2022-11-07 13:09:55 +02:00
throw new ArithmeticException("Bad Asin Input:" + (double)F);
2023-07-02 23:17:37 +03:00
DemonFloat f1 = ((((DemonFloat.FromRaw(145103 >> FRACTIONAL_PLACES) * F) -
DemonFloat.FromRaw(599880 >> FRACTIONAL_PLACES) * F) +
DemonFloat.FromRaw(1420468 >> FRACTIONAL_PLACES) * F) -
DemonFloat.FromRaw(3592413 >> FRACTIONAL_PLACES) * F) +
DemonFloat.FromRaw(26353447 >> FRACTIONAL_PLACES);
DemonFloat f2 = PI / (DemonFloat)2 - (Sqrt(DemonFloat.One - F) * f1);
2022-11-07 13:09:55 +02:00
return isNegative ? -f2 : f2;
}
#endregion
#region ATan, ATan2
2023-07-02 23:17:37 +03:00
public static DemonFloat Atan(DemonFloat F)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return Asin(F / Sqrt(DemonFloat.One + (F * F)));
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Atan2(DemonFloat F1, DemonFloat F2)
2022-11-07 13:09:55 +02:00
{
if (F2.RawValue == 0 && F1.RawValue == 0)
2023-07-02 23:17:37 +03:00
return (DemonFloat)0;
2022-11-07 13:09:55 +02:00
2023-07-02 23:17:37 +03:00
DemonFloat result = (DemonFloat)0;
2022-11-07 13:09:55 +02:00
if (F2 > 0)
result = Atan(F1 / F2);
else if (F2 < 0)
{
2023-07-02 23:17:37 +03:00
if (F1 >= (DemonFloat)0)
2022-11-07 13:09:55 +02:00
result = (PI - Atan(Abs(F1 / F2)));
else
result = -(PI - Atan(Abs(F1 / F2)));
}
else
2023-07-02 23:17:37 +03:00
result = (F1 >= (DemonFloat)0 ? PI : -PI) / (DemonFloat)2;
2022-11-07 13:09:55 +02:00
return result;
}
#endregion
}
2023-07-02 23:17:37 +03:00
public struct DemonVector3
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
public DemonFloat x;
public DemonFloat y;
public DemonFloat z;
2022-11-07 13:09:55 +02:00
2023-07-02 23:17:37 +03:00
public DemonVector3(DemonFloat x, DemonFloat y, DemonFloat z)
2022-11-07 13:09:55 +02:00
{
this.x = x;
this.y = y;
this.z = z;
}
2023-07-02 23:17:37 +03:00
public DemonVector3(DemonVector3 v)
2022-11-07 13:09:55 +02:00
{
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
2023-07-02 23:17:37 +03:00
public DemonFloat this[int index]
2022-11-07 13:09:55 +02:00
{
get
{
if (index == 0)
return x;
else if (index == 1)
return y;
else
return z;
}
set
{
if (index == 0)
x = value;
else if (index == 1)
y = value;
else
y = value;
}
}
2023-07-02 23:17:37 +03:00
public static DemonVector3 Zero
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
get { return new DemonVector3(DemonFloat.Zero, DemonFloat.Zero, DemonFloat.Zero); }
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonVector3 operator +(DemonVector3 a, DemonVector3 b)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat x = a.x + b.x;
DemonFloat y = a.y + b.y;
DemonFloat z = a.z + b.z;
return new DemonVector3(x, y, z);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonVector3 operator -(DemonVector3 a, DemonVector3 b)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat x = a.x - b.x;
DemonFloat y = a.y - b.y;
DemonFloat z = a.z - b.z;
return new DemonVector3(x, y, z);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonVector3 operator *(DemonFloat d, DemonVector3 a)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat x = a.x * d;
DemonFloat y = a.y * d;
DemonFloat z = a.z * d;
return new DemonVector3(x, y, z);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonVector3 operator *(DemonVector3 a, DemonFloat d)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat x = a.x * d;
DemonFloat y = a.y * d;
DemonFloat z = a.z * d;
return new DemonVector3(x, y, z);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonVector3 operator /(DemonVector3 a, DemonFloat d)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat x = a.x / d;
DemonFloat y = a.y / d;
DemonFloat z = a.z / d;
return new DemonVector3(x, y, z);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static bool operator ==(DemonVector3 lhs, DemonVector3 rhs)
2022-11-07 13:09:55 +02:00
{
return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
}
2023-07-02 23:17:37 +03:00
public static bool operator !=(DemonVector3 lhs, DemonVector3 rhs)
2022-11-07 13:09:55 +02:00
{
return lhs.x != rhs.x || lhs.y != rhs.y || lhs.z != rhs.z;
}
2023-07-02 23:17:37 +03:00
public static DemonFloat SqrMagnitude(DemonVector3 a)
2022-11-07 13:09:55 +02:00
{
return a.x * a.x + a.y * a.y + a.z * a.z;
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Distance(DemonVector3 a, DemonVector3 b)
2022-11-07 13:09:55 +02:00
{
return Magnitude(a - b);
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Magnitude(DemonVector3 a)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return DemonFloat.Sqrt(DemonVector3.SqrMagnitude(a));
2022-11-07 13:09:55 +02:00
}
public void Normalize()
{
2023-07-02 23:17:37 +03:00
DemonFloat n = x * x + y * y + z * z;
if (n == DemonFloat.Zero)
2022-11-07 13:09:55 +02:00
return;
2023-07-02 23:17:37 +03:00
n = DemonFloat.Sqrt(n);
2022-11-07 13:09:55 +02:00
2023-07-02 23:17:37 +03:00
if (n < (DemonFloat)0.0001)
2022-11-07 13:09:55 +02:00
{
return;
}
n = 1 / n;
x *= n;
y *= n;
z *= n;
}
2023-07-02 23:17:37 +03:00
public DemonVector3 GetNormalized()
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonVector3 v = new DemonVector3(this);
2022-11-07 13:09:55 +02:00
v.Normalize();
return v;
}
public override string ToString()
{
return string.Format("x:{0} y:{1} z:{2}", x, y, z);
}
public override bool Equals(object obj)
{
2023-07-02 23:17:37 +03:00
return obj is DemonVector2 && ((DemonVector3)obj) == this;
2022-11-07 13:09:55 +02:00
}
public override int GetHashCode()
{
return this.x.GetHashCode() + this.y.GetHashCode() + this.z.GetHashCode();
}
2023-07-02 23:17:37 +03:00
public static DemonVector3 Lerp(DemonVector3 from, DemonVector3 to, DemonFloat factor)
2022-11-07 13:09:55 +02:00
{
return from * (1 - factor) + to * factor;
}
#if _CLIENTLOGIC_
public UnityEngine.Vector3 ToVector3()
{
return new UnityEngine.Vector3((float)x, (float)y, (float)z);
}
#endif
}
2023-07-02 23:17:37 +03:00
public struct DemonVector2
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
public DemonFloat x;
public DemonFloat y;
2022-11-07 13:09:55 +02:00
2023-07-02 23:17:37 +03:00
public DemonVector2(DemonFloat x, DemonFloat y)
2022-11-07 13:09:55 +02:00
{
this.x = x;
this.y = y;
}
2023-07-02 23:17:37 +03:00
public DemonVector2(DemonFloat x, int y)
2022-11-07 13:09:55 +02:00
{
this.x = x;
2023-07-02 23:17:37 +03:00
this.y = (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public DemonVector2(int x, int y)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
this.x = (DemonFloat)x;
this.y = (DemonFloat)y;
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public DemonVector2(DemonVector2 v)
2022-11-07 13:09:55 +02:00
{
this.x = v.x;
this.y = v.y;
}
2023-07-02 23:17:37 +03:00
public static DemonVector2 operator -(DemonVector2 a, int b)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat x = a.x - b;
DemonFloat y = a.y - b;
return new DemonVector2(x, y);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public DemonFloat this[int index]
2022-11-07 13:09:55 +02:00
{
get { return index == 0 ? x : y; }
set
{
if (index == 0)
{
x = value;
}
else
{
y = value;
}
}
}
2023-07-02 23:17:37 +03:00
public static DemonVector2 Zero
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
get { return new DemonVector2(DemonFloat.Zero, DemonFloat.Zero); }
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonVector2 operator +(DemonVector2 a, DemonVector2 b)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat x = a.x + b.x;
DemonFloat y = a.y + b.y;
return new DemonVector2(x, y);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonVector2 operator -(DemonVector2 a, DemonVector2 b)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat x = a.x - b.x;
DemonFloat y = a.y - b.y;
return new DemonVector2(x, y);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonVector2 operator *(DemonFloat d, DemonVector2 a)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat x = a.x * d;
DemonFloat y = a.y * d;
return new DemonVector2(x, y);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonVector2 operator *(DemonVector2 a, DemonFloat d)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat x = a.x * d;
DemonFloat y = a.y * d;
return new DemonVector2(x, y);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static DemonVector2 operator /(DemonVector2 a, DemonFloat d)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat x = a.x / d;
DemonFloat y = a.y / d;
return new DemonVector2(x, y);
2022-11-07 13:09:55 +02:00
}
2023-07-02 23:17:37 +03:00
public static bool operator ==(DemonVector2 lhs, DemonVector2 rhs)
2022-11-07 13:09:55 +02:00
{
return lhs.x == rhs.x && lhs.y == rhs.y;
}
2023-07-02 23:17:37 +03:00
public static bool operator !=(DemonVector2 lhs, DemonVector2 rhs)
2022-11-07 13:09:55 +02:00
{
return lhs.x != rhs.x || lhs.y != rhs.y;
}
public override bool Equals(object obj)
{
2023-07-02 23:17:37 +03:00
return obj is DemonVector2 && ((DemonVector2)obj) == this;
2022-11-07 13:09:55 +02:00
}
public override int GetHashCode()
{
return this.x.GetHashCode() + this.y.GetHashCode();
}
2023-07-02 23:17:37 +03:00
public static DemonFloat SqrMagnitude(DemonVector2 a)
2022-11-07 13:09:55 +02:00
{
return a.x * a.x + a.y * a.y;
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Distance(DemonVector2 a, DemonVector2 b)
2022-11-07 13:09:55 +02:00
{
return Magnitude(a - b);
}
2023-07-02 23:17:37 +03:00
public static DemonFloat Magnitude(DemonVector2 a)
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
return DemonFloat.Sqrt(DemonVector2.SqrMagnitude(a));
2022-11-07 13:09:55 +02:00
}
public void Normalize()
{
2023-07-02 23:17:37 +03:00
DemonFloat n = x * x + y * y;
if (n == DemonFloat.Zero)
2022-11-07 13:09:55 +02:00
return;
2023-07-02 23:17:37 +03:00
n = DemonFloat.Sqrt(n);
2022-11-07 13:09:55 +02:00
2023-07-02 23:17:37 +03:00
if (n < (DemonFloat)0.0001)
2022-11-07 13:09:55 +02:00
{
return;
}
n = 1 / n;
x *= n;
y *= n;
}
2023-07-02 23:17:37 +03:00
public DemonVector2 GetNormalized()
2022-11-07 13:09:55 +02:00
{
2023-07-02 23:17:37 +03:00
DemonVector2 v = new DemonVector2(this);
2022-11-07 13:09:55 +02:00
v.Normalize();
return v;
}
public override string ToString()
{
return string.Format("x:{0} y:{1}", x, y);
}
#if _CLIENTLOGIC_
public UnityEngine.Vector2 ToVector2()
{
return new UnityEngine.Vector2((float)x, (float)y);
}
#endif
}
public struct NormalVector2
{
public float x;
public float y;
public NormalVector2(float x, float y)
{
this.x = x;
this.y = y;
}
public NormalVector2(int x, int y)
{
this.x = x;
this.y = y;
}
public NormalVector2(NormalVector2 v)
{
this.x = v.x;
this.y = v.y;
}
public static NormalVector2 operator -(NormalVector2 a, int b)
{
float x = a.x - b;
float y = a.y - b;
return new NormalVector2(x, y);
}
public float this[int index]
{
get { return index == 0 ? x : y; }
set
{
if (index == 0)
{
x = value;
}
else
{
y = value;
}
}
}
public static NormalVector2 Zero
{
get { return new NormalVector2(0, 0); }
}
public static NormalVector2 operator +(NormalVector2 a, NormalVector2 b)
{
float x = a.x + b.x;
float y = a.y + b.y;
return new NormalVector2(x, y);
}
public static NormalVector2 operator -(NormalVector2 a, NormalVector2 b)
{
float x = a.x - b.x;
float y = a.y - b.y;
return new NormalVector2(x, y);
}
public static NormalVector2 operator *(float d, NormalVector2 a)
{
float x = a.x * d;
float y = a.y * d;
return new NormalVector2(x, y);
}
public static NormalVector2 operator *(NormalVector2 a, float d)
{
float x = a.x * d;
float y = a.y * d;
return new NormalVector2(x, y);
}
public static NormalVector2 operator /(NormalVector2 a, float d)
{
float x = a.x / d;
float y = a.y / d;
return new NormalVector2(x, y);
}
public static bool operator ==(NormalVector2 lhs, NormalVector2 rhs)
{
return lhs.x == rhs.x && lhs.y == rhs.y;
}
public static bool operator !=(NormalVector2 lhs, NormalVector2 rhs)
{
return lhs.x != rhs.x || lhs.y != rhs.y;
}
public override bool Equals(object obj)
{
return obj is NormalVector2 && ((NormalVector2)obj) == this;
}
public override int GetHashCode()
{
return this.x.GetHashCode() + this.y.GetHashCode();
}
public static float SqrMagnitude(NormalVector2 a)
{
return a.x * a.x + a.y * a.y;
}
public static float Distance(NormalVector2 a, NormalVector2 b)
{
return Magnitude(a - b);
}
public static float Magnitude(NormalVector2 a)
{
return NormalVector2.SqrMagnitude(a);
}
public void Normalize()
{
float n = x * x + y * y;
if (n == 0)
return;
//n = float.Sqrt(n);
if (n < (float)0.0001)
{
return;
}
n = 1 / n;
x *= n;
y *= n;
}
public NormalVector2 GetNormalized()
{
NormalVector2 v = new NormalVector2(this);
v.Normalize();
return v;
}
public override string ToString()
{
return string.Format("x:{0} y:{1}", x, y);
}
#if _CLIENTLOGIC_
public UnityEngine.Vector2 ToVector2()
{
return new UnityEngine.Vector2((float)x, (float)y);
}
#endif
}
public struct IntVector2
{
public int x;
public int y;
public IntVector2(int x, int y)
{
this.x = x;
this.y = y;
}
public IntVector2(IntVector2 v)
{
this.x = v.x;
this.y = v.y;
}
public static IntVector2 operator -(IntVector2 a, int b)
{
int x = a.x - b;
int y = a.y - b;
return new IntVector2(x, y);
}
public int this[int index]
{
get { return index == 0 ? x : y; }
set
{
if (index == 0)
{
x = value;
}
else
{
y = value;
}
}
}
public static IntVector2 Zero
{
get { return new IntVector2(0, 0); }
}
public static IntVector2 operator +(IntVector2 a, IntVector2 b)
{
int x = a.x + b.x;
int y = a.y + b.y;
return new IntVector2(x, y);
}
public static IntVector2 operator -(IntVector2 a, IntVector2 b)
{
int x = a.x - b.x;
int y = a.y - b.y;
return new IntVector2(x, y);
}
public static IntVector2 operator *(int d, IntVector2 a)
{
int x = a.x * d;
int y = a.y * d;
return new IntVector2(x, y);
}
public static IntVector2 operator *(IntVector2 a, int d)
{
int x = a.x * d;
int y = a.y * d;
return new IntVector2(x, y);
}
public static IntVector2 operator /(IntVector2 a, int d)
{
int x = a.x / d;
int y = a.y / d;
return new IntVector2(x, y);
}
public static bool operator ==(IntVector2 lhs, IntVector2 rhs)
{
return lhs.x == rhs.x && lhs.y == rhs.y;
}
public static bool operator !=(IntVector2 lhs, IntVector2 rhs)
{
return lhs.x != rhs.x || lhs.y != rhs.y;
}
public override bool Equals(object obj)
{
return obj is IntVector2 && ((IntVector2)obj) == this;
}
public override int GetHashCode()
{
return this.x.GetHashCode() + this.y.GetHashCode();
}
public static int SqrMagnitude(IntVector2 a)
{
return a.x * a.x + a.y * a.y;
}
public static int Distance(IntVector2 a, IntVector2 b)
{
return Magnitude(a - b);
}
public static int Magnitude(IntVector2 a)
{
return IntVector2.SqrMagnitude(a);
}
public void Normalize()
{
int n = x * x + y * y;
if (n == 0)
return;
//n = int.Sqrt(n);
if (n < (int)0.0001)
{
return;
}
n = 1 / n;
x *= n;
y *= n;
}
public IntVector2 GetNormalized()
{
IntVector2 v = new IntVector2(this);
v.Normalize();
return v;
}
public override string ToString()
{
return string.Format("x:{0} y:{1}", x, y);
}
#if _CLIENTLOGIC_
public UnityEngine.Vector2 ToVector2()
{
return new UnityEngine.Vector2((int)x, (int)y);
}
#endif
}