-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoord.py
More file actions
128 lines (101 loc) · 3.62 KB
/
Copy pathcoord.py
File metadata and controls
128 lines (101 loc) · 3.62 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import math
class PolarCoord:
def __init__(self, d: float=0.0, a : float=0.0):
self._d: float = d
self._a: float = a
def __eq__(self, value):
return (isinstance(value, PolarCoord) and
(round(self.d, 6) == round(value.d, 6)) and
(round(self.a, 6) == round(value.a, 6)))
def __str__(self):
return f"d = {self.d}, a = {self.a}"
def to_cartesian(self):
return CoordUtils.to_cartesian(self.d, self.a)
@property
def d(self):
return self._d
@property
def a(self):
return self._a
class CartesianCoord:
def __init__(self, x: float=0.0, y: float=0.0):
self._x: float = x
self._y: float = y
def __eq__(self, value):
return (isinstance(value, CartesianCoord) and
(round(self.x, 6) == round(value.x, 6)) and
(round(self.y, 6) == round(value.y, 6)))
def __str__(self):
return f"x = {self.x}, y = {self.y}"
def __add__(self, value):
if isinstance(value, CartesianCoord):
return CartesianCoord(
x=self._x + value.x,
y=self._y + value.y
)
else:
raise TypeError(f"The object {value} is not a {type(self)}.")
def __sub__(self, value):
if isinstance(value, CartesianCoord):
return CartesianCoord(
x=self._x - value.x,
y=self._y - value.y
)
else:
raise TypeError(f"The object {value} is not a {type(self)}.")
def to_polar(self):
return CoordUtils.to_polar(self.x, self.y)
@property
def x(self):
return self._x
@property
def y(self):
return self._y
class CoordUtils:
def __init__(self):
"""Pass Constructor. Only static functions"""
pass
@staticmethod
def to_polar(dx: float, dy: float) -> PolarCoord:
"""Creates a polar coordinat from delta x and delta y
@param dx: Delta x
@param dy: Delta y
@return: Polar coordinate (0.0rad = east)
"""
return PolarCoord(
a=math.atan2(dy, dx),
d=math.sqrt(dx**2 + dy**2))
@staticmethod
def to_cartesian(d: float, a: float) -> CartesianCoord:
"""Creates a cartesian coordinate from a distance and angle
@param d: Distance
@param a: Angle (0.0rad = east)
@return: Cartesian coordinate (x = east, y = north)
"""
return CartesianCoord(
x=d * math.cos(a),
y=d * math.sin(a))
@staticmethod
def rad_to_degrees(a: float) -> float:
""" Converts radians into degrees. This function keeps the sign.
@param a: Angle in radians
@return Angel in degrees
"""
return a * 180.0 / math.pi
@staticmethod
def degrees_to_rad(a: float) -> float:
""" Converts degrees into radians. This function keeps the sign.
@param a: Angle in degrees
@return Angle in radians
"""
return a * math.pi / 180.0
@staticmethod
def get_azimuth(cartesian_coord_a: CartesianCoord, cartesian_coord_b: CartesianCoord) -> float:
""" Get global azimuth between two cartesian points in radians
@param cartesian_coord_a: First cartesian point
@param cartesian_coord_b: Second cartesian point
@return Azimuth in radians
"""
delta_polar = CoordUtils.to_polar(cartesian_coord_b.x - cartesian_coord_a.x,
cartesian_coord_b.y - cartesian_coord_a.y)
return delta_polar.a