| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139 |
- using System;
- using System.IO;
- public partial struct Fix64 : IEquatable<Fix64>, IComparable<Fix64> {
- readonly long m_rawValue;
- public static readonly decimal Precision = (decimal)(new Fix64(1L));
- public static readonly Fix64 One = new Fix64(ONE);
- public static readonly Fix64 Zero = new Fix64();
- public static readonly Fix64 PI = new Fix64(Pi);
- public static readonly Fix64 PITimes2 = new Fix64(PiTimes2);
- public static readonly Fix64 PIOver180 = new Fix64((long)72);
- public static readonly Fix64 Rad2Deg = Fix64.Pi * (Fix64)2 / (Fix64)360;
- public static readonly Fix64 Deg2Rad = (Fix64)360 / (Fix64.Pi * (Fix64)2);
- const long Pi = 12868;
- const long PiTimes2 = 25736;
- public const int FRACTIONAL_PLACES = 12;
- const long ONE = 1L << FRACTIONAL_PLACES;
- public static int Sign(Fix64 value) {
- return
- value.m_rawValue < 0 ? -1 :
- value.m_rawValue > 0 ? 1 :
- 0;
- }
- public static Fix64 Abs(Fix64 value) {
- return new Fix64(value.m_rawValue > 0 ? value.m_rawValue : -value.m_rawValue);
- }
- public static Fix64 Floor(Fix64 value) {
- return new Fix64((long)((ulong)value.m_rawValue & 0xFFFFFFFFFFFFF000));
- }
- public static Fix64 Ceiling(Fix64 value) {
- var hasFractionalPart = (value.m_rawValue & 0x0000000000000FFF) != 0;
- return hasFractionalPart ? Floor(value) + One : value;
- }
- public static Fix64 Max(Fix64 value1, Fix64 value2)
- {
- Fix64 clampvalue = value1 > value2 ? value1 : value2;
- return clampvalue;
- }
- public static Fix64 Clamp(Fix64 value,Fix64 Min,Fix64 Max)
- {
- Fix64 clampvalue = value < Min ? Min : value;
- clampvalue = value > Max ? Max : value;
- return clampvalue;
- }
- public static Fix64 operator +(Fix64 x, Fix64 y) {
- return new Fix64(x.m_rawValue + y.m_rawValue);
- }
- public static Fix64 operator +(Fix64 x, int y)
- {
- return x + (Fix64)y;
- }
- public static Fix64 operator +(int x, Fix64 y)
- {
- return (Fix64)x + y;
- }
- public static Fix64 operator +(Fix64 x, float y)
- {
- return x + (Fix64)y;
- }
- public static Fix64 operator +(float x, Fix64 y)
- {
- return (Fix64)x + y;
- }
- public static Fix64 operator +(Fix64 x, double y)
- {
- return x + (Fix64)y;
- }
- public static Fix64 operator +(double x, Fix64 y)
- {
- return (Fix64)x + y;
- }
- public static Fix64 operator -(Fix64 x, Fix64 y) {
- return new Fix64(x.m_rawValue - y.m_rawValue);
- }
- public static Fix64 operator -(Fix64 x, int y)
- {
- return x - (Fix64)y;
- }
- public static Fix64 operator -(int x, Fix64 y)
- {
- return (Fix64)x - y;
- }
- public static Fix64 operator -(Fix64 x, float y)
- {
- return x - (Fix64)y;
- }
- public static Fix64 operator -(float x, Fix64 y)
- {
- return (Fix64)x + y;
- }
- public static Fix64 operator -(Fix64 x, double y)
- {
- return x - (Fix64)y;
- }
- public static Fix64 operator -(double x, Fix64 y)
- {
- return (Fix64)x - y;
- }
- public static Fix64 operator *(Fix64 x, Fix64 y) {
- return new Fix64((x.m_rawValue * y.m_rawValue) >> FRACTIONAL_PLACES);
- }
- public static Fix64 operator *(Fix64 x, int y)
- {
- return x * (Fix64)y;
- }
- public static Fix64 operator *(int x, Fix64 y)
- {
- return (Fix64)x * y;
- }
- public static Fix64 operator *(Fix64 x, float y)
- {
- return x * (Fix64)y;
- }
- public static Fix64 operator *(float x, Fix64 y)
- {
- return (Fix64)x * y;
- }
- public static Fix64 operator *(Fix64 x, double y)
- {
- return x * (Fix64)y;
- }
- public static Fix64 operator *(double x, Fix64 y)
- {
- return (Fix64)x * y;
- }
- public static Fix64 operator /(Fix64 x, Fix64 y) {
- return new Fix64((x.m_rawValue << FRACTIONAL_PLACES) / y.m_rawValue);
- }
- public static Fix64 operator /(Fix64 x, int y)
- {
- return x / (Fix64)y;
- }
- public static Fix64 operator /(int x, Fix64 y)
- {
- return (Fix64)x / y;
- }
- public static Fix64 operator /(Fix64 x, float y)
- {
- return x / (Fix64)y;
- }
- public static Fix64 operator /(float x, Fix64 y)
- {
- return (Fix64)x / y;
- }
- public static Fix64 operator /(double x, Fix64 y)
- {
- return (Fix64)x / y;
- }
- public static Fix64 operator /(Fix64 x, double y)
- {
- return x / (Fix64)y;
- }
- public static Fix64 operator %(Fix64 x, Fix64 y) {
- return new Fix64(x.m_rawValue % y.m_rawValue);
- }
- public static Fix64 operator -(Fix64 x) {
- return new Fix64(-x.m_rawValue);
- }
- public static bool operator ==(Fix64 x, Fix64 y) {
- return x.m_rawValue == y.m_rawValue;
- }
- public static bool operator !=(Fix64 x, Fix64 y) {
- return x.m_rawValue != y.m_rawValue;
- }
- public static bool operator >(Fix64 x, Fix64 y) {
- return x.m_rawValue > y.m_rawValue;
- }
- public static bool operator >(Fix64 x, int y)
- {
- return x > (Fix64)y;
- }
- public static bool operator <(Fix64 x, int y)
- {
- return x < (Fix64)y;
- }
- public static bool operator >(Fix64 x, float y)
- {
- return x > (Fix64)y;
- }
- public static bool operator <(Fix64 x, float y)
- {
- return x < (Fix64)y;
- }
- public static bool operator <(Fix64 x, Fix64 y) {
- return x.m_rawValue < y.m_rawValue;
- }
- public static bool operator >=(Fix64 x, Fix64 y) {
- return x.m_rawValue >= y.m_rawValue;
- }
- public static bool operator <=(Fix64 x, Fix64 y) {
- return x.m_rawValue <= y.m_rawValue;
- }
- public static Fix64 operator >> (Fix64 x, int amount)
- {
- return new Fix64(x.RawValue >> amount);
- }
- public static Fix64 operator << (Fix64 x, int amount)
- {
- return new Fix64(x.RawValue << amount);
- }
- public static explicit operator Fix64(long value) {
- return new Fix64(value * ONE);
- }
- public static explicit operator long(Fix64 value) {
- return value.m_rawValue >> FRACTIONAL_PLACES;
- }
- public static explicit operator Fix64(float value) {
- return new Fix64((long)(value * ONE));
- }
- public static explicit operator float(Fix64 value) {
- return (float)value.m_rawValue / ONE;
- }
- public static explicit operator int(Fix64 value)
- {
- return (int)((float)value);
- }
- public static explicit operator Fix64(double value) {
- return new Fix64((long)(value * ONE));
- }
- public static explicit operator double(Fix64 value) {
- return (double)value.m_rawValue / ONE;
- }
- public static explicit operator Fix64(decimal value) {
- return new Fix64((long)(value * ONE));
- }
- public static explicit operator decimal(Fix64 value) {
- return (decimal)value.m_rawValue / ONE;
- }
- public override bool Equals(object obj)
- {
- return obj is Fix64 && ((Fix64)obj).m_rawValue == m_rawValue;
- }
- public override int GetHashCode() {
- return m_rawValue.GetHashCode();
- }
- public bool Equals(Fix64 other) {
- return m_rawValue == other.m_rawValue;
- }
- public int CompareTo(Fix64 other) {
- 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) + "";
- }
- public static Fix64 FromRaw(long rawValue) {
- return new Fix64(rawValue);
- }
- public static Fix64 Pow(Fix64 x, int y)
- {
- if (y == 1) return x;
- Fix64 result = Fix64.Zero;
- Fix64 tmp = Pow(x, y / 2);
- if ((y & 1) != 0) //奇数
- {
- result = x * tmp * tmp;
- }
- else
- {
- result = tmp * tmp;
- }
- return result;
- }
- public long RawValue { get { return m_rawValue; } }
- Fix64(long rawValue) {
- m_rawValue = rawValue;
- }
- public Fix64(int value) {
- m_rawValue = value * ONE;
- }
- public static Fix64 Sqrt(Fix64 f, int numberIterations)
- {
- if (f.RawValue < 0)
- {
- throw new ArithmeticException("sqrt error");
- }
- if (f.RawValue == 0)
- return Fix64.Zero;
- Fix64 k = f + Fix64.One >> 1;
- for (int i = 0; i < numberIterations; i++)
- k = (k + (f / k)) >> 1;
- if (k.RawValue < 0)
- throw new ArithmeticException("Overflow");
- else
- return k;
- }
- public static Fix64 Lerp(Fix64 from, Fix64 to, Fix64 factor)
- {
- return from * (1 - factor) + to * factor;
- }
- public static Fix64 Sqrt(Fix64 f)
- {
- byte numberOfIterations = 8;
- if (f.RawValue > 0x64000)
- numberOfIterations = 12;
- if (f.RawValue > 0x3e8000)
- numberOfIterations = 16;
- return Sqrt(f, numberOfIterations);
- }
- #region Sin
- public static Fix64 Sin(Fix64 i)
- {
- Fix64 j = (Fix64)0;
- for (; i < Fix64.Zero; i += Fix64.FromRaw(PiTimes2));
- if (i > Fix64.FromRaw(PiTimes2))
- i %= Fix64.FromRaw(PiTimes2);
- Fix64 k = (i * Fix64.FromRaw(100000000)) / Fix64.FromRaw(7145244444);
- if (i != Fix64.Zero && i != Fix64.FromRaw(6434) && i != Fix64.FromRaw(Pi) &&
- i != Fix64.FromRaw(19302) && i != Fix64.FromRaw(PiTimes2))
- j = (i * Fix64.FromRaw(100000000)) / Fix64.FromRaw(7145244444) - k * Fix64.FromRaw(10);
- if (k <= Fix64.FromRaw(90))
- return sin_lookup(k, j);
- if (k <= Fix64.FromRaw(180))
- return sin_lookup(Fix64.FromRaw(180) - k, j);
- if (k <= Fix64.FromRaw(270))
- return -sin_lookup(k - Fix64.FromRaw(180), j);
- else
- return -sin_lookup(Fix64.FromRaw(360) - k, j);
- }
- private static Fix64 sin_lookup(Fix64 i, Fix64 j)
- {
- if (j > 0 && j < Fix64.FromRaw(10) && i < Fix64.FromRaw(90))
- return Fix64.FromRaw(SIN_TABLE[i.RawValue]) +
- ((Fix64.FromRaw(SIN_TABLE[i.RawValue + 1]) - Fix64.FromRaw(SIN_TABLE[i.RawValue])) /
- Fix64.FromRaw(10)) * j;
- else
- return Fix64.FromRaw(SIN_TABLE[i.RawValue]);
- }
- 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
- public static Fix64 Cos(Fix64 i)
- {
- return Sin(i + Fix64.FromRaw(6435));
- }
- public static Fix64 Tan(Fix64 i)
- {
- return Sin(i) / Cos(i);
- }
- public static Fix64 Asin(Fix64 F)
- {
- bool isNegative = F < 0;
- F = Abs(F);
- if (F > Fix64.One)
- throw new ArithmeticException("Bad Asin Input:" + (double)F);
- Fix64 f1 = ((((Fix64.FromRaw(145103 >> FRACTIONAL_PLACES) * F) -
- Fix64.FromRaw(599880 >> FRACTIONAL_PLACES)* F) +
- Fix64.FromRaw(1420468 >> FRACTIONAL_PLACES)* F) -
- Fix64.FromRaw(3592413 >> FRACTIONAL_PLACES)* F) +
- Fix64.FromRaw(26353447 >> FRACTIONAL_PLACES);
- Fix64 f2 = PI / (Fix64)2 - (Sqrt(Fix64.One - F) * f1);
- return isNegative ? -f2 : f2;
- }
- #endregion
- #region ATan, ATan2
- public static Fix64 Atan(Fix64 F)
- {
- return Asin(F / Sqrt(Fix64.One + (F * F)));
- }
- public static Fix64 Atan2(Fix64 F1, Fix64 F2)
- {
- if (F2.RawValue == 0 && F1.RawValue == 0)
- return (Fix64)0;
- Fix64 result = (Fix64)0;
- if (F2 > 0)
- result = Atan(F1 / F2);
- else if (F2 < 0)
- {
- if (F1 >= (Fix64)0)
- result = (PI - Atan(Abs(F1 / F2)));
- else
- result = -(PI - Atan(Abs(F1 / F2)));
- }
- else
- result = (F1 >= (Fix64)0 ? PI : -PI) / (Fix64)2;
- return result;
- }
- #endregion
- }
- public struct FixVector3
- {
- public Fix64 x;
- public Fix64 y;
- public Fix64 z;
- public FixVector3(Fix64 x, Fix64 y, Fix64 z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public FixVector3(UnityEngine.Vector3 v3Pos)
- {
- this.x = (Fix64)v3Pos.x;
- this.y = (Fix64)v3Pos.y;
- this.z = (Fix64)v3Pos.z;
- }
- public FixVector3(FixVector3 v)
- {
- this.x = v.x;
- this.y = v.y;
- this.z = v.z;
- }
- public Fix64 this[int index]
- {
- 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;
- }
- }
- public static FixVector3 Up
- {
- get { return new FixVector3(Fix64.Zero, Fix64.One, Fix64.Zero); }
- }
- public static FixVector3 Zero
- {
- get { return new FixVector3(Fix64.Zero, Fix64.Zero, Fix64.Zero); }
- }
- public static FixVector3 operator +(FixVector3 a, FixVector3 b)
- {
- Fix64 x = a.x + b.x;
- Fix64 y = a.y + b.y;
- Fix64 z = a.z + b.z;
- return new FixVector3(x, y, z);
- }
- public static FixVector3 operator -(FixVector3 a, FixVector3 b)
- {
- Fix64 x = a.x - b.x;
- Fix64 y = a.y - b.y;
- Fix64 z = a.z - b.z;
- return new FixVector3(x, y, z);
- }
- public static FixVector3 operator *(Fix64 d, FixVector3 a)
- {
- Fix64 x = a.x * d;
- Fix64 y = a.y * d;
- Fix64 z = a.z * d;
- return new FixVector3(x, y, z);
- }
- public static FixVector3 operator *(FixVector3 a, Fix64 d)
- {
- Fix64 x = a.x * d;
- Fix64 y = a.y * d;
- Fix64 z = a.z * d;
- return new FixVector3(x, y, z);
- }
- public static FixVector3 operator /(FixVector3 a, Fix64 d)
- {
- Fix64 x = a.x / d;
- Fix64 y = a.y / d;
- Fix64 z = a.z / d;
- return new FixVector3(x, y, z);
- }
- public static Fix64 Dot(FixVector3 a, FixVector3 b)
- {
- return a.x*b.x + a.y*b.y + a.z*b.z;
- }
- public static FixVector3 Cross(FixVector3 a, FixVector3 b)
- {
- Fix64 x = a.y * b.z - a.z * b.y;
- Fix64 y = a.z * b.x - a.x * b.z;
- Fix64 z = a.x * b.y - a.y * b.x;
- return new FixVector3(x, y, z);
- }
- public static bool operator ==(FixVector3 lhs, FixVector3 rhs)
- {
- return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
- }
- public static bool operator !=(FixVector3 lhs, FixVector3 rhs)
- {
- return lhs.x != rhs.x || lhs.y != rhs.y || lhs.z != rhs.z;
- }
- public static Fix64 SqrMagnitude(FixVector3 a)
- {
- return a.x * a.x + a.y * a.y + a.z * a.z;
- }
- public static Fix64 Distance(FixVector3 a, FixVector3 b)
- {
- return Magnitude(a - b);
- }
- public static Fix64 Magnitude(FixVector3 a)
- {
- return Fix64.Sqrt(FixVector3.SqrMagnitude(a));
- }
- public void Normalize()
- {
- Fix64 n = x * x + y * y + z * z;
- if (n == Fix64.Zero)
- return;
- n = Fix64.Sqrt(n);
- if (n < (Fix64)0.0001)
- {
- return;
- }
- n = 1 / n;
- x *= n;
- y *= n;
- z *= n;
- }
- public FixVector3 GetNormalized()
- {
- FixVector3 v = new FixVector3(this);
- 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)
- {
- return obj is FixVector2 && ((FixVector3)obj) == this;
- }
- public override int GetHashCode()
- {
- return this.x.GetHashCode() + this.y.GetHashCode() + this.z.GetHashCode();
- }
- public static FixVector3 Lerp(FixVector3 from, FixVector3 to, Fix64 factor)
- {
- return from * (1 - factor) + to * factor;
- }
- public UnityEngine.Vector3 ToVector3()
- {
- return new UnityEngine.Vector3((float)x, (float)y, (float)z);
- }
- }
- public struct FixVector2
- {
- public Fix64 x;
- public Fix64 y;
- public FixVector2(Fix64 x, Fix64 y)
- {
- this.x = x;
- this.y = y;
- }
- public FixVector2(Fix64 x, int y)
- {
- this.x = x;
- this.y = (Fix64)y;
- }
- public FixVector2(int x, int y)
- {
- this.x = (Fix64)x;
- this.y = (Fix64)y;
- }
- public FixVector2(FixVector2 v)
- {
- this.x = v.x;
- this.y = v.y;
- }
- public static FixVector2 operator -(FixVector2 a, int b)
- {
- Fix64 x = a.x - b;
- Fix64 y = a.y - b;
- return new FixVector2(x, y);
- }
- public Fix64 this[int index]
- {
- get { return index == 0 ? x : y; }
- set
- {
- if (index == 0)
- {
- x = value;
- }
- else
- {
- y = value;
- }
- }
- }
- public static FixVector2 Zero
- {
- get { return new FixVector2(Fix64.Zero, Fix64.Zero); }
- }
- public static FixVector2 operator +(FixVector2 a, FixVector2 b)
- {
- Fix64 x = a.x + b.x;
- Fix64 y = a.y + b.y;
- return new FixVector2(x, y);
- }
- public static FixVector2 operator -(FixVector2 a, FixVector2 b)
- {
- Fix64 x = a.x - b.x;
- Fix64 y = a.y - b.y;
- return new FixVector2(x, y);
- }
- public static FixVector2 operator *(Fix64 d, FixVector2 a)
- {
- Fix64 x = a.x * d;
- Fix64 y = a.y * d;
- return new FixVector2(x, y);
- }
- public static FixVector2 operator *(FixVector2 a, Fix64 d)
- {
- Fix64 x = a.x * d;
- Fix64 y = a.y * d;
- return new FixVector2(x, y);
- }
- public static FixVector2 operator /(FixVector2 a, Fix64 d)
- {
- Fix64 x = a.x / d;
- Fix64 y = a.y / d;
- return new FixVector2(x, y);
- }
- public static bool operator ==(FixVector2 lhs, FixVector2 rhs)
- {
- return lhs.x == rhs.x && lhs.y == rhs.y;
- }
- public static bool operator !=(FixVector2 lhs, FixVector2 rhs)
- {
- return lhs.x != rhs.x || lhs.y != rhs.y;
- }
- public override bool Equals(object obj)
- {
- return obj is FixVector2 && ((FixVector2)obj) == this;
- }
- public override int GetHashCode()
- {
- return this.x.GetHashCode() + this.y.GetHashCode();
- }
- public static Fix64 SqrMagnitude(FixVector2 a)
- {
- return a.x * a.x + a.y * a.y;
- }
- public static Fix64 Distance(FixVector2 a, FixVector2 b)
- {
- return Magnitude(a - b);
- }
- public static Fix64 Magnitude(FixVector2 a)
- {
- return Fix64.Sqrt(FixVector2.SqrMagnitude(a));
- }
- public void Normalize()
- {
- Fix64 n = x * x + y * y;
- if (n == Fix64.Zero)
- return;
- n = Fix64.Sqrt(n);
- if (n < (Fix64)0.0001)
- {
- return;
- }
- n = 1 / n;
- x *= n;
- y *= n;
- }
- public FixVector2 GetNormalized()
- {
- FixVector2 v = new FixVector2(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 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);
- }
- public UnityEngine.Vector2 ToVector2()
- {
- return new UnityEngine.Vector2((float)x, (float)y);
- }
- }
- 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);
- }
- public UnityEngine.Vector2 ToVector2()
- {
- return new UnityEngine.Vector2((int)x, (int)y);
- }
- }
|