Common
A library of common classes.
Vector2DInt.cs
1using System;
2using System.Runtime.Serialization;
3
5{
9 [DataContract(Namespace = Constants.DataContractNamespace)]
10 public struct Vector2DInt : IEquatable<Vector2DInt>, IComparable<Vector2DInt>
11 {
15 public static Vector2DInt Zero => new Vector2DInt();
16
20 public static Vector2DInt One => new Vector2DInt(1, 1);
21
25 [DataMember(Order = 0)]
26 public int X { get; private set; }
27
31 [DataMember(Order = 1)]
32 public int Y { get; private set; }
33
39 public Vector2DInt(int x, int y)
40 {
41 X = x;
42 Y = y;
43 }
44
48 public Vector3DInt To3D() => new Vector3DInt(X, Y, 0);
49
51 public override string ToString()
52 {
53 return $"Vector2DInt({X}, {Y})";
54 }
55
57 public override bool Equals(object obj)
58 {
59 return obj is Vector2DInt vector && Equals(vector);
60 }
61
63 public bool Equals(Vector2DInt other)
64 {
65 return X == other.X &&
66 Y == other.Y;
67 }
68
70 public int CompareTo(Vector2DInt other)
71 {
72 var comparison = X.CompareTo(other.X);
73
74 if (comparison != 0)
75 return comparison;
76
77 return Y.CompareTo(other.Y);
78 }
79
81 public override int GetHashCode()
82 {
83 int hashCode = 1861411795;
84 hashCode = hashCode * -1521134295 + X.GetHashCode();
85 hashCode = hashCode * -1521134295 + Y.GetHashCode();
86 return hashCode;
87 }
88
90 public static bool operator ==(Vector2DInt left, Vector2DInt right)
91 {
92 return left.Equals(right);
93 }
94
96 public static bool operator !=(Vector2DInt left, Vector2DInt right)
97 {
98 return !(left == right);
99 }
100
103 {
104 return new Vector2DInt(left.X + right.X, left.Y + right.Y);
105 }
106
109 {
110 return new Vector2DInt(left.X - right.X, left.Y - right.Y);
111 }
112
114 public static Vector2DInt operator -(Vector2DInt vector)
115 {
116 return new Vector2DInt(-vector.X, -vector.Y);
117 }
118
124 public static Vector2DInt Max(Vector2DInt value1, Vector2DInt value2)
125 {
126 return new Vector2DInt(Math.Max(value1.X, value2.X), Math.Max(value1.Y, value2.Y));
127 }
128
134 public static Vector2DInt Min(Vector2DInt value1, Vector2DInt value2)
135 {
136 return new Vector2DInt(Math.Min(value1.X, value2.X), Math.Min(value1.Y, value2.Y));
137 }
138
143 public static Vector2DInt Sign(Vector2DInt vector)
144 {
145 return new Vector2DInt(Math.Sign(vector.X), Math.Sign(vector.Y));
146 }
147
153 public static int Dot(Vector2DInt value1, Vector2DInt value2)
154 {
155 return value1.X * value2.X + value1.Y * value2.Y;
156 }
157 }
158}
A 2D vector with integer values.
Definition: Vector2DInt.cs:11
Vector3DInt To3D()
Converts the vector to a 3D vector with a Z value of zero.
static Vector2DInt operator-(Vector2DInt left, Vector2DInt right)
Definition: Vector2DInt.cs:108
static Vector2DInt operator+(Vector2DInt left, Vector2DInt right)
Definition: Vector2DInt.cs:102
static Vector2DInt One
Returns a vector of ones.
Definition: Vector2DInt.cs:20
static Vector2DInt Min(Vector2DInt value1, Vector2DInt value2)
Returns the minimum values of the two vectors.
Definition: Vector2DInt.cs:134
Vector2DInt(int x, int y)
Initializes a new vector.
Definition: Vector2DInt.cs:39
bool Equals(Vector2DInt other)
Definition: Vector2DInt.cs:63
static bool operator==(Vector2DInt left, Vector2DInt right)
Definition: Vector2DInt.cs:90
int CompareTo(Vector2DInt other)
Definition: Vector2DInt.cs:70
static bool operator!=(Vector2DInt left, Vector2DInt right)
Definition: Vector2DInt.cs:96
override bool Equals(object obj)
Definition: Vector2DInt.cs:57
static Vector2DInt Zero
Returns a zero vector.
Definition: Vector2DInt.cs:15
static Vector2DInt Max(Vector2DInt value1, Vector2DInt value2)
Returns the maximum values of the two vectors.
Definition: Vector2DInt.cs:124
static Vector2DInt Sign(Vector2DInt vector)
Returns the sign of the vector.
Definition: Vector2DInt.cs:143
static int Dot(Vector2DInt value1, Vector2DInt value2)
Returns the dot product of the two vectors.
Definition: Vector2DInt.cs:153
A 3D vector with integer values.
Definition: Vector3DInt.cs:11