Skip to content

Instantly share code, notes, and snippets.

@nathan130200
Created February 7, 2021 22:20
Show Gist options
  • Save nathan130200/e3bf88ed27b97fffebf1440de7664249 to your computer and use it in GitHub Desktop.
Save nathan130200/e3bf88ed27b97fffebf1440de7664249 to your computer and use it in GitHub Desktop.
C# - Generic Vector3.
public class Vec3<T>
{
public T X { get; set; }
public T Y { get; set; }
public T Z { get; set; }
public Vec3()
{
this.X = default;
this.Y = default;
this.Z = default;
}
public Vec3(T v)
{
this.X = v;
this.Y = v;
this.Z = v;
}
public Vec3(T x, T y, T z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public static Vec3<T> operator +(Vec3<T> a, T v)
{
dynamic x1 = a.X, y1 = a.Y, z1 = a.Z;
return new Vec3<T>
{
X = x1 + v,
Y = y1 + v,
Z = z1 + v
};
}
public static Vec3<T> operator +(Vec3<T> a, Vec3<T> b)
{
dynamic x1 = a.X, y1 = a.Y, z1 = a.Z;
dynamic x2 = b.X, y2 = b.Y, z2 = b.Z;
return new Vec3<T>
{
X = x1 + x2,
Y = y1 + y2,
Z = z1 + z2
};
}
public static Vec3<T> operator -(Vec3<T> a, T v)
{
dynamic x1 = a.X, y1 = a.Y, z1 = a.Z;
return new Vec3<T>
{
X = x1 - v,
Y = y1 - v,
Z = z1 - v
};
}
public static Vec3<T> operator -(Vec3<T> a, Vec3<T> b)
{
dynamic x1 = a.X, y1 = a.Y, z1 = a.Z;
dynamic x2 = b.X, y2 = b.Y, z2 = b.Z;
return new Vec3<T>
{
X = x1 - x2,
Y = y1 - y2,
Z = z1 - z2
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment