Last active
August 9, 2018 20:01
-
-
Save matthewjberger/53b3e33d4285f5941a3f7a1655c53857 to your computer and use it in GitHub Desktop.
A C# 2D Vector class that can be bound to from a ViewModel and has rectangular and polar representations. (Uses MVVMLightLibs from nuget)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Vector2Example | |
{ | |
using System; | |
using GalaSoft.MvvmLight; | |
public class Angle : ObservableObject | |
{ | |
private Angle() { } | |
public static Angle FromRadians(double radians) | |
{ | |
return new Angle {Radians = radians}; | |
} | |
public static Angle FromDegrees(double degrees) | |
{ | |
return new Angle {Degrees = degrees}; | |
} | |
private double _radians; | |
public double Radians | |
{ | |
get { return _radians; } | |
set | |
{ | |
if (!Set(() => Radians, ref _radians, value % (2*Math.PI))) return; | |
RaisePropertyChanged(nameof(Degrees)); | |
} | |
} | |
public double Degrees | |
{ | |
get { return _radians * 180/Math.PI; } | |
set { Set(() => Radians, ref _radians, value * Math.PI/180); } | |
} | |
public static Angle operator +(Angle leftHandSide, Angle rightHandSide) | |
{ | |
return FromRadians(leftHandSide.Radians + rightHandSide.Radians); | |
} | |
public static Angle operator -(Angle leftHandSide, Angle rightHandSide) | |
{ | |
return FromRadians(leftHandSide.Radians - rightHandSide.Radians); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Vector2Example | |
{ | |
using System; | |
using GalaSoft.MvvmLight; | |
public class Vector2 : ObservableObject | |
{ | |
private Vector2(){} | |
public static Vector2 FromRectangular(double x, double y) | |
{ | |
return new Vector2 { X = x, Y = y }; | |
} | |
public static Vector2 FromPolar(Angle angle, double magnitude) | |
{ | |
return new Vector2 { Angle = angle, Magnitude = magnitude }; | |
} | |
private void RecalculatePolar() | |
{ | |
Magnitude = Math.Sqrt(Math.Pow(X, 2) + Math.Pow(Y, 2)); | |
Angle.Radians = Math.Atan2(Y, X); | |
Angle.Radians += Angle.Radians < 0 ? 2 * Math.PI : 0; | |
} | |
private void RecalculateRectangular() | |
{ | |
X = Magnitude * Math.Cos(Angle.Radians); | |
Y = Magnitude * Math.Sin(Angle.Radians); | |
} | |
// Rectangular Representation | |
private double _x; | |
public double X | |
{ | |
get { return _x; } | |
set | |
{ | |
if (!Set(() => X, ref _x, value)) return; | |
RecalculatePolar(); | |
} | |
} | |
private double _y; | |
public double Y | |
{ | |
get { return _y; } | |
set | |
{ | |
if (!Set(() => Y, ref _y, value)) return; | |
RecalculatePolar(); | |
} | |
} | |
// Polar Representation | |
private Angle _angle; | |
public Angle Angle | |
{ | |
get { return _angle; } | |
set | |
{ | |
if (!Set(() => Angle, ref _angle, value)) return; | |
RecalculateRectangular(); | |
} | |
} | |
private double _magnitude; | |
public double Magnitude | |
{ | |
get { return _magnitude; } | |
set | |
{ | |
if (!Set(() => Magnitude, ref _magnitude, value)) return; | |
RecalculateRectangular(); | |
} | |
} | |
public static Vector2 operator +(Vector2 leftHandSide, Vector2 rightHandSide) | |
{ | |
return FromRectangular(leftHandSide.X + rightHandSide.X, leftHandSide.Y + rightHandSide.Y); | |
} | |
public static Vector2 operator -(Vector2 leftHandSide, Vector2 rightHandSide) | |
{ | |
return FromRectangular(leftHandSide.X - rightHandSide.X, leftHandSide.Y - rightHandSide.Y); | |
} | |
public double Dot(Vector2 rightHandSide) | |
{ | |
return X * rightHandSide.X + Y * rightHandSide.Y; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment