| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- using System;
- using System.Globalization;
- namespace UnityEngine
- {
- [Serializable]
- public struct Vector2: IEquatable<Vector2>
- {
- public static readonly Vector2 zero = new Vector2();
- public static readonly Vector2 one = new Vector2(1f, 1f);
-
- public float x;
- public float y;
-
- public static implicit operator Vector2(Vector3 v)
- {
- return new Vector2(v.x, v.y);
- }
- public static implicit operator Vector3(Vector2 v)
- {
- return new Vector3(v.x, v.y, 0.0f);
- }
- public Vector2(float x, float y)
- {
- this.x = x;
- this.y = y;
- }
- public Vector2(float value)
- {
- this.x = this.y = value;
- }
- public float this[int index]
- {
- get
- {
- if (index == 0)
- return this.x;
- if (index == 1)
- return this.y;
- throw new IndexOutOfRangeException("Invalid Vector2 index!");
- }
- set
- {
- if (index != 0)
- {
- if (index != 1)
- throw new IndexOutOfRangeException("Invalid Vector2 index!");
- this.y = value;
- }
- else
- this.x = value;
- }
- }
- public override string ToString()
- {
- CultureInfo currentCulture = CultureInfo.CurrentCulture;
- return string.Format(currentCulture, "{0}, {1}",
- new object[2]
- {
- this.x.ToString(currentCulture),
- this.y.ToString(currentCulture)
- });
- }
- public bool Equals(Vector2 other)
- {
- return this == other;
- }
- public override bool Equals(object obj)
- {
- bool flag = false;
- if (obj is Vector2)
- flag = this.Equals((Vector2) obj);
- return flag;
- }
- public override int GetHashCode()
- {
- return this.x.GetHashCode() + this.y.GetHashCode();
- }
- public float Length()
- {
- return (float) Math.Sqrt(this.x * (double) this.x + this.y * (double) this.y);
- }
- public float LengthSquared()
- {
- return (float) (this.x * (double) this.x + this.y * (double) this.y);
- }
-
- public float magnitude
- {
- get
- {
- return this.Length();
- }
- }
-
- public float sqrMagnitude
- {
- get
- {
- return this.LengthSquared();
- }
- }
- public static float Distance(Vector2 value1, Vector2 value2)
- {
- float num1 = value1.x - value2.x;
- float num2 = value1.y - value2.y;
- return (float) Math.Sqrt(num1 * (double) num1 + num2 * (double) num2);
- }
- public static void Distance(ref Vector2 value1, ref Vector2 value2, out float result)
- {
- float num1 = value1.x - value2.x;
- float num2 = value1.y - value2.y;
- float num3 = (float) (num1 * (double) num1 + num2 * (double) num2);
- result = (float) Math.Sqrt(num3);
- }
- public static float DistanceSquared(Vector2 value1, Vector2 value2)
- {
- float num1 = value1.x - value2.x;
- float num2 = value1.y - value2.y;
- return (float) (num1 * (double) num1 + num2 * (double) num2);
- }
- public static void DistanceSquared(ref Vector2 value1, ref Vector2 value2, out float result)
- {
- float num1 = value1.x - value2.x;
- float num2 = value1.y - value2.y;
- result = (float) (num1 * (double) num1 + num2 * (double) num2);
- }
- public void Normalize()
- {
- float num1 = (float) (this.x * (double) this.x + this.y * (double) this.y);
- if (num1 < 9.99999974737875E-06)
- return;
- float num2 = 1f / (float) Math.Sqrt(num1);
- this.x *= num2;
- this.y *= num2;
- }
- public static Vector2 Normalize(Vector2 value)
- {
- float num1 = (float) (value.x * (double) value.x + value.y * (double) value.y);
- if (num1 < 9.99999974737875E-06)
- return value;
- float num2 = 1f / (float) Math.Sqrt(num1);
- Vector2 vector2;
- vector2.x = value.x * num2;
- vector2.y = value.y * num2;
- return vector2;
- }
- public static void Normalize(ref Vector2 value, out Vector2 result)
- {
- float num1 = (float) (value.x * (double) value.x + value.y * (double) value.y);
- if (num1 < 9.99999974737875E-06)
- {
- result = value;
- }
- else
- {
- float num2 = 1f / (float) Math.Sqrt(num1);
- result.x = value.x * num2;
- result.y = value.y * num2;
- }
- }
-
- public Vector2 normalized
- {
- get
- {
- return Vector2.Normalize(this);
- }
- }
- public static Vector2 Reflect(Vector2 vector, Vector2 normal)
- {
- float num = (float) (vector.x * (double) normal.x + vector.y * (double) normal.y);
- Vector2 vector2;
- vector2.x = vector.x - 2f * num * normal.x;
- vector2.y = vector.y - 2f * num * normal.y;
- return vector2;
- }
- public static void Reflect(ref Vector2 vector, ref Vector2 normal, out Vector2 result)
- {
- float num = (float) (vector.x * (double) normal.x + vector.y * (double) normal.y);
- result.x = vector.x - 2f * num * normal.x;
- result.y = vector.y - 2f * num * normal.y;
- }
- public static Vector2 Min(Vector2 value1, Vector2 value2)
- {
- Vector2 vector2;
- vector2.x = (double) value1.x < (double) value2.x? value1.x : value2.x;
- vector2.y = (double) value1.y < (double) value2.y? value1.y : value2.y;
- return vector2;
- }
- public static void Min(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
- {
- result.x = (double) value1.x < (double) value2.x? value1.x : value2.x;
- result.y = (double) value1.y < (double) value2.y? value1.y : value2.y;
- }
- public static Vector2 Max(Vector2 value1, Vector2 value2)
- {
- Vector2 vector2;
- vector2.x = (double) value1.x > (double) value2.x? value1.x : value2.x;
- vector2.y = (double) value1.y > (double) value2.y? value1.y : value2.y;
- return vector2;
- }
- public static void Max(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
- {
- result.x = (double) value1.x > (double) value2.x? value1.x : value2.x;
- result.y = (double) value1.y > (double) value2.y? value1.y : value2.y;
- }
- public static Vector2 Clamp(Vector2 value1, Vector2 min, Vector2 max)
- {
- float x = value1.x;
- float num1 = (double) x > (double) max.x? max.x : x;
- float num2 = (double) num1 < (double) min.x? min.x : num1;
- float y = value1.y;
- float num3 = (double) y > (double) max.y? max.y : y;
- float num4 = (double) num3 < (double) min.y? min.y : num3;
- Vector2 vector2;
- vector2.x = num2;
- vector2.y = num4;
- return vector2;
- }
- public static void Clamp(ref Vector2 value1, ref Vector2 min, ref Vector2 max, out Vector2 result)
- {
- float x = value1.x;
- float num1 = (double) x > (double) max.x? max.x : x;
- float num2 = (double) num1 < (double) min.x? min.x : num1;
- float y = value1.y;
- float num3 = (double) y > (double) max.y? max.y : y;
- float num4 = (double) num3 < (double) min.y? min.y : num3;
- result.x = num2;
- result.y = num4;
- }
- public static Vector2 Lerp(Vector2 value1, Vector2 value2, float amount)
- {
- Vector2 vector2;
- vector2.x = value1.x + (value2.x - value1.x) * amount;
- vector2.y = value1.y + (value2.y - value1.y) * amount;
- return vector2;
- }
- public static void Lerp(ref Vector2 value1, ref Vector2 value2, float amount, out Vector2 result)
- {
- result.x = value1.x + (value2.x - value1.x) * amount;
- result.y = value1.y + (value2.y - value1.y) * amount;
- }
- public static Vector2 SmoothStep(Vector2 value1, Vector2 value2, float amount)
- {
- amount = (double) amount > 1.0? 1f : ((double) amount < 0.0? 0.0f : amount);
- amount = (float) (amount * (double) amount * (3.0 - 2.0 * amount));
- Vector2 vector2;
- vector2.x = value1.x + (value2.x - value1.x) * amount;
- vector2.y = value1.y + (value2.y - value1.y) * amount;
- return vector2;
- }
- public static void SmoothStep(ref Vector2 value1, ref Vector2 value2, float amount, out Vector2 result)
- {
- amount = (double) amount > 1.0? 1f : ((double) amount < 0.0? 0.0f : amount);
- amount = (float) (amount * (double) amount * (3.0 - 2.0 * amount));
- result.x = value1.x + (value2.x - value1.x) * amount;
- result.y = value1.y + (value2.y - value1.y) * amount;
- }
- public static Vector2 Negate(Vector2 value)
- {
- Vector2 vector2;
- vector2.x = -value.x;
- vector2.y = -value.y;
- return vector2;
- }
- public static void Negate(ref Vector2 value, out Vector2 result)
- {
- result.x = -value.x;
- result.y = -value.y;
- }
- public static float Dot(Vector2 value1, Vector2 value2)
- {
- return (float) (value1.x * (double) value2.x + value1.y * (double) value2.y);
- }
- public static void Dot(ref Vector2 value1, ref Vector2 value2, out float result)
- {
- result = (float) (value1.x * (double) value2.x + value1.y * (double) value2.y);
- }
- public static float Angle(Vector2 from, Vector2 to)
- {
- from.Normalize();
- to.Normalize();
- float result;
- Vector2.Dot(ref from, ref to, out result);
- return Mathf.Cos(Mathf.Clamp(result, -1f, 1f)) * 57.29578f;
- }
- public static void Angle(ref Vector2 from, ref Vector2 to, out float result)
- {
- from.Normalize();
- to.Normalize();
- float result1;
- Vector2.Dot(ref from, ref to, out result1);
- result = Mathf.Cos(Mathf.Clamp(result1, -1f, 1f)) * 57.29578f;
- }
- public static Vector2 Add(Vector2 value1, Vector2 value2)
- {
- Vector2 vector2;
- vector2.x = value1.x + value2.x;
- vector2.y = value1.y + value2.y;
- return vector2;
- }
- public static void Add(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
- {
- result.x = value1.x + value2.x;
- result.y = value1.y + value2.y;
- }
- public static Vector2 Sub(Vector2 value1, Vector2 value2)
- {
- Vector2 vector2;
- vector2.x = value1.x - value2.x;
- vector2.y = value1.y - value2.y;
- return vector2;
- }
- public static void Sub(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
- {
- result.x = value1.x - value2.x;
- result.y = value1.y - value2.y;
- }
- public static Vector2 Multiply(Vector2 value1, Vector2 value2)
- {
- Vector2 vector2;
- vector2.x = value1.x * value2.x;
- vector2.y = value1.y * value2.y;
- return vector2;
- }
- public static void Multiply(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
- {
- result.x = value1.x * value2.x;
- result.y = value1.y * value2.y;
- }
- public static Vector2 Multiply(Vector2 value1, float scaleFactor)
- {
- Vector2 vector2;
- vector2.x = value1.x * scaleFactor;
- vector2.y = value1.y * scaleFactor;
- return vector2;
- }
- public static void Multiply(ref Vector2 value1, float scaleFactor, out Vector2 result)
- {
- result.x = value1.x * scaleFactor;
- result.y = value1.y * scaleFactor;
- }
- public static Vector2 Divide(Vector2 value1, Vector2 value2)
- {
- Vector2 vector2;
- vector2.x = value1.x / value2.x;
- vector2.y = value1.y / value2.y;
- return vector2;
- }
- public static void Divide(ref Vector2 value1, ref Vector2 value2, out Vector2 result)
- {
- result.x = value1.x / value2.x;
- result.y = value1.y / value2.y;
- }
- public static Vector2 Divide(Vector2 value1, float divider)
- {
- float num = 1f / divider;
- Vector2 vector2;
- vector2.x = value1.x * num;
- vector2.y = value1.y * num;
- return vector2;
- }
- public static void Divide(ref Vector2 value1, float divider, out Vector2 result)
- {
- float num = 1f / divider;
- result.x = value1.x * num;
- result.y = value1.y * num;
- }
- public static Vector2 operator -(Vector2 value)
- {
- Vector2 vector2;
- vector2.x = -value.x;
- vector2.y = -value.y;
- return vector2;
- }
- public static bool operator ==(Vector2 lhs, Vector2 rhs)
- {
- return (lhs - rhs).sqrMagnitude < 9.99999943962493E-11;
- }
- public static bool operator !=(Vector2 lhs, Vector2 rhs)
- {
- return !(lhs == rhs);
- }
- public static Vector2 operator +(Vector2 value1, Vector2 value2)
- {
- Vector2 vector2;
- vector2.x = value1.x + value2.x;
- vector2.y = value1.y + value2.y;
- return vector2;
- }
- public static Vector2 operator -(Vector2 value1, Vector2 value2)
- {
- Vector2 vector2;
- vector2.x = value1.x - value2.x;
- vector2.y = value1.y - value2.y;
- return vector2;
- }
- public static Vector2 operator *(Vector2 value1, Vector2 value2)
- {
- Vector2 vector2;
- vector2.x = value1.x * value2.x;
- vector2.y = value1.y * value2.y;
- return vector2;
- }
- public static Vector2 operator *(Vector2 value, float scaleFactor)
- {
- Vector2 vector2;
- vector2.x = value.x * scaleFactor;
- vector2.y = value.y * scaleFactor;
- return vector2;
- }
- public static Vector2 operator *(float scaleFactor, Vector2 value)
- {
- Vector2 vector2;
- vector2.x = value.x * scaleFactor;
- vector2.y = value.y * scaleFactor;
- return vector2;
- }
- public static Vector2 operator /(Vector2 value1, Vector2 value2)
- {
- Vector2 vector2;
- vector2.x = value1.x / value2.x;
- vector2.y = value1.y / value2.y;
- return vector2;
- }
- public static Vector2 operator /(Vector2 value1, float divider)
- {
- float num = 1f / divider;
- Vector2 vector2;
- vector2.x = value1.x * num;
- vector2.y = value1.y * num;
- return vector2;
- }
- }
- }
|