Common
A library of common classes.
Vector3DInt.cs
1using System;
2using System.Runtime.Serialization;
3
5{
9 [DataContract(Namespace = Constants.DataContractNamespace)]
10 public struct Vector3DInt : IEquatable<Vector3DInt>, IComparable<Vector3DInt>
11 {
15 public static Vector3DInt Zero => new Vector3DInt();
16
20 public static Vector3DInt One => new Vector3DInt(1, 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
37 [DataMember(Order = 2)]
38 public int Z { get; private set; }
39
46 public Vector3DInt(int x, int y, int z)
47 {
48 X = x;
49 Y = y;
50 Z = z;
51 }
52
56 public Vector2DInt To2D() => new Vector2DInt(X, Y);
57
59 public override string ToString()
60 {
61 return $"Vector3DInt({X}, {Y}, {Z})";
62 }
63
65 public override bool Equals(object obj)
66 {
67 return obj is Vector3DInt vector && Equals(vector);
68 }
69
71 public bool Equals(Vector3DInt other)
72 {
73 return X == other.X &&
74 Y == other.Y &&
75 Z == other.Z;
76 }
77
79 public int CompareTo(Vector3DInt other)
80 {
81 var comparison = X.CompareTo(other.X);
82
83 if (comparison != 0)
84 return comparison;
85
86 comparison = Y.CompareTo(other.Y);
87
88 if (comparison != 0)
89 return comparison;
90
91 return Z.CompareTo(other.Z);
92 }
93
95 public override int GetHashCode()
96 {
97 int hashCode = -307843816;
98 hashCode = hashCode * -1521134295 + X.GetHashCode();
99 hashCode = hashCode * -1521134295 + Y.GetHashCode();
100 hashCode = hashCode * -1521134295 + Z.GetHashCode();
101 return hashCode;
102 }
103
105 public static bool operator ==(Vector3DInt left, Vector3DInt right)
106 {
107 return left.Equals(right);
108 }
109
111 public static bool operator !=(Vector3DInt left, Vector3DInt right)
112 {
113 return !(left == right);
114 }
115
118 {
119 return new Vector3DInt(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
120 }
121
124 {
125 return new Vector3DInt(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
126 }
127
129 public static Vector3DInt operator -(Vector3DInt vector)
130 {
131 return new Vector3DInt(-vector.X, -vector.Y, -vector.Z);
132 }
133
139 public static Vector3DInt Max(Vector3DInt value1, Vector3DInt value2)
140 {
141 return new Vector3DInt(Math.Max(value1.X, value2.X), Math.Max(value1.Y, value2.Y), Math.Max(value1.Z, value2.Z));
142 }
143
149 public static Vector3DInt Min(Vector3DInt value1, Vector3DInt value2)
150 {
151 return new Vector3DInt(Math.Min(value1.X, value2.X), Math.Min(value1.Y, value2.Y), Math.Min(value1.Z, value2.Z));
152 }
153
158 public static Vector3DInt Sign(Vector3DInt vector)
159 {
160 return new Vector3DInt(Math.Sign(vector.X), Math.Sign(vector.Y), Math.Sign(vector.Z));
161 }
162
168 public static int Dot(Vector3DInt value1, Vector3DInt value2)
169 {
170 return value1.X * value2.X + value1.Y * value2.Y + value1.Z * value2.Z;
171 }
172 }
173}
A 2D vector with integer values.
Definition: Vector2DInt.cs:11
A 3D vector with integer values.
Definition: Vector3DInt.cs:11
static Vector3DInt Min(Vector3DInt value1, Vector3DInt value2)
Returns the minimum values of the two vectors.
Definition: Vector3DInt.cs:149
static int Dot(Vector3DInt value1, Vector3DInt value2)
Returns the dot product of the two vectors.
Definition: Vector3DInt.cs:168
Vector3DInt(int x, int y, int z)
Initializes a new vector.
Definition: Vector3DInt.cs:46
static bool operator!=(Vector3DInt left, Vector3DInt right)
Definition: Vector3DInt.cs:111
static bool operator==(Vector3DInt left, Vector3DInt right)
Definition: Vector3DInt.cs:105
static Vector3DInt operator-(Vector3DInt left, Vector3DInt right)
Definition: Vector3DInt.cs:123
bool Equals(Vector3DInt other)
Definition: Vector3DInt.cs:71
static Vector3DInt Zero
Returns a zero vector.
Definition: Vector3DInt.cs:15
override bool Equals(object obj)
Definition: Vector3DInt.cs:65
static Vector3DInt One
Returns a vector of ones.
Definition: Vector3DInt.cs:20
static Vector3DInt Max(Vector3DInt value1, Vector3DInt value2)
Returns the maximum values of the two vectors.
Definition: Vector3DInt.cs:139
static Vector3DInt operator+(Vector3DInt left, Vector3DInt right)
Definition: Vector3DInt.cs:117
static Vector3DInt Sign(Vector3DInt vector)
Returns the sign of the vector.
Definition: Vector3DInt.cs:158
Vector2DInt To2D()
Converts the vector to a 2D vector, without the Z value.
int CompareTo(Vector3DInt other)
Definition: Vector3DInt.cs:79