38 lines
1.4 KiB
Python
38 lines
1.4 KiB
Python
import math
|
|
from Direction import Direction
|
|
|
|
class Spherical:
|
|
def __init__(self, distance, direction):
|
|
self.distance = distance
|
|
self.direction = direction
|
|
|
|
# def __init__(self, distance, horizontal, vertical):
|
|
# self.distance = distance
|
|
# self.direction = Direction(horizontal, vertical)
|
|
|
|
def to_cartesian(self):
|
|
x = self.distance * math.sin(self.direction.horizontal) * math.cos(self.direction.vertical)
|
|
y = self.distance * math.sin(self.direction.horizontal) * math.sin(self.direction.vertical)
|
|
z = self.distance * math.cos(self.direction.horizontal)
|
|
return x, y, z
|
|
|
|
def from_cartesian(self, x, y, z):
|
|
self.distance = math.sqrt(x**2 + y**2 + z**2)
|
|
self.direction.horizontal = math.acos(z / self.distance)
|
|
self.direction.vertical = math.atan2(y, x)
|
|
|
|
def __add__(self, other):
|
|
x1, y1, z1 = self.to_cartesian()
|
|
x2, y2, z2 = other.to_cartesian()
|
|
return Spherical.from_cartesian(x1 + x2, y1 + y2, z1 + z2)
|
|
|
|
def __sub__(self, other):
|
|
x1, y1, z1 = self.to_cartesian()
|
|
x2, y2, z2 = other.to_cartesian()
|
|
return Spherical.from_cartesian(x1 - x2, y1 - y2, z1 - z2)
|
|
|
|
def __repr__(self):
|
|
return f"Spherical(r={self.distance}, horizontal={self.direction.horizontal}, phi={self.direction.vertical})"
|
|
|
|
Spherical.zero = Spherical(0, Direction.forward)
|