-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector3.py
75 lines (63 loc) · 2.09 KB
/
vector3.py
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
from math import copysign
from typing import Union, overload
class Vector3:
"""
Represents a point or a vector in 3D cartesian space
(integers only)
"""
x:int
y:int
z:int
def __init__(self,a:int=0,b:int=0,c:int=0) -> None:
self.x, self.y, self.z = a,b,c
def toTuple(self) -> tuple[int,int,int]:
return int(self.x),int(self.y),int(self.z)
def rotateX(self) -> None:
self.z, self.y = self.y, -self.z
def rotateY(self) -> None:
self.x, self.z = self.z, -self.x
def rotateZ(self) -> None:
self.y, self.x = self.x, -self.y
def copy(self) -> "Vector3":
return Vector3(self.x,self.y,self.z)
def __str__(self) -> str:
return str(self.toTuple())
def __eq__(self, __o: "Vector3") -> bool:
if int(self.x) != int(__o.x):return False
if int(self.y) != int(__o.y):return False
if int(self.z) != int(__o.z):return False
return True
def __add__(self,__o: "Vector3") -> "Vector3":
return Vector3(self.x+__o.x,self.y+__o.y,self.z+__o.z)
def __sub__(self, __o:"Vector3") -> "Vector3":
return Vector3(self.x-__o.x, self.y - __o.y, self.z - __o.z)
def __hash__(self) -> int:
return hash((self.x,self.y,self.z))
def abs(self) -> "Vector3":
nx = abs(self.x)
ny = abs(self.y)
nz = abs(self.x)
return Vector3(nx,ny,nz)
def intDistance(a:Vector3,b:Vector3) -> int:
"""
Cartesian distance between two vectors, rounded down
"""
from math import sqrt,floor
dx,dy,dz = a.x-b.x, a.y-b.y, a.z-b.z
return floor(sqrt(dx*dx + dy*dy + dz*dz))
def vecDistance(a:Vector3,b:Vector3) -> tuple[int,int,int]:
"""
Vector distance between two vectors, converted to tuple\n
The result plus a equals b
"""
return (b-a).toTuple()
def manDistance(a:Vector3,b:Vector3) -> int:
"""
Manhattan distance between a and b
"""
return abs(a.x-b.x) + abs(a.y-b.y) + abs(a.z-b.z)
#Debug main function
if __name__ == "__main__":
v1 = Vector3(-1,0,0)
v2 = Vector3(1,1,0)
print(vecDistance(v1,v2))