Skip to content

Commit eaee29b

Browse files
committed
update: expand vector operators
1 parent 42dece3 commit eaee29b

2 files changed

Lines changed: 157 additions & 7 deletions

File tree

src/FixedMathSharp/Numerics/Vector2d.cs

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,11 +782,25 @@ public override string ToString()
782782
/// become (X, Z) in the resulting vector, with the provided Z parameter assigned to Y.
783783
/// </returns>
784784
[MethodImpl(MethodImplOptions.AggressiveInlining)]
785-
public Vector3d ToVector3d(Fixed64 z)
785+
public readonly Vector3d ToVector3d(Fixed64 z)
786786
{
787787
return new Vector3d(x, z, y);
788788
}
789789

790+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
791+
public readonly void Deconstruct(out float x, out float y)
792+
{
793+
x = this.x.ToPreciseFloat();
794+
y = this.y.ToPreciseFloat();
795+
}
796+
797+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
798+
public readonly void Deconstruct(out int x, out int y)
799+
{
800+
x = this.x.RoundToInt();
801+
y = this.y.RoundToInt();
802+
}
803+
790804
/// <summary>
791805
/// Converts each component of the vector from radians to degrees.
792806
/// </summary>
@@ -831,6 +845,36 @@ public static Vector2d ToRadians(Vector2d degrees)
831845
return new Vector2d(v1.x + mag, v1.y + mag);
832846
}
833847

848+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
849+
public static Vector2d operator +(Fixed64 mag, Vector2d v1)
850+
{
851+
return v1 + mag;
852+
}
853+
854+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
855+
public static Vector2d operator +(Vector2d v1, (int x, int y) v2)
856+
{
857+
return new Vector2d(v1.x + v2.x, v1.y + v2.y);
858+
}
859+
860+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
861+
public static Vector2d operator +((int x, int y) v2, Vector2d v1)
862+
{
863+
return v1 + v2;
864+
}
865+
866+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
867+
public static Vector2d operator +(Vector2d v1, (float x, float y) v2)
868+
{
869+
return new Vector2d(v1.x + v2.x, v1.y + v2.y);
870+
}
871+
872+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
873+
public static Vector2d operator +((float x, float y) v1, Vector2d v2)
874+
{
875+
return v2 + v1;
876+
}
877+
834878
[MethodImpl(MethodImplOptions.AggressiveInlining)]
835879
public static Vector2d operator -(Vector2d v1, Vector2d v2)
836880
{
@@ -843,6 +887,36 @@ public static Vector2d ToRadians(Vector2d degrees)
843887
return new Vector2d(v1.x - mag, v1.y - mag);
844888
}
845889

890+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
891+
public static Vector2d operator -(Fixed64 mag, Vector2d v1)
892+
{
893+
return new Vector2d(mag - v1.x, mag - v1.y);
894+
}
895+
896+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
897+
public static Vector2d operator -(Vector2d v1, (int x, int y) v2)
898+
{
899+
return new Vector2d(v1.x - v2.x, v1.y - v2.y);
900+
}
901+
902+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
903+
public static Vector2d operator -((int x, int y) v1, Vector2d v2)
904+
{
905+
return new Vector2d(v1.x - v2.x, v1.y - v2.y);
906+
}
907+
908+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
909+
public static Vector2d operator -(Vector2d v1, (float x, float y) v2)
910+
{
911+
return new Vector2d(v1.x - v2.x, v1.y - v2.y);
912+
}
913+
914+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
915+
public static Vector2d operator -((float x, float y) v1, Vector2d v2)
916+
{
917+
return new Vector2d(v1.x - v2.x, v1.y - v2.y);
918+
}
919+
846920
[MethodImpl(MethodImplOptions.AggressiveInlining)]
847921
public static Vector2d operator -(Vector2d v1)
848922
{
@@ -900,7 +974,7 @@ public bool NotZero()
900974
}
901975

902976
[MethodImpl(MethodImplOptions.AggressiveInlining)]
903-
public override bool Equals(object obj)
977+
public override bool Equals(object? obj)
904978
{
905979
return obj is Vector2d other && Equals(other);
906980
}

