Skip to content

Instantly share code, notes, and snippets.

@sbrl
Last active March 20, 2018 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrl/589aac9044145cc41a008ca474758c7e to your computer and use it in GitHub Desktop.
Save sbrl/589aac9044145cc41a008ca474758c7e to your computer and use it in GitHub Desktop.
[Vector2.cs] A half-port of my particularly useful Vector.js to C♯. Doesn't currently implement all the methods that Vector.js has, but I'll add them as I need them. #microlibrary
using System;
namespace SBRL.IO.Svg
{
/// <summary>
/// Represents a single point in 2D space.
/// May also be used to represent a direction with a magnitude.
/// </summary>
/// <version>v0.4.2</version>
/// <changelog>
/// v0.1 - 1st April 2017
/// - Added this changelog
/// v0.2 - 10th March 2018
/// - Fixed terribible bug in Add + Subtract methods
/// v0.3 - 10th March 2018
/// - Upgraded from ints to floats
/// - Added Multiply(Vector2)
/// - Added ToString() override
/// v0.4 - 17th March 2018
/// - Add operator overloading! :D
/// v0.4.1 - 18th March 2018
/// - Port static FromBearing() from Vector.js
/// - Add Length read-only property
/// v0.4.2 - 19th March 2018
/// - Add AspectRatio read-only property
/// </changelog>
public struct Vector2
{
/// <summary>
/// A Vector2 with all it's properties initialised to zero.
/// </summary>
public static Vector2 Zero = new Vector2() { X = 0, Y = 0 };
/// <summary>
/// The X coordinate.
/// </summary>
public float X { get; set; }
/// <summary>
/// The Y coordinate.
/// </summary>
public float Y { get; set; }
/// <summary>
/// The length of this vector.
/// </summary>
public float Length => (float)Math.Sqrt(X * X + Y * Y);
/// <summary>
/// The aspect ratio of this vector.
/// Useful when a Vector2 is being used to represent the size of something.
/// </summary>
public float AspectRatio => X / Y;
public Vector2(float x, float y)
{
X = x;
Y = y;
}
public Vector2 Add(Vector2 b)
{
return new Vector2(
X + b.X,
Y + b.Y
);
}
public Vector2 Subtract(Vector2 b)
{
return new Vector2(
X - b.X,
Y - b.Y
);
}
public Vector2 Divide(float b)
{
return new Vector2(
X / b,
Y / b
);
}
public Vector2 Multiply(float b)
{
return new Vector2(
X * b,
Y * b
);
}
public Vector2 Multiply(Vector2 b)
{
return new Vector2(
X * b.X,
Y * b.Y
);
}
#region Operator overloads
public static Vector2 operator+ (Vector2 a, Vector2 b)
{
return a.Add(b);
}
public static Vector2 operator- (Vector2 a, Vector2 b)
{
return a.Subtract(b);
}
public static Vector2 operator* (Vector2 a, Vector2 b)
{
return a.Multiply(b);
}
public static Vector2 operator +(Vector2 a, float b)
{
return a.Multiply(b);
}
public static Vector2 operator/ (Vector2 a, float b)
{
return a.Divide(b);
}
#endregion
public override string ToString()
{
return string.Format("({0}, {1})", X, Y);
}
#region Static Methods
/// <summary>
/// Returns a new vector based on an angle and a length.
/// </summary>
/// <param name="angle">The angle, in radians.</param>
/// <param name="length">The length.</param>
/// <returns>A new vector that represents the (x, y) of the specified angle and length.</returns>
public static Vector2 FromBearing(float angle, float length) {
return new Vector2(
(float)(length * Math.Cos(angle)),
(float)(length * Math.Sin(angle))
);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment