-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR3.py
89 lines (67 loc) · 2.76 KB
/
R3.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Defines some mathematical objects in three-dimensional Euclidean space - R3 -
namely, the point and vector. Unary operations of the vector and point are
native to the classes.
"""
class R3Point:
"""
Defines a point in Euclidean 3-space.
"""
def __init__(self, x: float = 0, y: float = 0, z: float = 0):
"""
Initialises a point with the given coords, defaults to origin. Coordinates
must either be given in full as a str (x, y, z) or as separate arguments.
If x alone is given, y and z default to 0, but x must be specified as 0 if
y is to be given as a nonzero coordinate.
"""
self._x, self._y, self._z = x, y, z
def __str__(self) -> str:
return "(" + str(self._x) + ", " + str(self._y) + ", " + str(self._z) + ")"
def overwrite(self, x = 0, y = 0, z = 0):
"""
Overwrites the current coordinates, defaulting to zero.
"""
self._x, self._y, self._z = x, y, z
class R3Vector(R3Point):
"""
Defines a vector in Euclidean 3-space, with some scalar and unary operations.
"""
def __init__(self, point1: R3Point = R3Point(0, 0, 0), point2 = R3Point(0, 0, 0)):
"""
Defines a new vector between point1 and point2. If point2 is not specified,
gives the origin vector.
"""
super().__init__(getattr(point1, '_x') - getattr(point2, '_x'),
getattr(point1, '_y') - getattr(point2, '_y'),
getattr(point1, '_z') - getattr(point2, '_z'))
def __str__(self) -> str:
if (int(self._x) == self._x and int(self._y) == self._y and int(self._z) == self._z):
self._x, self._y, self._z = int(self._x), int(self._y), int(self._z)
return "<" + super().__str__()[1: -1] + ">"
def magnitude(self) -> float:
"""
Returns the magnitude of the vector.
"""
return ((self._x) ** 2 + (self._y) ** 2 + (self._z ** 2)) ** 0.5
def direction_vector(self):
"""
Returns a unit vector in the direction of the original vector.
"""
return self * (self.magnitude()**(-1))
def reverse(self):
"""
Returns the vector whose direction is opposite to this vector.
"""
return self * -1
def __add__(self, B):
return R3Vector(R3Point(self._x + B._x, self._y + B._y, self._z + B._z))
def __mul__(self, B: float):
return R3Vector(R3Point(self._x * B, self._y * B, self._z * B))
def __sub__(self, B):
return R3Vector(R3Point(self._x - B._x, self._y - B._y, self._z - B._z))
def __rmul__(self, B):
return self * B
def __eq__(self, B):
return str(self) == str(B) and type(self) == type(B)
def __ne__(self, B):
return not self == B