Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public struct Vec3f
- {
- public static readonly Vec3f Zero = new Vec3f(0);
- public static readonly Vec3f Forward = new Vec3f(0, 0, -1.0f);
- public static readonly Vec3f Backward = -Forward;
- public static readonly Vec3f Right = new Vec3f(1.0f);
- public static readonly Vec3f Left = -Right;
- public static readonly Vec3f Up = new Vec3f(0, 1.0f);
- public static readonly Vec3f Down = -Up;
- public float x, y, z;
- public Vec3f(float _x = 0, float _y = 0, float _z = 0)
- {
- x = _x;
- y = _y;
- z = _z;
- }
- public float LengthSquared()
- { return x * x + y * y + z * z; }
- public float Length()
- {
- return MathF.sqrtfl(x*x + y*y + z*z);
- }
- public void add(Vec3f other)
- {
- x += other.x;
- y += other.y;
- z += other.z;
- }
- public void add(ref Vec3f other)
- {
- x += other.x;
- y += other.y;
- z += other.z;
- }
- public void sub(Vec3f other)
- {
- x -= other.x;
- y -= other.y;
- z -= other.z;
- }
- public void sub(ref Vec3f other)
- {
- x -= other.x;
- y -= other.y;
- z -= other.z;
- }
- public void mul(Vec3f other)
- {
- x *= other.x;
- y *= other.y;
- z *= other.z;
- }
- public void mul(ref Vec3f other)
- {
- x *= other.x;
- y *= other.y;
- z *= other.z;
- }
- public void div(Vec3f other)
- {
- x /= other.x;
- y /= other.y;
- z /= other.z;
- }
- public void div(ref Vec3f other)
- {
- x /= other.x;
- y /= other.y;
- z /= other.z;
- }
- public void add(float other)
- {
- x += other;
- y += other;
- z += other;
- }
- public void sub(float other)
- {
- x -= other;
- y -= other;
- z -= other;
- }
- public void mul(float other)
- {
- x *= other;
- y *= other;
- z *= other;
- }
- public void div(float other)
- {
- x /= other;
- y /= other;
- z /= other;
- }
- public void normalize()
- {
- var l = (float)Length();
- x /= l;
- y /= l;
- z /= l;
- }
- public void negate()
- {
- x = -x;
- y = -y;
- z = -z;
- }
- public static Vec3f operator +(Vec3f a, Vec3f b)
- {
- return new Vec3f(a.x + b.x, a.y + b.y, a.z + b.z);
- }
- public static Vec3f operator -(Vec3f a, Vec3f b)
- {
- return new Vec3f(a.x - b.x, a.y - b.y, a.z - b.z);
- }
- /// <summary>
- /// Скалярное произведение двух векторов
- /// </summary>
- public static float operator *(Vec3f a, Vec3f b)
- {
- return a.x * b.x + a.y * b.y + a.z * b.z;
- }
- /// <summary>
- /// Векторное произведение двух векторов
- /// </summary>
- public static Vec3f operator |(Vec3f a, Vec3f b)
- {
- 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);
- }
- /// <summary>
- /// Угол между векторами (ненулевые!)
- /// </summary>
- public static float operator ^(Vec3f a, Vec3f b)
- {
- return MathF.acosfl((a.x*b.x + a.y*b.y + a.z*b.z)/(a.Length()*b.Length()));
- }
- public static Vec3f operator *(Vec3f a, float b)
- {
- return new Vec3f(a.x * b, a.y * b, a.z * b);
- }
- public static Vec3f operator /(Vec3f a, float b)
- {
- return new Vec3f(a.x / b, a.y / b, a.z / b);
- }
- public static Vec3f operator -(Vec3f a)
- {
- return new Vec3f(-a.x, -a.y, -a.z);
- }
- public static Vec3f operator !(Vec3f a)
- {
- var l = (float)a.Length();
- return new Vec3f(a.x / l, a.y / l, a.z / l);
- }
- public override string ToString()
- {
- return string.Format("[{0:.00}, {1:.00}, {2:.00}]", x, y, z);
- }
- public unsafe static explicit operator byte[](Vec3f s)
- {
- var bts = new byte[12];
- var ps = &s;
- var btsp = (byte*)ps;
- for (var i = 0; i < 12; i++)
- bts[i] = btsp[i];
- return bts;
- }
- public unsafe static explicit operator Vec3f(byte[] bts)
- {
- fixed (byte* btsp = &bts[0])
- {
- return *(Vec3f*)btsp;
- }
- }
- public static bool operator ==(Vec3f a, Vec3f b)
- {
- return a.x == b.x && a.y == b.y && a.z == b.z;
- }
- public static bool operator !=(Vec3f a, Vec3f b)
- {
- return !(a == b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement