simetri.helpers.vector
1from math import cos, sin 2from typing import Tuple, Union 3 4import numpy as np 5 6 7Vec2 = Tuple[float, float] 8 9 10class Vector2D: 11 """A 2D vector class. 12 13 Attributes: 14 vector (np.ndarray): The vector represented as a numpy array. 15 """ 16 17 def __init__(self, x: float, y: float): 18 """Initializes a 2D vector with x and y coordinates. 19 20 Args: 21 x (float): The x-coordinate. 22 y (float): The y-coordinate. 23 """ 24 self.vector = np.array([x, y]) 25 26 def __add__(self, other: Vec2) -> Vec2: 27 """Adds two vectors. 28 29 Args: 30 other (Vec2): The vector to add. 31 32 Returns: 33 Vec2: The resulting vector. 34 """ 35 return Vector2D(*(self.vector + other.vector)) 36 37 def __sub__(self, other: Vec2) -> Vec2: 38 """Subtracts two vectors. 39 40 Args: 41 other (Vec2): The vector to subtract. 42 43 Returns: 44 Vec2: The resulting vector. 45 """ 46 return Vector2D(*(self.vector - other.vector)) 47 48 def __mul__(self, other: Union[Vec2, float]) -> Union[float, Vec2]: 49 """Multiplies the vector with another vector or a scalar. 50 51 Args: 52 other (Union[Vec2, float]): The vector or scalar to multiply with. 53 54 Returns: 55 Union[float, Vec2]: The resulting vector or scalar. 56 """ 57 if isinstance(other, Vector2D): 58 res = np.cross(self.vector, other.vector) 59 elif isinstance(other, (int, float)): 60 res = Vector2D(*(other * self.vector)) 61 else: 62 res = NotImplemented 63 64 return res 65 66 def __neg__(self) -> Vec2: 67 """Negates the vector. 68 69 Returns: 70 Vec2: The negated vector. 71 """ 72 return Vector2D(*(-self.vector)) 73 74 def __abs__(self) -> float: 75 """Returns the norm of the vector. 76 77 Returns: 78 float: The norm of the vector. 79 """ 80 return np.linalg.norm(self.vector) 81 82 def norm(self) -> float: 83 """Returns the norm of the vector. 84 85 Returns: 86 float: The norm of the vector. 87 """ 88 return np.linalg.norm(self.vector) 89 90 def dot(self, other: Vec2) -> float: 91 """Returns the dot product of self and other. 92 93 Args: 94 other (Vec2): The vector to dot with. 95 96 Returns: 97 float: The dot product. 98 """ 99 return np.dot(self.vector, other.vector) 100 101 def cross(self, other: Vec2) -> float: 102 """Returns the cross product of self and other. 103 104 Args: 105 other (Vec2): The vector to cross with. 106 107 Returns: 108 float: The cross product. 109 """ 110 return np.cross(self.vector, other.vector) 111 112 def rotate(self, angle: float) -> Vec2: 113 """Rotates the vector counterclockwise by a given angle. 114 115 Args: 116 angle (float): The angle in degrees. 117 118 Returns: 119 Vec2: The rotated vector. 120 """ 121 angle_rad = np.radians(angle) 122 rotation_matrix = np.array( 123 [[cos(angle_rad), -sin(angle_rad)], [sin(angle_rad), cos(angle_rad)]] 124 ) 125 rotated_vector = rotation_matrix @ self.vector 126 return Vector2D(*rotated_vector) 127 128 def __repr__(self) -> str: 129 """Returns a string representation of the vector. 130 131 Returns: 132 str: The string representation. 133 """ 134 return f"({self.vector[0]:.2f}, {self.vector[1]:.2f})"
Vec2 =
typing.Tuple[float, float]
class
Vector2D:
11class Vector2D: 12 """A 2D vector class. 13 14 Attributes: 15 vector (np.ndarray): The vector represented as a numpy array. 16 """ 17 18 def __init__(self, x: float, y: float): 19 """Initializes a 2D vector with x and y coordinates. 20 21 Args: 22 x (float): The x-coordinate. 23 y (float): The y-coordinate. 24 """ 25 self.vector = np.array([x, y]) 26 27 def __add__(self, other: Vec2) -> Vec2: 28 """Adds two vectors. 29 30 Args: 31 other (Vec2): The vector to add. 32 33 Returns: 34 Vec2: The resulting vector. 35 """ 36 return Vector2D(*(self.vector + other.vector)) 37 38 def __sub__(self, other: Vec2) -> Vec2: 39 """Subtracts two vectors. 40 41 Args: 42 other (Vec2): The vector to subtract. 43 44 Returns: 45 Vec2: The resulting vector. 46 """ 47 return Vector2D(*(self.vector - other.vector)) 48 49 def __mul__(self, other: Union[Vec2, float]) -> Union[float, Vec2]: 50 """Multiplies the vector with another vector or a scalar. 51 52 Args: 53 other (Union[Vec2, float]): The vector or scalar to multiply with. 54 55 Returns: 56 Union[float, Vec2]: The resulting vector or scalar. 57 """ 58 if isinstance(other, Vector2D): 59 res = np.cross(self.vector, other.vector) 60 elif isinstance(other, (int, float)): 61 res = Vector2D(*(other * self.vector)) 62 else: 63 res = NotImplemented 64 65 return res 66 67 def __neg__(self) -> Vec2: 68 """Negates the vector. 69 70 Returns: 71 Vec2: The negated vector. 72 """ 73 return Vector2D(*(-self.vector)) 74 75 def __abs__(self) -> float: 76 """Returns the norm of the vector. 77 78 Returns: 79 float: The norm of the vector. 80 """ 81 return np.linalg.norm(self.vector) 82 83 def norm(self) -> float: 84 """Returns the norm of the vector. 85 86 Returns: 87 float: The norm of the vector. 88 """ 89 return np.linalg.norm(self.vector) 90 91 def dot(self, other: Vec2) -> float: 92 """Returns the dot product of self and other. 93 94 Args: 95 other (Vec2): The vector to dot with. 96 97 Returns: 98 float: The dot product. 99 """ 100 return np.dot(self.vector, other.vector) 101 102 def cross(self, other: Vec2) -> float: 103 """Returns the cross product of self and other. 104 105 Args: 106 other (Vec2): The vector to cross with. 107 108 Returns: 109 float: The cross product. 110 """ 111 return np.cross(self.vector, other.vector) 112 113 def rotate(self, angle: float) -> Vec2: 114 """Rotates the vector counterclockwise by a given angle. 115 116 Args: 117 angle (float): The angle in degrees. 118 119 Returns: 120 Vec2: The rotated vector. 121 """ 122 angle_rad = np.radians(angle) 123 rotation_matrix = np.array( 124 [[cos(angle_rad), -sin(angle_rad)], [sin(angle_rad), cos(angle_rad)]] 125 ) 126 rotated_vector = rotation_matrix @ self.vector 127 return Vector2D(*rotated_vector) 128 129 def __repr__(self) -> str: 130 """Returns a string representation of the vector. 131 132 Returns: 133 str: The string representation. 134 """ 135 return f"({self.vector[0]:.2f}, {self.vector[1]:.2f})"
A 2D vector class.
Attributes:
- vector (np.ndarray): The vector represented as a numpy array.
Vector2D(x: float, y: float)
18 def __init__(self, x: float, y: float): 19 """Initializes a 2D vector with x and y coordinates. 20 21 Args: 22 x (float): The x-coordinate. 23 y (float): The y-coordinate. 24 """ 25 self.vector = np.array([x, y])
Initializes a 2D vector with x and y coordinates.
Arguments:
- x (float): The x-coordinate.
- y (float): The y-coordinate.
def
norm(self) -> float:
83 def norm(self) -> float: 84 """Returns the norm of the vector. 85 86 Returns: 87 float: The norm of the vector. 88 """ 89 return np.linalg.norm(self.vector)
Returns the norm of the vector.
Returns:
float: The norm of the vector.
def
dot(self, other: Tuple[float, float]) -> float:
91 def dot(self, other: Vec2) -> float: 92 """Returns the dot product of self and other. 93 94 Args: 95 other (Vec2): The vector to dot with. 96 97 Returns: 98 float: The dot product. 99 """ 100 return np.dot(self.vector, other.vector)
Returns the dot product of self and other.
Arguments:
- other (Vec2): The vector to dot with.
Returns:
float: The dot product.
def
cross(self, other: Tuple[float, float]) -> float:
102 def cross(self, other: Vec2) -> float: 103 """Returns the cross product of self and other. 104 105 Args: 106 other (Vec2): The vector to cross with. 107 108 Returns: 109 float: The cross product. 110 """ 111 return np.cross(self.vector, other.vector)
Returns the cross product of self and other.
Arguments:
- other (Vec2): The vector to cross with.
Returns:
float: The cross product.
def
rotate(self, angle: float) -> Tuple[float, float]:
113 def rotate(self, angle: float) -> Vec2: 114 """Rotates the vector counterclockwise by a given angle. 115 116 Args: 117 angle (float): The angle in degrees. 118 119 Returns: 120 Vec2: The rotated vector. 121 """ 122 angle_rad = np.radians(angle) 123 rotation_matrix = np.array( 124 [[cos(angle_rad), -sin(angle_rad)], [sin(angle_rad), cos(angle_rad)]] 125 ) 126 rotated_vector = rotation_matrix @ self.vector 127 return Vector2D(*rotated_vector)
Rotates the vector counterclockwise by a given angle.
Arguments:
- angle (float): The angle in degrees.
Returns:
Vec2: The rotated vector.