Skip to content

Commit c801730

Browse files
isHarryhK0lb3
authored andcommitted
refactor(math): Vector2
1 parent 6cdc42f commit c801730

File tree

1 file changed

+82
-159
lines changed

1 file changed

+82
-159
lines changed

UnityPy/math/Vector2.py

Lines changed: 82 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,83 @@
1+
from dataclasses import dataclass
2+
from math import sqrt
3+
4+
5+
kEpsilon = 0.00001
6+
7+
8+
@dataclass
19
class Vector2:
2-
X: float
3-
Y: float
4-
5-
def __init__(self, x: float, y: float):
6-
self.X = x
7-
self.Y = y
8-
9-
10-
"""
11-
using System;
12-
using System.Runtime.InteropServices;
13-
14-
namespace AssetStudio
15-
{
16-
[StructLayout(LayoutKind.Sequential, Pack = 4)]
17-
public struct Vector2 : IEquatable<Vector2>
18-
{
19-
public float X;
20-
public float Y;
21-
22-
public Vector2(float x, float y)
23-
{
24-
X = x;
25-
Y = y;
26-
}
27-
28-
public float this[int index]
29-
{
30-
get
31-
{
32-
switch (index)
33-
{
34-
case 0: return X;
35-
case 1: return Y;
36-
default: throw new ArgumentOutOfRangeException(nameof(index), "Invalid Vector2 index!");
37-
}
38-
}
39-
40-
set
41-
{
42-
switch (index)
43-
{
44-
case 0: X = value; break;
45-
case 1: Y = value; break;
46-
default: throw new ArgumentOutOfRangeException(nameof(index), "Invalid Vector2 index!");
47-
}
48-
}
49-
}
50-
51-
public override int GetHashCode()
52-
{
53-
return X.GetHashCode() ^ (Y.GetHashCode() << 2);
54-
}
55-
56-
public override bool Equals(object other)
57-
{
58-
if (!(other is Vector2))
59-
return false;
60-
return Equals((Vector2)other);
61-
}
62-
63-
public bool Equals(Vector2 other)
64-
{
65-
return X.Equals(other.X) && Y.Equals(other.Y);
66-
}
67-
68-
public void Normalize()
69-
{
70-
var length = Length();
71-
if (length > kEpsilon)
72-
{
73-
var invNorm = 1.0f / length;
74-
X *= invNorm;
75-
Y *= invNorm;
76-
}
77-
else
78-
{
79-
X = 0;
80-
Y = 0;
81-
}
82-
}
83-
84-
public float Length()
85-
{
86-
return (float)Math.Sqrt(LengthSquared());
87-
}
88-
89-
public float LengthSquared()
90-
{
91-
return X * X + Y * Y;
92-
}
93-
94-
public static Vector2 Zero => new Vector2();
95-
96-
public static Vector2 operator +(Vector2 a, Vector2 b)
97-
{
98-
return new Vector2(a.X + b.X, a.Y + b.Y);
99-
}
100-
101-
public static Vector2 operator -(Vector2 a, Vector2 b)
102-
{
103-
return new Vector2(a.X - b.X, a.Y - b.Y);
104-
}
105-
106-
public static Vector2 operator *(Vector2 a, Vector2 b)
107-
{
108-
return new Vector2(a.X * b.X, a.Y * b.Y);
109-
}
110-
111-
public static Vector2 operator /(Vector2 a, Vector2 b)
112-
{
113-
return new Vector2(a.X / b.X, a.Y / b.Y);
114-
}
115-
116-
public static Vector2 operator -(Vector2 a)
117-
{
118-
return new Vector2(-a.X, -a.Y);
119-
}
120-
121-
public static Vector2 operator *(Vector2 a, float d)
122-
{
123-
return new Vector2(a.X * d, a.Y * d);
124-
}
125-
126-
public static Vector2 operator *(float d, Vector2 a)
127-
{
128-
return new Vector2(a.X * d, a.Y * d);
129-
}
130-
131-
public static Vector2 operator /(Vector2 a, float d)
132-
{
133-
return new Vector2(a.X / d, a.Y / d);
134-
}
135-
136-
public static bool operator ==(Vector2 lhs, Vector2 rhs)
137-
{
138-
return (lhs - rhs).LengthSquared() < kEpsilon * kEpsilon;
139-
}
140-
141-
public static bool operator !=(Vector2 lhs, Vector2 rhs)
142-
{
143-
return !(lhs == rhs);
144-
}
145-
146-
public static implicit operator Vector3(Vector2 v)
147-
{
148-
return new Vector3(v.X, v.Y, 0);
149-
}
150-
151-
public static implicit operator Vector4(Vector2 v)
152-
{
153-
return new Vector4(v.X, v.Y, 0.0F, 0.0F);
154-
}
155-
156-
private const float kEpsilon = 0.00001F;
157-
}
158-
}
159-
160-
"""
10+
'''https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector2.cs'''
11+
12+
X: float = 0.0
13+
Y: float = 0.0
14+
15+
def __init__(self, x: float = 0.0, y: float = 0.0):
16+
if not all(isinstance(v, (int, float)) for v in (x, y)):
17+
raise TypeError("All components must be numeric.")
18+
self.X = float(x)
19+
self.Y = float(y)
20+
21+
def __getitem__(self, index):
22+
return (self.X, self.Y)[index]
23+
24+
def __setitem__(self, index, value):
25+
if index == 0:
26+
self.X = value
27+
elif index == 1:
28+
self.Y = value
29+
else:
30+
raise IndexError("Index out of range")
31+
32+
def __hash__(self):
33+
return self.X.__hash__() ^ (self.Y.__hash__() << 2)
34+
35+
def normalize(self):
36+
length = self.length()
37+
if length > kEpsilon:
38+
invNorm = 1.0 / length
39+
self.X *= invNorm
40+
self.Y *= invNorm
41+
else:
42+
self.X = self.Y = 0.0
43+
44+
Normalize = normalize
45+
46+
def length(self):
47+
return sqrt(self.lengthSquared())
48+
49+
Length = length
50+
51+
def lengthSquared(self):
52+
return self.X ** 2 + self.Y ** 2
53+
54+
LengthSquared = lengthSquared
55+
56+
@staticmethod
57+
def Zero():
58+
return Vector2(0, 0)
59+
60+
@staticmethod
61+
def One():
62+
return Vector2(1, 1)
63+
64+
def __add__(a, b):
65+
return Vector2(a.X + b.X, a.Y + b.Y)
66+
67+
def __sub__(a, b):
68+
return Vector2(a.X - b.X, a.Y - b.Y)
69+
70+
def __mul__(a, d):
71+
return Vector2(a.X * d, a.Y * d)
72+
73+
def __truediv__(a, d):
74+
return Vector2(a.X / d, a.Y / d)
75+
76+
def __eq__(lhs, rhs):
77+
if isinstance(rhs, Vector2):
78+
diff = lhs - rhs
79+
return diff.lengthSquared() < kEpsilon * kEpsilon
80+
return False
81+
82+
def __ne__(lhs, rhs):
83+
return not (lhs == rhs)

0 commit comments

Comments
 (0)