Advertisement
RaWRCoder

Vector (3 floats)

Aug 16th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.24 KB | None | 0 0
  1.     public struct Vec3f
  2.     {
  3.         public static readonly Vec3f Zero = new Vec3f(0);
  4.         public static readonly Vec3f Forward = new Vec3f(0, 0, -1.0f);
  5.         public static readonly Vec3f Backward = -Forward;
  6.         public static readonly Vec3f Right = new Vec3f(1.0f);
  7.         public static readonly Vec3f Left = -Right;
  8.         public static readonly Vec3f Up = new Vec3f(0, 1.0f);
  9.         public static readonly Vec3f Down = -Up;
  10.  
  11.         public float x, y, z;
  12.  
  13.         public Vec3f(float _x = 0, float _y = 0, float _z = 0)
  14.         {
  15.             x = _x;
  16.             y = _y;
  17.             z = _z;
  18.         }
  19.         public float LengthSquared()
  20.         { return x * x + y * y + z * z; }
  21.         public float Length()
  22.         {
  23.             return MathF.sqrtfl(x*x + y*y + z*z);
  24.         }
  25.  
  26.         public void add(Vec3f other)
  27.         {
  28.             x += other.x;
  29.             y += other.y;
  30.             z += other.z;
  31.         }
  32.         public void add(ref Vec3f other)
  33.         {
  34.             x += other.x;
  35.             y += other.y;
  36.             z += other.z;
  37.         }
  38.         public void sub(Vec3f other)
  39.         {
  40.             x -= other.x;
  41.             y -= other.y;
  42.             z -= other.z;
  43.         }
  44.         public void sub(ref Vec3f other)
  45.         {
  46.             x -= other.x;
  47.             y -= other.y;
  48.             z -= other.z;
  49.         }
  50.         public void mul(Vec3f other)
  51.         {
  52.             x *= other.x;
  53.             y *= other.y;
  54.             z *= other.z;
  55.         }
  56.         public void mul(ref Vec3f other)
  57.         {
  58.             x *= other.x;
  59.             y *= other.y;
  60.             z *= other.z;
  61.         }
  62.         public void div(Vec3f other)
  63.         {
  64.             x /= other.x;
  65.             y /= other.y;
  66.             z /= other.z;
  67.         }
  68.         public void div(ref Vec3f other)
  69.         {
  70.             x /= other.x;
  71.             y /= other.y;
  72.             z /= other.z;
  73.         }
  74.         public void add(float other)
  75.         {
  76.             x += other;
  77.             y += other;
  78.             z += other;
  79.         }
  80.         public void sub(float other)
  81.         {
  82.             x -= other;
  83.             y -= other;
  84.             z -= other;
  85.         }
  86.         public void mul(float other)
  87.         {
  88.             x *= other;
  89.             y *= other;
  90.             z *= other;
  91.         }
  92.         public void div(float other)
  93.         {
  94.             x /= other;
  95.             y /= other;
  96.             z /= other;
  97.         }
  98.  
  99.         public void normalize()
  100.         {
  101.             var l = (float)Length();
  102.             x /= l;
  103.             y /= l;
  104.             z /= l;
  105.         }
  106.         public void negate()
  107.         {
  108.             x = -x;
  109.             y = -y;
  110.             z = -z;
  111.         }
  112.  
  113.         public static Vec3f operator +(Vec3f a, Vec3f b)
  114.         {
  115.             return new Vec3f(a.x + b.x, a.y + b.y, a.z + b.z);
  116.         }
  117.         public static Vec3f operator -(Vec3f a, Vec3f b)
  118.         {
  119.             return new Vec3f(a.x - b.x, a.y - b.y, a.z - b.z);
  120.         }
  121.         /// <summary>
  122.         /// Скалярное произведение двух векторов
  123.         /// </summary>
  124.         public static float operator *(Vec3f a, Vec3f b)
  125.         {
  126.             return a.x * b.x + a.y * b.y + a.z * b.z;
  127.         }
  128.         /// <summary>
  129.         /// Векторное произведение двух векторов
  130.         /// </summary>
  131.         public static Vec3f operator |(Vec3f a, Vec3f b)
  132.         {
  133.             return new Vec3f(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
  134.         }
  135.         /// <summary>
  136.         /// Угол между векторами (ненулевые!)
  137.         /// </summary>
  138.         public static float operator ^(Vec3f a, Vec3f b)
  139.         {
  140.             return MathF.acosfl((a.x*b.x + a.y*b.y + a.z*b.z)/(a.Length()*b.Length()));
  141.         }
  142.         public static Vec3f operator *(Vec3f a, float b)
  143.         {
  144.             return new Vec3f(a.x * b, a.y * b, a.z * b);
  145.         }
  146.         public static Vec3f operator /(Vec3f a, float b)
  147.         {
  148.             return new Vec3f(a.x / b, a.y / b, a.z / b);
  149.         }
  150.         public static Vec3f operator -(Vec3f a)
  151.         {
  152.             return new Vec3f(-a.x, -a.y, -a.z);
  153.         }
  154.         public static Vec3f operator !(Vec3f a)
  155.         {
  156.             var l = (float)a.Length();
  157.             return new Vec3f(a.x / l, a.y / l, a.z / l);
  158.         }
  159.         public override string ToString()
  160.         {
  161.             return string.Format("[{0:.00}, {1:.00}, {2:.00}]", x, y, z);
  162.         }
  163.         public unsafe static explicit operator byte[](Vec3f s)
  164.         {
  165.             var bts = new byte[12];
  166.             var ps = &s;
  167.             var btsp = (byte*)ps;
  168.             for (var i = 0; i < 12; i++)
  169.                 bts[i] = btsp[i];
  170.             return bts;
  171.         }
  172.         public unsafe static explicit operator Vec3f(byte[] bts)
  173.         {
  174.             fixed (byte* btsp = &bts[0])
  175.             {
  176.                 return *(Vec3f*)btsp;
  177.             }
  178.         }
  179.  
  180.         public static bool operator ==(Vec3f a, Vec3f b)
  181.         {
  182.             return a.x == b.x && a.y == b.y && a.z == b.z;
  183.         }
  184.         public static bool operator !=(Vec3f a, Vec3f b)
  185.         {
  186.             return !(a == b);
  187.         }
  188.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement