Advertisement
ParnassianStudios

Unity - Vector3i

Oct 28th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.57 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3.  
  4. namespace Lysander
  5. {
  6.     [Serializable]
  7.     public struct Vector3i : IEquatable<Vector3i>
  8.     {
  9.         // Properties
  10.         public int x { get; set; }
  11.         public int y { get; set; }
  12.         public int z { get; set; }
  13.  
  14.         public Vector3i normalized
  15.         {
  16.             get
  17.             {
  18.                 Vector3i newVector = new Vector3i(this.x, this.y, this.z);
  19.                 newVector.Normalize();
  20.                 return newVector;
  21.             }
  22.         }
  23.  
  24.         public float magnitude { get { return Mathf.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z); } }
  25.  
  26.         public float sqrMagnitude { get { return this.x * this.x + this.y * this.y + this.z * this.z; } }
  27.  
  28.  
  29.         // Indexer
  30.         public int this[int index]
  31.         {
  32.             get
  33.             {
  34.                 switch (index)
  35.                 {
  36.                     case 0:
  37.                         return this.x;
  38.                     case 1:
  39.                         return this.y;
  40.                     case 2:
  41.                         return this.z;
  42.                     default:
  43.                         throw new IndexOutOfRangeException("Invalid Vector3i index!");
  44.                 }
  45.             }
  46.  
  47.             set
  48.             {
  49.                 switch (index)
  50.                 {
  51.                     case 0:
  52.                         this.x = value;
  53.                         break;
  54.                     case 1:
  55.                         this.y = value;
  56.                         break;
  57.                     case 2:
  58.                         this.z = value;
  59.                         break;
  60.                     default:
  61.                         throw new IndexOutOfRangeException("Invalid Vector3i index!");
  62.                 }
  63.             }
  64.         }
  65.  
  66.  
  67.         // Constructors
  68.         public Vector3i(int x, int y, int z) : this()
  69.         {
  70.             this.x = x;
  71.             this.y = y;
  72.             this.z = z;
  73.         }
  74.  
  75.         public Vector3i(int x, int y) : this()
  76.         {
  77.             this.x = x;
  78.             this.y = y;
  79.             this.z = 0;
  80.         }
  81.  
  82.  
  83.         // Static Conversions From Other Types
  84.         public static Vector3i fromVector2(Vector2 vector2)
  85.         {
  86.             Vector3i newVector = new Vector3i();
  87.             newVector.x = Mathf.RoundToInt(vector2.x);
  88.             newVector.y = Mathf.RoundToInt(vector2.y);
  89.             return newVector;
  90.         }
  91.  
  92.         public static Vector3i fromVector3(Vector3 vector3)
  93.         {
  94.             Vector3i newVector = new Vector3i();
  95.             newVector.x = Mathf.RoundToInt(vector3.x);
  96.             newVector.y = Mathf.RoundToInt(vector3.y);
  97.             newVector.z = Mathf.RoundToInt(vector3.z);
  98.             return newVector;
  99.         }
  100.  
  101.         public static Vector3i fromVector2i(Vector2i vector2i)
  102.         {
  103.             Vector3i newVector = new Vector3i();
  104.             newVector.x = vector2i.x;
  105.             newVector.y = vector2i.y;
  106.             return newVector;
  107.         }
  108.  
  109.  
  110.         // Conversions To Other Types
  111.         public Vector2 toVector2() { return new Vector2(this.x, this.y); }
  112.  
  113.         public Vector3 toVector3() { return new Vector3(this.x, this.y, this.z); }
  114.  
  115.         public Vector2i toVector2i() { return new Vector2i(this.x, this.y); }
  116.  
  117.  
  118.         // Static Vector3i Values
  119.         public readonly static Vector3i zero = new Vector3i(0, 0, 0);
  120.  
  121.         public readonly static Vector3i forward = new Vector3i(0, 0, 1);
  122.  
  123.         public readonly static Vector3i back = new Vector3i(0, 0, -1);
  124.  
  125.         public readonly static Vector3i up = new Vector3i(0, 1, 0);
  126.  
  127.         public readonly static Vector3i down = new Vector3i(0, -1, 0);
  128.  
  129.         public readonly static Vector3i right = new Vector3i(1, 0, 0);
  130.  
  131.         public readonly static Vector3i left = new Vector3i(-1, 0, 0);
  132.  
  133.         public readonly static Vector3i one = new Vector3i(1, 1, 1);
  134.  
  135.  
  136.         // Other Static Functions
  137.         public static Vector3i Scale(Vector3i a, Vector3i b)
  138.         {
  139.             return new Vector3i(a.x * b.x, a.y * b.y, a.z * b.z);
  140.         }
  141.  
  142.         public static Vector3i Cross(Vector3i a, Vector3i b)
  143.         {
  144.             return new Vector3i(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
  145.         }
  146.  
  147.         public static Vector3i Reflect(Vector3i inDirection, Vector3i inNormal)
  148.         {
  149.             return -2f * Vector3i.Dot(inNormal, inDirection) * inNormal + inDirection;
  150.         }
  151.  
  152.         public static float Dot(Vector3i a, Vector3i b)
  153.         {
  154.             return a.x * b.x + a.y * b.y + a.z * b.z;
  155.         }
  156.  
  157.         public static float Angle(Vector3i from, Vector3i to)
  158.         {
  159.             return Mathf.Acos(Mathf.Clamp(Vector3i.Dot(from.normalized, to.normalized), -1f, 1f)) * 57.29578f;
  160.         }
  161.  
  162.         public static float Distance(Vector3i a, Vector3i b)
  163.         {
  164.             Vector3i vector = new Vector3i(a.x - b.x, a.y - b.y, a.z - b.z);
  165.             return Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
  166.         }
  167.  
  168.         public static Vector3i ClampMagnitude(Vector3i vector, float maxLength)
  169.         {
  170.             if (vector.sqrMagnitude > maxLength * maxLength)
  171.             {
  172.                 return vector.normalized * maxLength;
  173.             }
  174.             return vector;
  175.         }
  176.  
  177.         public static Vector3i Min(Vector3i a, Vector3i b)
  178.         {
  179.             return new Vector3i(Mathf.Min(a.x, b.x), Mathf.Min(a.y, b.y), Mathf.Min(a.z, b.z));
  180.         }
  181.  
  182.         public static Vector3i Max(Vector3i a, Vector3i b)
  183.         {
  184.             return new Vector3i(Mathf.Max(a.x, b.x), Mathf.Max(a.y, b.y), Mathf.Max(a.z, b.z));
  185.         }
  186.  
  187.  
  188.         // Other Functions
  189.         public void Set(int x, int y, int z)
  190.         {
  191.             this.x = x;
  192.             this.y = y;
  193.             this.z = z;
  194.         }
  195.  
  196.         public void Scale(Vector3i scale)
  197.         {
  198.             this.x *= scale.x;
  199.             this.y *= scale.y;
  200.             this.z *= scale.z;
  201.         }
  202.  
  203.         public void Normalize()
  204.         {
  205.             float num = magnitude;
  206.             if (num > 1E-05f)
  207.                 this /= num;
  208.             else
  209.                 this = Vector3i.zero;
  210.         }
  211.  
  212.         public void ShiftAll(int amount)
  213.         {
  214.             this.x += amount;
  215.             this.y += amount;
  216.             this.z += amount;
  217.         }
  218.  
  219.         public void ShiftAll(float amount)
  220.         {
  221.             ShiftAll(Mathf.RoundToInt(amount));
  222.         }
  223.  
  224.  
  225.         // Vector2i & Vector2i Operator Overloads
  226.         public static Vector3i operator +(Vector3i a, Vector3i b)
  227.         {
  228.             return new Vector3i(a.x + b.x, a.y + b.y, a.z + b.z);
  229.         }
  230.  
  231.         public static Vector3i operator -(Vector3i a, Vector3i b)
  232.         {
  233.             return new Vector3i(a.x - b.x, a.y - b.y, a.z - b.z);
  234.         }
  235.  
  236.         public static Vector3i operator -(Vector3i a)
  237.         {
  238.             return new Vector3i(-a.x, -a.y, -a.z);
  239.         }
  240.  
  241.         public static Vector3i operator *(Vector3i a, Vector3i b)
  242.         {
  243.             return new Vector3i(a.x * b.x, a.y * b.y, a.z * b.z);
  244.         }
  245.  
  246.         public static Vector3i operator /(Vector3i a, Vector3i b)
  247.         {
  248.             return new Vector3i(a.x / b.x, a.y / b.y, a.z / b.z);
  249.         }
  250.  
  251.  
  252.         // Vector2i & Int Operator Overloads
  253.         public static Vector3i operator *(Vector3i a, int d)
  254.         {
  255.             return new Vector3i(a.x * d, a.y * d, a.z * d);
  256.         }
  257.  
  258.         public static Vector3i operator *(Vector3i a, float d)
  259.         {
  260.             return new Vector3i(Mathf.RoundToInt(a.x * d), Mathf.RoundToInt(a.y * d), Mathf.RoundToInt(a.z * d));
  261.         }
  262.  
  263.         public static Vector3i operator *(int d, Vector3i a)
  264.         {
  265.             return new Vector3i(a.x * d, a.y * d, a.z * d);
  266.         }
  267.  
  268.         public static Vector3i operator *(float d, Vector3i a)
  269.         {
  270.             return new Vector3i(Mathf.RoundToInt(a.x * d), Mathf.RoundToInt(a.y * d), Mathf.RoundToInt(a.z * d));
  271.         }
  272.  
  273.         public static Vector3i operator /(Vector3i a, int d)
  274.         {
  275.             return new Vector3i(a.x / d, a.y / d, a.z / d);
  276.         }
  277.  
  278.         public static Vector3i operator /(Vector3i a, float d)
  279.         {
  280.             return new Vector3i(Mathf.RoundToInt((float)a.x / d), Mathf.RoundToInt((float)a.y / d), Mathf.RoundToInt((float)a.z / d));
  281.         }
  282.  
  283.  
  284.         // Equality and Inequality Operator Overloads
  285.         public static bool operator ==(Vector3i a, Vector3i b)
  286.         {
  287.             return a.x.Equals(b.x) && a.y.Equals(b.y) && a.z.Equals(b.z);
  288.         }
  289.  
  290.         public static bool operator !=(Vector3i a, Vector3i b)
  291.         {
  292.             return !a.x.Equals(b.x) || !a.y.Equals(b.y) || !a.z.Equals(b.z);
  293.         }
  294.  
  295.  
  296.         // Overrides & Implementations
  297.         public override bool Equals(object obj)
  298.         {
  299.             if (obj is Vector3i)
  300.             {
  301.                 Vector3i vector = (Vector3i)obj;
  302.                 return this.x.Equals(vector.x) && this.y.Equals(vector.y) && this.z.Equals(vector.z);
  303.             }
  304.             return false;
  305.         }
  306.  
  307.         public bool Equals(Vector3i vector)
  308.         {
  309.             return this.x.Equals(vector.x) && this.y.Equals(vector.y) && this.z.Equals(vector.z);
  310.         }
  311.  
  312.         public override int GetHashCode()
  313.         {
  314.             return this.x.GetHashCode() ^ this.y.GetHashCode() << 2 ^ this.z.GetHashCode() >> 2;
  315.         }
  316.  
  317.         public override string ToString()
  318.         {
  319.             return "(" + this.x.ToString() + ", " + this.y.ToString() + ", " + this.z.ToString() + ")";
  320.         }
  321.     }
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement