mirror of
https://github.com/pim-n/pg-rad
synced 2026-03-11 19:58:11 +01:00
Update sources and objects to new position system
This commit is contained in:
@ -1,36 +1,42 @@
|
||||
import math
|
||||
from typing import Self
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
class BaseObject:
|
||||
def __init__(
|
||||
self,
|
||||
x: float,
|
||||
y: float,
|
||||
z: float,
|
||||
pos: tuple[float, float, float],
|
||||
name: str = "Unnamed object",
|
||||
color: str = 'grey'):
|
||||
"""
|
||||
A generic object.
|
||||
|
||||
Args:
|
||||
x (float): X coordinate.
|
||||
y (float): Y coordinate.
|
||||
z (float): Z coordinate.
|
||||
pos (tuple[float, float, float]): Position vector (x,y,z).
|
||||
name (str, optional): Name for the object.
|
||||
Defaults to "Unnamed object".
|
||||
color (str, optional): Matplotlib compatible color string.
|
||||
Defaults to "red".
|
||||
"""
|
||||
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.z = z
|
||||
if len(pos) != 3:
|
||||
raise ValueError("Position must be tuple of length 3 (x,y,z).")
|
||||
self.pos = pos
|
||||
self.name = name
|
||||
self.color = color
|
||||
|
||||
def distance_to(self, other: Self) -> float:
|
||||
return math.dist(
|
||||
(self.x, self.y, self.z),
|
||||
(other.x, other.y, other.z),
|
||||
)
|
||||
def distance_to(self, other: Self | tuple) -> float:
|
||||
if isinstance(other, tuple) and len(other) == 3:
|
||||
r = np.linalg.norm(
|
||||
np.subtract(self.pos, other)
|
||||
)
|
||||
else:
|
||||
try:
|
||||
r = np.linalg.norm(
|
||||
np.subtract(self.pos, other.pos)
|
||||
)
|
||||
except AttributeError as e:
|
||||
raise e("other must be an object in the world \
|
||||
or a position tuple (x,y,z).")
|
||||
return r
|
||||
|
||||
Reference in New Issue
Block a user