| | | 1 | | using ReFlex.Core.Common.Interfaces; |
| | | 2 | | |
| | | 3 | | namespace ReFlex.Core.Common.Components |
| | | 4 | | { |
| | | 5 | | /// <inheritdoc /> |
| | | 6 | | /// <summary> |
| | | 7 | | /// A twodimensional vector. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <seealso cref="IBase2" /> |
| | | 10 | | public class Vector2 : IBase2 |
| | | 11 | | { |
| | | 12 | | /// <inheritdoc /> |
| | | 13 | | /// <summary> |
| | | 14 | | /// Gets or sets the x value. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <value> |
| | | 17 | | /// The x value. |
| | | 18 | | /// </value> |
| | 6 | 19 | | public float X { get; set; } |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets or sets the y value. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <value> |
| | | 26 | | /// The y value. |
| | | 27 | | /// </value> |
| | 6 | 28 | | public float Y { get; set; } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | /// <summary> |
| | | 32 | | /// Returns true if ... is valid. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <value> |
| | | 35 | | /// <c>true</c> if this point is valid; otherwise, <c>false</c>. |
| | | 36 | | /// </value> |
| | 3 | 37 | | public bool IsValid { get; set; } = true; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Initializes a new instance of the <see cref="Vector2"/> class. |
| | | 41 | | /// </summary> |
| | 0 | 42 | | public Vector2() => Set(0, 0); |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new instance of the <see cref="Vector2"/> class. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="x">The x.</param> |
| | | 48 | | /// <param name="y">The y.</param> |
| | 6 | 49 | | public Vector2(float x, float y) => Set(x, y); |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Initializes a new instance of the <see cref="Vector2"/> class. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="base2">The base2.</param> |
| | 0 | 55 | | public Vector2(IBase2 base2) => Set(base2); |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | /// <summary> |
| | | 59 | | /// Copies the values from the input to [this] instance. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="base2">The base2.</param> |
| | 0 | 62 | | public void Set(IBase2 base2) => Set(base2.X, base2.Y); |
| | | 63 | | |
| | | 64 | | /// <inheritdoc /> |
| | | 65 | | /// <summary> |
| | | 66 | | /// Creates a copy of [this]. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <returns></returns> |
| | 0 | 69 | | public IBase2 Copy() => new Vector2(this); |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the length. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <value> |
| | | 75 | | /// The length. |
| | | 76 | | /// </value> |
| | 0 | 77 | | public float Length => (float)System.Math.Sqrt(X * X + Y * Y); |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets the normalized copy. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <value> |
| | | 83 | | /// The normalized copy. |
| | | 84 | | /// </value> |
| | 0 | 85 | | public Vector2 NormalizedCopy => Length > 0 ? new Vector2(X / Length, Y / Length) : new Vector2(); |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Adds the specified vector. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="vector">The vector.</param> |
| | | 91 | | /// <returns></returns> |
| | 0 | 92 | | public Vector2 Add(Vector2 vector) => new Vector2(X + vector.X, Y + vector.Y); |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Multiplies this vector with the specified vector. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="vector">The vector.</param> |
| | | 98 | | /// <returns></returns> |
| | 0 | 99 | | public float Multiply(Vector2 vector) => X * vector.X + Y * vector.Y; |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Multiplies the specified value to this vector. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="value">The value.</param> |
| | | 105 | | public Vector2 Multiply(float value) |
| | 0 | 106 | | { |
| | 0 | 107 | | X *= value; |
| | 0 | 108 | | Y *= value; |
| | 0 | 109 | | return this; |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Calculates the angle between this vector and a specified vector. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="vector">The vector.</param> |
| | | 116 | | /// <returns></returns> |
| | 0 | 117 | | public float AngleBetween(Vector2 vector) => Multiply(vector) / (Length * vector.Length); |
| | | 118 | | |
| | | 119 | | /// <inheritdoc /> |
| | | 120 | | /// <summary> |
| | | 121 | | /// Sets the x and y value. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="x">The x value.</param> |
| | | 124 | | /// <param name="y">The y value.</param> |
| | | 125 | | public void Set(float x, float y) |
| | 3 | 126 | | { |
| | 3 | 127 | | X = x; |
| | 3 | 128 | | Y = y; |
| | 3 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <inheritdoc /> |
| | | 132 | | /// <summary> |
| | | 133 | | /// Equalses the specified base2. |
| | | 134 | | /// </summary> |
| | | 135 | | /// <param name="base2">The base2.</param> |
| | | 136 | | /// <returns></returns> |
| | 0 | 137 | | public bool Equals(IBase2 base2) => base2 != null && Equals(X, base2.X) && Equals(Y, base2.Y); |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Returns a <see cref="string" /> that represents [this] instance. |
| | | 141 | | /// </summary> |
| | | 142 | | /// <returns> |
| | | 143 | | /// A <see cref="string" /> that represents [this] instance. |
| | | 144 | | /// </returns> |
| | 0 | 145 | | public override string ToString() => IsValid ? $"X: {X}; Y: {Y}" : "invalid"; |
| | | 146 | | } |
| | | 147 | | } |