forked from XIV-Tools/CustomizePlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.cs
122 lines (99 loc) · 2.86 KB
/
Vector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// © Anamnesis.
// Developed by W and A Walsh.
// Licensed under the MIT license.
namespace Anamnesis.Memory
{
using System;
using System.Globalization;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct Vector : IEquatable<object>
{
public static readonly Vector Zero = new Vector(0, 0, 0);
public static readonly Vector One = new Vector(1, 1, 1);
public Vector(float x = 0, float y = 0, float z = 0)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
public static bool operator !=(Vector lhs, Vector rhs)
{
return !lhs.Equals(rhs);
}
public static bool operator ==(Vector lhs, Vector rhs)
{
return lhs.Equals(rhs);
}
public static Vector operator +(Vector left, Vector right)
{
return new Vector(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
}
public static Vector operator +(Vector left, float right)
{
return new Vector(left.X + right, left.Y + right, left.Z + right);
}
public static Vector operator -(Vector left, Vector right)
{
return new Vector(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
}
public static Vector operator -(Vector left, float right)
{
return new Vector(left.X - right, left.Y - right, left.Z - right);
}
public static Vector operator *(Vector left, Vector right)
{
return new Vector(left.X * right.X, left.Y * right.Y, left.Z * right.Z);
}
public static Vector operator *(Vector left, float right)
{
return new Vector(left.X * right, left.Y * right, left.Z * right);
}
public static Vector FromString(string str)
{
string[] parts = str.Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 3)
throw new FormatException();
Vector v = default;
v.X = float.Parse(parts[0], CultureInfo.InvariantCulture);
v.Y = float.Parse(parts[1], CultureInfo.InvariantCulture);
v.Z = float.Parse(parts[2], CultureInfo.InvariantCulture);
return v;
}
public void NormalizeAngles()
{
this.X = NormalizeAngle(this.X);
this.Y = NormalizeAngle(this.Y);
this.Z = NormalizeAngle(this.Z);
}
public override bool Equals(object? obj)
{
return obj is Vector quaternion && this.Equals(quaternion);
}
public bool Equals(Vector other)
{
return this.X == other.X
&& this.Y == other.Y
&& this.Z == other.Z;
}
public override int GetHashCode()
{
return HashCode.Combine(this.X, this.Y, this.Z);
}
public override string ToString()
{
return this.X.ToString(CultureInfo.InvariantCulture) + ", " + this.Y.ToString(CultureInfo.InvariantCulture) + ", " + this.Z.ToString(CultureInfo.InvariantCulture);
}
private static float NormalizeAngle(float angle)
{
while (angle > 360)
angle -= 360;
while (angle < 0)
angle += 360;
return angle;
}
}
}