From c09ec3549d07a6a18ff915f0d14964a2f5606bcd Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Wed, 27 Oct 2021 07:43:31 -0400 Subject: [PATCH] convert Vector2D into value type --- MBS.Framework/Drawing/Vector.cs | 52 ++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/MBS.Framework/Drawing/Vector.cs b/MBS.Framework/Drawing/Vector.cs index 6bcd98e..36513e3 100644 --- a/MBS.Framework/Drawing/Vector.cs +++ b/MBS.Framework/Drawing/Vector.cs @@ -5,28 +5,54 @@ using System.Text; namespace MBS.Framework.Drawing { - public abstract class Vector + public struct Vector2D : IEquatable { - } - public class Vector2D - { - private double mvarX = 0.0; - public double X { get { return mvarX; } set { mvarX = value; } } - private double mvarY = 0.0; - public double Y { get { return mvarY; } set { mvarY = value; } } + private bool _IsNotEmpty; + public bool IsEmpty { get { return !_IsNotEmpty; } } + + public static Vector2D Empty = new Vector2D(); + + public double X { get; set; } + public double Y { get; set; } - public Vector2D() - { - } public Vector2D(double x, double y) { - mvarX = x; - mvarY = y; + X = x; + Y = y; + _IsNotEmpty = true; } public override string ToString() { return String.Format("({0}, {1})", X, Y); } + + #region IEquatable implementation + + public bool Equals(Vector2D other) + { + return (this.X.Equals(other.X) && this.Y.Equals(other.Y)); + } + + public static bool operator ==(Vector2D left, Vector2D right) + { + return left.Equals(right); + } + public static bool operator !=(Vector2D left, Vector2D right) + { + return !left.Equals(right); + } + public override bool Equals(object obj) + { + if (obj is Vector2D) + { + Vector2D other = (Vector2D)obj; + return X.Equals(other.X) && Y.Equals(other.Y) && IsEmpty.Equals(other.IsEmpty); + } + return base.Equals(obj); + } + + #endregion + } }