forked from K0lb3/UnityPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2.py
More file actions
160 lines (132 loc) · 3.73 KB
/
Vector2.py
File metadata and controls
160 lines (132 loc) · 3.73 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
class Vector2:
X: float
Y: float
def __init__(self, x: float, y: float):
self.X = x
self.Y = y
"""
using System;
using System.Runtime.InteropServices;
namespace AssetStudio
{
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Vector2 : IEquatable<Vector2>
{
public float X;
public float Y;
public Vector2(float x, float y)
{
X = x;
Y = y;
}
public float this[int index]
{
get
{
switch (index)
{
case 0: return X;
case 1: return Y;
default: throw new ArgumentOutOfRangeException(nameof(index), "Invalid Vector2 index!");
}
}
set
{
switch (index)
{
case 0: X = value; break;
case 1: Y = value; break;
default: throw new ArgumentOutOfRangeException(nameof(index), "Invalid Vector2 index!");
}
}
}
public override int GetHashCode()
{
return X.GetHashCode() ^ (Y.GetHashCode() << 2);
}
public override bool Equals(object other)
{
if (!(other is Vector2))
return false;
return Equals((Vector2)other);
}
public bool Equals(Vector2 other)
{
return X.Equals(other.X) && Y.Equals(other.Y);
}
public void Normalize()
{
var length = Length();
if (length > kEpsilon)
{
var invNorm = 1.0f / length;
X *= invNorm;
Y *= invNorm;
}
else
{
X = 0;
Y = 0;
}
}
public float Length()
{
return (float)Math.Sqrt(LengthSquared());
}
public float LengthSquared()
{
return X * X + Y * Y;
}
public static Vector2 Zero => new Vector2();
public static Vector2 operator +(Vector2 a, Vector2 b)
{
return new Vector2(a.X + b.X, a.Y + b.Y);
}
public static Vector2 operator -(Vector2 a, Vector2 b)
{
return new Vector2(a.X - b.X, a.Y - b.Y);
}
public static Vector2 operator *(Vector2 a, Vector2 b)
{
return new Vector2(a.X * b.X, a.Y * b.Y);
}
public static Vector2 operator /(Vector2 a, Vector2 b)
{
return new Vector2(a.X / b.X, a.Y / b.Y);
}
public static Vector2 operator -(Vector2 a)
{
return new Vector2(-a.X, -a.Y);
}
public static Vector2 operator *(Vector2 a, float d)
{
return new Vector2(a.X * d, a.Y * d);
}
public static Vector2 operator *(float d, Vector2 a)
{
return new Vector2(a.X * d, a.Y * d);
}
public static Vector2 operator /(Vector2 a, float d)
{
return new Vector2(a.X / d, a.Y / d);
}
public static bool operator ==(Vector2 lhs, Vector2 rhs)
{
return (lhs - rhs).LengthSquared() < kEpsilon * kEpsilon;
}
public static bool operator !=(Vector2 lhs, Vector2 rhs)
{
return !(lhs == rhs);
}
public static implicit operator Vector3(Vector2 v)
{
return new Vector3(v.X, v.Y, 0);
}
public static implicit operator Vector4(Vector2 v)
{
return new Vector4(v.X, v.Y, 0.0F, 0.0F);
}
private const float kEpsilon = 0.00001F;
}
}
"""