Skip to content

Instantly share code, notes, and snippets.

@untodesu
Last active November 6, 2019 14:26
Show Gist options
  • Save untodesu/b10025d4158e8b3dcf174cce700c0353 to your computer and use it in GitHub Desktop.
Save untodesu/b10025d4158e8b3dcf174cce700c0353 to your computer and use it in GitHub Desktop.
Vectors.
using System;
namespace Aux2D.Shared.MathTypes
{
/// <summary>
/// 2-Dimensional vector.
/// </summary>
class Vector2D
{
/// <summary>
/// X value
/// </summary>
public double X { get; set; }
/// <summary>
/// Y value
/// </summary>
public double Y { get; set; }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="x">X default value</param>
/// <param name="y">Y default value</param>
public Vector2D(double x = 0.0, double y = 0.0)
{
X = x;
Y = y;
}
///==================================
/// Operators overloading
///==================================
public static Vector2D operator +(Vector2D vec1, Vector2D vec2)
=> new Vector2D(vec1.X + vec2.X, vec1.Y + vec2.Y);
public static Vector2D operator -(Vector2D vec1, Vector2D vec2)
=> new Vector2D(vec1.X - vec2.X, vec1.Y - vec2.Y);
public static Vector2D operator *(Vector2D vec1, Vector2D vec2)
=> new Vector2D(vec1.X * vec2.X, vec1.Y * vec2.Y);
public static Vector2D operator /(Vector2D vec1, Vector2D vec2)
=> new Vector2D(vec1.X / vec2.X, vec1.Y / vec2.Y);
public static Vector2D operator %(Vector2D vec1, Vector2D vec2)
=> new Vector2D(vec1.X % vec2.X, vec1.Y % vec2.Y);
public static Vector2D operator +(Vector2D vec, double value)
=> new Vector2D(vec.X + value, vec.Y + value);
public static Vector2D operator -(Vector2D vec, double value)
=> new Vector2D(vec.X - value, vec.Y - value);
public static Vector2D operator *(Vector2D vec, double value)
=> new Vector2D(vec.X * value, vec.Y * value);
public static Vector2D operator /(Vector2D vec, double value)
=> new Vector2D(vec.X / value, vec.Y / value);
public static Vector2D operator %(Vector2D vec, double value)
=> new Vector2D(vec.X % value, vec.Y % value);
///==================================
/// Vector math
/// =================================
/// <summary>
/// Get Squared length.
/// </summary>
public double LengthSquared()
=> Math.Pow(X, 2) + Math.Pow(Y, 2);
/// <summary>
/// Get vector length
/// </summary>
public double Length()
=> Math.Sqrt(LengthSquared());
/// <summary>
/// Get normalized vector (Length == 1)
/// </summary>
public Vector2D Normalize()
=> this / Length();
/// <summary>
/// Scalar product of two vectors
/// </summary>
/// <param name="vec">Vector.</param>
public double DotProduct(Vector2D vec)
=> (vec.X * X + vec.Y * Y);
/// <summary>
/// Vector length * vector length
/// </summary>
/// <param name="vec">The vector.</param>
public static double LengthSquared(Vector2D vec)
=> vec.LengthSquared();
/// <summary>
/// Vector length.
/// </summary>
/// <param name="vec">The vector</param>
public static double Length(Vector2D vec)
=> vec.Length();
/// <summary>
/// Normalize vector (Length := 1)
/// </summary>
/// <param name="vec">The vector</param>
public static Vector2D Normalize(Vector2D vec)
=> vec.Normalize();
/// <summary>
/// Scalar product of two vectors
/// </summary>
public static double DotProduct(Vector2D vec1, Vector2D vec2)
=> vec1.DotProduct(vec2);
}
}
using System;
namespace Aux2D.Shared.MathTypes
{
/// <summary>
/// 3-Dimensional vector.
/// </summary>
class Vector3D
{
/// <summary>
/// X value
/// </summary>
public double X { get; set; }
/// <summary>
/// Y value
/// </summary>
public double Y { get; set; }
/// <summary>
/// Z value
/// </summary>
public double Z { get; set; }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="x">X default value</param>
/// <param name="y">Y default value</param>
/// <param name="z">Z default value</param>
public Vector3D(double x = 0.0, double y = 0.0, double z = 0.0)
{
X = x;
Y = y;
Z = z;
}
///==================================
/// Operators overloading
///==================================
public static Vector3D operator +(Vector3D vec1, Vector3D vec2)
=> new Vector3D(vec1.X + vec2.X, vec1.Y + vec2.Y, vec1.Z + vec2.Z);
public static Vector3D operator -(Vector3D vec1, Vector3D vec2)
=> new Vector3D(vec1.X - vec2.X, vec1.Y - vec2.Y, vec1.Z - vec2.Z);
public static Vector3D operator *(Vector3D vec1, Vector3D vec2)
=> new Vector3D(vec1.X * vec2.X, vec1.Y * vec2.Y, vec1.Z * vec2.Z);
public static Vector3D operator /(Vector3D vec1, Vector3D vec2)
=> new Vector3D(vec1.X / vec2.X, vec1.Y / vec2.Y, vec1.Z / vec2.Z);
public static Vector3D operator %(Vector3D vec1, Vector3D vec2)
=> new Vector3D(vec1.X % vec2.X, vec1.Y % vec2.Y, vec1.Z % vec2.Z);
public static Vector3D operator +(Vector3D vec, double value)
=> new Vector3D(vec.X + value, vec.Y + value, vec.Z + value);
public static Vector3D operator -(Vector3D vec, double value)
=> new Vector3D(vec.X - value, vec.Y - value, vec.Z - value);
public static Vector3D operator *(Vector3D vec, double value)
=> new Vector3D(vec.X * value, vec.Y * value, vec.Z * value);
public static Vector3D operator /(Vector3D vec, double value)
=> new Vector3D(vec.X / value, vec.Y / value, vec.Z / value);
public static Vector3D operator %(Vector3D vec, double value)
=> new Vector3D(vec.X % value, vec.Y % value, vec.Z % value);
///==================================
/// Vector math
/// =================================
/// <summary>
/// Get Squared length.
/// </summary>
public double LengthSquared()
=> Math.Pow(X, 2) + Math.Pow(Y, 2) + Math.Pow(Z, 2);
/// <summary>
/// Get vector length
/// </summary>
public double Length()
=> Math.Sqrt(LengthSquared());
/// <summary>
/// Get normalized vector (Length == 1)
/// </summary>
public Vector3D Normalize()
=> this / Length();
/// <summary>
/// Scalar product of two vectors
/// </summary>
/// <param name="vec">Vector.</param>
public double DotProduct(Vector3D vec)
=> (vec.X * X + vec.Y * Y + vec.Z * Z);
/// <summary>
/// Vector length * vector length
/// </summary>
/// <param name="vec">The vector.</param>
public static double LengthSquared(Vector3D vec)
=> vec.LengthSquared();
/// <summary>
/// Vector length.
/// </summary>
/// <param name="vec">The vector</param>
public static double Length(Vector3D vec)
=> vec.Length();
/// <summary>
/// Normalize vector (Length := 1)
/// </summary>
/// <param name="vec">The vector</param>
public static Vector3D Normalize(Vector3D vec)
=> vec.Normalize();
/// <summary>
/// Scalar product of two vectors
/// </summary>
public static double DotProduct(Vector3D vec1, Vector3D vec2)
=> vec1.DotProduct(vec2);
}
}
using System;
namespace Aux2D.Shared.MathTypes
{
/// <summary>
/// 4-Dimensional vector.
/// </summary>
class Vector4D
{
/// <summary>
/// X value
/// </summary>
public double X { get; set; }
/// <summary>
/// Y value
/// </summary>
public double Y { get; set; }
/// <summary>
/// Z value
/// </summary>
public double Z { get; set; }
/// <summary>
/// W value
/// </summary>
public double W { get; set; }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="x">X default value</param>
/// <param name="y">Y default value</param>
/// <param name="z">Z default value</param>
/// <param name="w">W default value</param>
public Vector4D(double x = 0.0, double y = 0.0, double z = 0.0, double w = 0.0)
{
X = x;
Y = y;
Z = z;
W = w;
}
///==================================
/// Operators overloading
///==================================
public static Vector4D operator +(Vector4D vec1, Vector4D vec2)
=> new Vector4D(vec1.X + vec2.X, vec1.Y + vec2.Y, vec1.Z + vec2.Z, vec1.W + vec2.W);
public static Vector4D operator -(Vector4D vec1, Vector4D vec2)
=> new Vector4D(vec1.X - vec2.X, vec1.Y - vec2.Y, vec1.Z - vec2.Z, vec1.W - vec2.W);
public static Vector4D operator *(Vector4D vec1, Vector4D vec2)
=> new Vector4D(vec1.X * vec2.X, vec1.Y * vec2.Y, vec1.Z * vec2.Z, vec1.W * vec2.W);
public static Vector4D operator /(Vector4D vec1, Vector4D vec2)
=> new Vector4D(vec1.X / vec2.X, vec1.Y / vec2.Y, vec1.Z / vec2.Z, vec1.W / vec2.W);
public static Vector4D operator %(Vector4D vec1, Vector4D vec2)
=> new Vector4D(vec1.X % vec2.X, vec1.Y % vec2.Y, vec1.Z % vec2.Z, vec1.W % vec2.W);
public static Vector4D operator +(Vector4D vec, double value)
=> new Vector4D(vec.X + value, vec.Y + value, vec.Z + value, vec.W + value);
public static Vector4D operator -(Vector4D vec, double value)
=> new Vector4D(vec.X - value, vec.Y - value, vec.Z - value, vec.W - value);
public static Vector4D operator *(Vector4D vec, double value)
=> new Vector4D(vec.X * value, vec.Y * value, vec.Z * value, vec.W * value);
public static Vector4D operator /(Vector4D vec, double value)
=> new Vector4D(vec.X / value, vec.Y / value, vec.Z / value, vec.W / value);
public static Vector4D operator %(Vector4D vec, double value)
=> new Vector4D(vec.X % value, vec.Y % value, vec.Z % value, vec.W % value);
///==================================
/// Vector math
/// =================================
/// <summary>
/// Get Squared length.
/// </summary>
public double LengthSquared()
=> Math.Pow(X, 2) + Math.Pow(Y, 2) + Math.Pow(Z, 2) + Math.Pow(W, 2);
/// <summary>
/// Get vector length
/// </summary>
public double Length()
=> Math.Sqrt(LengthSquared());
/// <summary>
/// Get normalized vector (Length == 1)
/// </summary>
public Vector4D Normalize()
=> this / Length();
/// <summary>
/// Scalar product of two vectors
/// </summary>
/// <param name="vec">Vector.</param>
public double DotProduct(Vector4D vec)
=> (vec.X * X + vec.Y * Y + vec.Z * Z + vec.W * W);
/// <summary>
/// Vector length * vector length
/// </summary>
/// <param name="vec">The vector.</param>
public static double LengthSquared(Vector4D vec)
=> vec.LengthSquared();
/// <summary>
/// Vector length.
/// </summary>
/// <param name="vec">The vector</param>
public static double Length(Vector4D vec)
=> vec.Length();
/// <summary>
/// Normalize vector (Length := 1)
/// </summary>
/// <param name="vec">The vector</param>
public static Vector4D Normalize(Vector4D vec)
=> vec.Normalize();
/// <summary>
/// Scalar product of two vectors
/// </summary>
public static double DotProduct(Vector4D vec1, Vector4D vec2)
=> vec1.DotProduct(vec2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment