-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Vector.py
55 lines (45 loc) · 1.44 KB
/
Vector.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
from math import sqrt, pow
from constants import *
class Vector2:
def __init__(self, x, y):
self.x = x
self.y = y
def magnitude(self):
return sqrt(pow(self.x, 2) + pow(self.y, 2) + pow(self.z, 2))
def __add__(self, b):
if type(b) is Vector2:
return Vector2(self.x + b.x, self.y + b.y)
return Vector2(self.x + b, self.y + b)
def __sub__(self, b):
if type(b) is Vector2:
return Vector2(self.x - b.x, self.y - b.y)
return Vector2(self.x - b, self.y - b)
def __mul__(self, b):
if type(b) is Vector2:
return Vector2(self.x * b.x, self.y * b.y)
return Vector2(self.x * b, self.y * b)
def __truediv__(self, b):
if type(b) is Vector2:
return Vector2(self.x / b.x, self.y / b.y)
if b == 0:
return Vector2(0, 0)
return Vector2(self.x / b, self.y / b)
def TuplePosition(self):
x, y =self.x, self.y
if self.x > Width:
x = Width
elif self.x < 0:
x = 0
if self.y > Height:
y = Height
if self.y < 0:
y = 0
return (x, y)
def __repr__(self):
return f'{self.x} , {self.y}'
def toVector(mat):
if len(mat) == 2:
return Vector2(mat[0][0], mat[1][0])
def Distance(v1, v2):
if type(v1) is Vector2 :
return sqrt( (v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y) )