Exemplo n.º 1
0
    def __init__(self, *args: Coordinate):

        self.args = args

        if len(args) > 0:
            self.x = args[0]
        else:
            self.x = 0

        if len(args) > 1:
            self.y = args[1]
        else:
            self.y = 0

        if len(args) > 2:
            self.z = args[2]
        else:
            self.z = 0

        self.values = (self.x, self.y, self.z)

        sum_squared_2d_components = sum(
            [components**2 for components in (self.x, self.y)])

        sum_squared_3d_components = sum(
            [components**2 for components in (self.x, self.y, self.z)])

        self.r = closestnum(math.sqrt(sum_squared_2d_components))
        self.rho = closestnum(math.sqrt(sum_squared_3d_components))
        self.theta = closestnum(math.atan2(self.y, self.x))
        self.phi = closestnum(math.acos(self.z / self.rho))
Exemplo n.º 2
0
    def angle(self,
              other: 'Vector3',
              degree: bool = False,
              pretty_print: bool = False) -> Union[float, str]:
        '''
        Returns the angle between two vectors
        e.g. Vector3(1, 2, 3).angle(Vector3(2, 4, 6)) == 0
        '''

        angle_radians = math.acos(self.dot(other) / (self.norm * other.norm))
        angle_degrees = 180 / math.pi * angle_radians

        if degree:

            if pretty_print:

                return f'{angle_degrees:.2f}º'

            return closestnum(angle_degrees)

        if pretty_print:

            if where_is_pi(angle_radians) != angle_radians:

                return f'{where_is_pi(angle_radians)} rad'

            return f'{angle_radians:.3f} rad'

        return closestnum(angle_radians)
Exemplo n.º 3
0
    def cross(self, other: 'Vector3') -> 'Vector3':
        '''
        Returns the cross product between two vectors.
        Namely a x b
        Can be used as a ** b in code.
        '''

        x = closestnum(self.y * other.z - self.z * other.y)
        y = closestnum(self.z * other.x - self.x * other.z)
        z = closestnum(self.x * other.y - self.y * other.x)

        return Vector3(x, y, z)
Exemplo n.º 4
0
    def norm(self) -> float:
        '''
        Returns the norm (magnitude) of the vector.
        '''

        components_square = tuple(component**2 for component in self)

        return closestnum(math.sqrt(sum(components_square)))
Exemplo n.º 5
0
    def __init__(self,
                 norm: RealNumber,
                 angle: RealNumber = 0,
                 degree: bool = False,
                 polar_repr: bool = False):

        self.ang = angle

        self._polar_repr = polar_repr

        if degree:

            angle = math.radians(angle)

        self.x = closestnum(norm * math.cos(angle))
        self.y = closestnum(norm * math.sin(angle))

        super().__init__(self.x, self.y)
Exemplo n.º 6
0
    def _scalar_division(self, other: RealNumber) -> 'Vector3':
        '''
        Returns the division between a vector and a scalar
        e.g. Vector3(3, 6, 9) / 3 == Vector3(1, 2, 3)
        '''

        scaled_components = tuple(closestnum(a / other) for a in self)

        return Vector3(*scaled_components)
Exemplo n.º 7
0
    def _scalar_multiplication(self, other: RealNumber) -> 'Vector3':
        '''
        Returns the multiplication between a vector and a scalar
        e.g. Vector3(1, 2, 3) * 2 == Vector3(2, 4, 6)
        '''

        scaled_components = tuple(closestnum(a * other) for a in self)

        return Vector3(*scaled_components)
Exemplo n.º 8
0
    def normalize(self) -> 'Vector3':
        '''
        Returns a normalized vector, i.e. a vector with the same direction with norm (magnitude) == 1
        e.g. Vector3(2, 0, 0).normalize() == Vector3(1, 0, 0)
        '''

        normalized = tuple(closestnum(a / self.norm) for a in self)

        return Vector3(*normalized)
Exemplo n.º 9
0
    def dot(self, other: 'Vector3') -> float:
        '''
        Returns the dot product between two vectors.
        Namely a • b
        Can be used as a * b in code.
        '''

        dot_product = sum(a * b for a, b in zip(self, other))

        return closestnum(dot_product)
Exemplo n.º 10
0
    def _vector_subtraction(self, other: 'Vector3') -> 'Vector3':
        '''
        Returns the difference between two vectors
        Namely a - b
        '''

        subtracted_components = tuple(
            closestnum(a - b) for a, b in zip(self, other))

        return Vector3(*subtracted_components)
Exemplo n.º 11
0
    def _vector_addition(self, other: 'Vector3') -> 'Vector3':
        '''
        Returns the sum of two vectors
        Namely a + b
        '''

        summed_components = tuple(
            closestnum(a + b) for a, b in zip(self, other))

        return Vector3(*summed_components)
Exemplo n.º 12
0
    def __init__(self,
                 r: RealNumber,
                 theta: RealNumber,
                 z: RealNumber,
                 degree: bool = False,
                 cylindrical_repr: bool = False):

        self.r = r
        self.theta = theta
        self.z = z

        self._cylindrical_repr = cylindrical_repr

        if degree:

            theta = math.radians(theta)

        self.x = closestnum(r * math.cos(theta))
        self.y = closestnum(r * math.sin(theta))

        super().__init__(self.x, self.y, self.z)
Exemplo n.º 13
0
    def compact_radius(self) -> str:
        '''
        This function makes clear the presence of square roots, so the output
        of conversions to polar, cylindrical, and spherical coordinate vectors become nicer.
        
        For instance, the radius "r", used in cylindrical coordinates, for Vector3(1, 1, 1) is 1.414213...
        and the radius "rho", used in spherical coordinates, for the same vector is 1.732050...
        Both are, successively, sqrt(2) and sqrt(3), and this is how the function represents them.
        
        Also works for, for instance, the radius "rho" for Vector(1, 1, 4) is sqrt(18),
        which can also be represented as 3 sqrt(2), and this is how the function represents it.
        '''

        sum_squared_2d_components = closestnum(self.x**2 + self.y**2)
        sum_squared_3d_components = closestnum(self.x**2 + self.y**2 +
                                               self.z**2)

        r = pretty_sqrt(sum_squared_2d_components)
        rho = pretty_sqrt(sum_squared_3d_components)

        return {'r': r, 'rho': rho}
Exemplo n.º 14
0
    def __init__(self,
                 rho: RealNumber,
                 theta: RealNumber,
                 phi: RealNumber,
                 degree: bool = False,
                 spherical_repr: bool = False):

        self.rho = rho
        self.theta = theta
        self.phi = phi

        self._spherical_repr = spherical_repr

        if degree:

            theta = math.radians(theta)
            phi = math.radians(phi)

        self.x = closestnum(rho * math.sin(phi) * math.cos(theta))
        self.y = closestnum(rho * math.sin(phi) * math.sin(theta))
        self.z = closestnum(rho * math.cos(phi))

        super().__init__(self.x, self.y, self.z)