src/FixedMathSharp/Numerics/Vector3d.cs

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -965,9 +965,39 @@ public static Vector3d InverseRotate(Vector3d source, Vector3d position, FixedQu
965965
}
966966

967967
[MethodImpl(MethodImplOptions.AggressiveInlining)]
968-
public static Vector3d operator +(Vector3d v1, Fixed64 add)
968+
public static Vector3d operator +(Vector3d v1, Fixed64 mag)
969969
{
970-
return new Vector3d(v1.x + add, v1.y + add, v1.z + add);
970+
return new Vector3d(v1.x + mag, v1.y + mag, v1.z + mag);
971+
}
972+
973+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
974+
public static Vector3d operator +(Fixed64 mag, Vector3d v1)
975+
{
976+
return v1 + mag;
977+
}
978+
979+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
980+
public static Vector3d operator +(Vector3d v1, (int x, int y, int z) v2)
981+
{
982+
return new Vector3d(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
983+
}
984+
985+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
986+
public static Vector3d operator +((int x, int y, int z) v2, Vector3d v1)
987+
{
988+
return v1 + v2;
989+
}
990+
991+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
992+
public static Vector3d operator +(Vector3d v1, (float x, float y, float z) v2)
993+
{
994+
return new Vector3d(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
995+
}
996+
997+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
998+
public static Vector3d operator +((float x, float y, float z) v1, Vector3d v2)
999+
{
1000+
return v2 + v1;
9711001
}
9721002

9731003
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -977,9 +1007,39 @@ public static Vector3d InverseRotate(Vector3d source, Vector3d position, FixedQu
9771007
}
9781008

9791009
[MethodImpl(MethodImplOptions.AggressiveInlining)]
980-
public static Vector3d operator -(Vector3d v1, Fixed64 sub)
1010+
public static Vector3d operator -(Vector3d v1, Fixed64 mag)
1011+
{
1012+
return new Vector3d(v1.x - mag, v1.y - mag, v1.z - mag);
1013+
}
1014+
1015+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1016+
public static Vector3d operator -(Fixed64 mag, Vector3d v1)
1017+
{
1018+
return new Vector3d(mag - v1.x, mag - v1.y, mag - v1.z);
1019+
}
1020+
1021+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1022+
public static Vector3d operator -(Vector3d v1, (int x, int y, int z) v2)
1023+
{
1024+
return new Vector3d(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
1025+
}
1026+
1027+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1028+
public static Vector3d operator -((int x, int y, int z) v1, Vector3d v2)
9811029
{
982-
return new Vector3d(v1.x - sub, v1.y - sub, v1.z - sub);
1030+
return new Vector3d(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
1031+
}
1032+
1033+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1034+
public static Vector3d operator -(Vector3d v1, (float x, float y, float z) v2)
1035+
{
1036+
return new Vector3d(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
1037+
}
1038+
1039+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1040+
public static Vector3d operator -((float x, float y, float z) v1, Vector3d v2)
1041+
{
1042+
return new Vector3d(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
9831043
}
9841044

9851045
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -1187,6 +1247,22 @@ public Vector2d ToVector2d()
11871247
return new Vector2d(x, z);
11881248
}
11891249

1250+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1251+
public readonly void Deconstruct(out float x, out float y, out float z)
1252+
{
1253+
x = this.x.ToPreciseFloat();
1254+
y = this.y.ToPreciseFloat();
1255+
z = this.z.ToPreciseFloat();
1256+
}
1257+
1258+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1259+
public readonly void Deconstruct(out int x, out int y, out int z)
1260+
{
1261+
x = this.x.RoundToInt();
1262+
y = this.y.RoundToInt();
1263+
z = this.z.RoundToInt();
1264+
}
1265+
11901266
/// <summary>
11911267
/// Converts each component of the vector from radians to degrees.
11921268
/// </summary>
@@ -1220,7 +1296,7 @@ public static Vector3d ToRadians(Vector3d degrees)
12201296
#region Equality and HashCode Overrides
12211297

12221298
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1223-
public override bool Equals(object obj)
1299+
public override bool Equals(object? obj)
12241300
{
12251301
return obj is Vector3d other && Equals(other);
12261302
}

0 commit comments

Comments
 (0)