Skip to content

Instantly share code, notes, and snippets.

@RevenantX
Created August 8, 2017 06:06
Show Gist options
  • Save RevenantX/24b16791f7b7115c0f708434fe954d38 to your computer and use it in GitHub Desktop.
Save RevenantX/24b16791f7b7115c0f708434fe954d38 to your computer and use it in GitHub Desktop.
#if !UNITY
using System;
namespace UnityEngine
{
public struct Vector3
{
public float x;
public float y;
public float z;
public static readonly Vector3 forward = new Vector3(0f, 0f, 1f);
public static readonly Vector3 zero = new Vector3(0f, 0f, 0f);
public static readonly Vector3 up = new Vector3(0f, 1f, 0f);
public static readonly Vector3 down = new Vector3(0f, -1f, 0f);
public Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public static Vector3 Cross(Vector3 lhs, Vector3 rhs)
{
return new Vector3(lhs.y*rhs.z - lhs.z*rhs.y, lhs.z*rhs.x - lhs.x*rhs.z, lhs.x*rhs.y - lhs.y*rhs.x);
}
public static Vector3 Lerp(Vector3 from, Vector3 to, float t)
{
if (t < 0f)
t = 0f;
else if (t > 1f)
t = 1f;
return new Vector3(from.x + (to.x - from.x) * t, from.y + (to.y - from.y) * t, from.z + (to.z - from.z) * t);
}
public float sqrMagnitude
{
get { return x*x + y*y + z*z; }
}
public float magnitude
{
get { return (float) Math.Sqrt(x*x + y*y + z*z); }
}
public Vector3 normalized
{
get
{
if (x == 0f && y == 0f && z == 0f)
return this;
float mag = (float) Math.Sqrt(x*x + y*y + z*z);
return new Vector3(x/mag, y/mag, z/mag);
}
}
public void Normalize()
{
if (x == 0f && y == 0f && z == 0f)
return;
float mag = (float) Math.Sqrt(x*x + y*y + z*z);
x /= mag;
y /= mag;
z /= mag;
}
public override string ToString()
{
return string.Format("({0:0.00} {1:0.00} {2:0.00})", x, y, z);
}
public static Vector3 operator *(Vector3 v, float s)
{
return new Vector3(v.x*s, v.y*s, v.z*s);
}
public static Vector3 operator /(Vector3 v, float s)
{
return new Vector3(v.x / s, v.y / s, v.z / s);
}
public static bool operator ==(Vector3 v1, Vector3 v2)
{
return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
}
public static bool operator !=(Vector3 v1, Vector3 v2)
{
return v1.x != v2.x || v1.y != v2.y || v1.z != v2.z;
}
public override bool Equals(Object o)
{
if (o == null)
return false;
if (!(o is Vector3))
{
return false;
}
Vector3 v = (Vector3) o;
return (x == v.x && y == v.y && z == v.z);
}
public override int GetHashCode()
{
return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
}
public static Vector3 operator +(Vector3 v1, Vector3 v2)
{
return new Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}
public static Vector3 operator -(Vector3 v1, Vector3 v2)
{
return new Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment