-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector2D.py
More file actions
63 lines (53 loc) · 1.71 KB
/
vector2D.py
File metadata and controls
63 lines (53 loc) · 1.71 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
import math
class Vector2D:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
return "({0},{1})".format(self.x, self.y)
def __add__(self, other):
if isinstance(other, Vector2D):
x = self.x + other.x
y = self.y + other.y
if isinstance(other, float):
x = self.x + other
y = self.y + other
if isinstance(other, int):
x = self.x + other
y = self.y + other
return Vector2D(x, y)
def __sub__(self, other):
x = self.x - other.x
y = self.y - other.y
return Vector2D(x, y)
def __mul__(self, other):
if isinstance(other, Vector2D):
x = self.x * other.x
y = self.y * other.y
elif isinstance(other, float):
x = self.x * other
y = self.y * other
elif isinstance(other, int):
x = self.x * other
y = self.y * other
return Vector2D(x, y)
def __truediv__(self, other):
if isinstance(other, Vector2D):
x = self.x / other.x
y = self.y / other.y
elif isinstance(other, float):
x = self.x / other
y = self.y / other
return Vector2D(x, y)
def __eq__(self, other):
if self.x == other.x and self.y == other.y:
return True
def CopyFrom(self, other):
self.x = other.x
self.y = other.y
def Clone(self):
return Vector2D(self.x, self.y)
def DistanceToPoint(self, point):
ax = (self.x - point.x)
ay = (self.y - point.y)
return math.sqrt(ax * ax + ay * ay)