-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPoint.py
More file actions
56 lines (45 loc) · 2.29 KB
/
Point.py
File metadata and controls
56 lines (45 loc) · 2.29 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
from __future__ import annotations
import math
from Vector import Vector3D
from typing import Optional, Union, Iterable
class Point3D:
x: Union[float, Iterable[float]]
y: Optional[float]
z: Optional[float]
def __init__(self, x=0, y=0, z=0) -> None:
try:
(self.x, self.y, self.z) = x
except TypeError:
self.x = x
self.y = y
self.z = z
def __add__(self, other) -> Union[Point3D, Vector3D]:
if isinstance(other, Vector3D):
return Point3D(x=self.x + other.x, y=self.y + other.y, z=self.z + other.z)
if isinstance(other, Point3D):
return Vector3D(x=self.x + other.x, y=self.y + other.y, z=self.z + other.z)
return Point3D(x=self.x + other, y=self.y + other, z=self.z + other)
def __sub__(self, other) -> Union[Point3D, Vector3D]:
if isinstance(other, Vector3D):
return Point3D(x=self.x - other.x, y=self.y - other.y, z=self.z - other.z)
elif isinstance(other, (int, float)):
return Point3D(x=self.x - other, y=self.y - other, z=self.z - other)
elif isinstance(other, Point3D):
return Vector3D(x=self.x - other.x, y=self.y - other.y, z=self.z - other.z)
def __rsub__(self, other) -> Union[Point3D, Vector3D]:
if isinstance(other, Vector3D):
return Point3D(x=other.x - self.x, y=other.y - self.y, z=other.z - self.z)
elif isinstance(other, (int, float)):
return Point3D(x=other - self.x, y=other - self.y, z=other - self.z)
elif isinstance(other, Point3D):
return Vector3D(x=other.x - self.x, y=other.y - self.y, z=other.z - self.z)
def __mul__(self, other) -> Point3D:
return Point3D(x=self.x * other, y=self.y * other, z=self.z * other)
def __rmul__(self, other) -> Point3D:
return Point3D(x=self.x * other, y=self.y * other, z=self.z * other)
def distance(self, point) -> float:
return math.sqrt((self.x - point.x) ** 2 + (self.y - point.y) ** 2 + (self.z - point.z) ** 2)
def __repr__(self) -> str:
return f"Point3D(x={self.x}, y={self.y}, z={self.z})"
def __eq__(self, other) -> bool:
return (self.x == other.x) and (self.y == other.y) and (self.z == other.z)