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