-
Notifications
You must be signed in to change notification settings - Fork 3
Vector3
Represents a 3D vector with x, y, and z coordinates.
Initializes a new Vector instance with the given x, y, and z coordinates.
- `x` (float): X-coordinate of the vector.
- `y` (float): Y-coordinate of the vector.
- `z` (float): Z-coordinate of the vector.
-
__str__(self)
: Returns a string representation of the vector. -
angle_between(vector_1: Vector, vector_2: Vector) -> float
: Computes the angle in degrees between two vectors. The angle between two vectors is the angle required to rotate one vector onto the other, measured in degrees. -
angle_between_XY(vector_1: Vector, vector_2: Vector) -> float
: Computes the angle in degrees between two vectors projected onto the XY plane (Z-axis rotation). -
angle_between_XZ(vector_1: Vector, vector_2: Vector) -> float
: Computes the angle in degrees between two vectors projected onto the XZ plane (Y-axis rotation). -
angle_between_YZ(vector_1: Vector, vector_2: Vector) -> float
: Computes the angle in degrees between two vectors projected onto the YZ plane (X-axis rotation). -
angle_radian_between(vector_1: Vector, vector_2: Vector) -> float
: Computes the angle in radians between two vectors. The angle between two vectors is the angle required to rotate one vector onto the other, measured in radians. -
by_line(line_1) -> Vector
: Computes a vector representing the direction of a given line. This method takes a Line object and returns a Vector representing the direction of the line. -
by_two_points(point_1: Point, point_2: Point) -> Vector
: Computes the vector between two points. -
cross_product(vector_1: Vector, vector_2: Vector) -> Vector
: Computes the cross product of two vectors. The cross product of two vectors in three-dimensional space is a vector that is perpendicular to both original vectors. It is used to find a vector that is normal to a plane defined by the input vectors. -
deserialize(data)
: Converts a dictionary representation of a vector into a Vector object. This method takes a dictionary containing 'x', 'y', and 'z' keys with numeric values and creates a new Vector instance representing the vector described by these values. It's particularly useful for converting serialized vector data back into Vector objects, for instance, when loading vectors from a file or a database. -
diff(vector_1: Vector, vector_2: Vector) -> Vector
: Calculates the difference between two Vector objects. This method returns a new Vector object that is the result of subtracting the components ofvector_2
fromvector_1
. -
divide(vector_1: Vector, vector_2: Vector) -> Vector
: Divides the components of the first vector by the corresponding components of the second vector. This method performs component-wise division. If any component ofvector_2
is 0, the result for that component will be undefined. -
dot_product(vector_1: Vector, vector_2: Vector) -> Vector
: Computes the dot product of two vectors. The dot product of two vectors is a scalar quantity equal to the sum of the products of their corresponding components. It gives insight into the angle between the vectors. -
from_matrix(vector_list: list) -> Vector
: Creates a Vector object from a list representation. -
length(vector_1: Vector) -> float
: Computes the length (magnitude) of a vector. The length of a vector is the Euclidean norm or magnitude of the vector, which is calculated as the square root of the sum of the squares of its components. -
new_length(vector_1: Vector, newlength: float) -> Vector
: Rescales the vector to have the specified length. -
normalize(vector_1: Vector) -> Vector
: Returns the normalized form of the input vector. The normalized form of a vector is a vector with the same direction but with a length (magnitude) of 1. -
perpendicular(vector_1: Vector) -> tuple
: Computes two vectors perpendicular to the input vector. -
pitch(vector_1: Vector, angle: float) -> Vector
: Rotates a vector around the X-axis (pitch). This method rotates the vector around the X-axis (pitch) by the specified angle. -
product(number: float, vector_1: Vector) -> Vector
: Scales a vector by a scalar value. This method multiplies each component of the vector by the given scalar value. -
reverse(vector_1: Vector) -> Vector
: Returns the reverse (negation) of the vector. -
rotate_XY(vector: Vector, Beta: float) -> Vector
: Rotates the vector in the XY plane by the specified angle. -
scale(vector: Vector, scalefactor: float) -> Vector
: Scales the vector by the specified scale factor. -
serialize(self) -> dict
: Serializes the Vector object into a dictionary. -
square(vector_1: Vector) -> Vector
: Computes the square of each component of the input vector. -
subtract(vector_1: Vector, vector_2: Vector) -> Vector
: Subtracts the components of the second vector from the first. This method is synonymous withdiff
and serves the same purpose, providing an alternative naming convention. -
sum(vector_1: Vector, vector_2: Vector) -> Vector
: Adds two vectors element-wise. -
sum3(vector_1: Vector, vector_2: Vector, vector_3: Vector) -> Vector
: Calculates the sum of three Vector objects. This method returns a new Vector object whose components are the sum of the corresponding components of the three input vectors. -
to_line(vector_1: Vector, vector_2: Vector) -> Vector
: Creates a Line object from two vectors. -
to_matrix(vector: Vector) -> list
: Converts the vector to a list representation. -
to_point(vector_1: Vector) -> Vector
: Converts the vector to a Point object. -
value(vector_1: Vector) -> tuple
: Returns the rounded values of the vector's components.
Returns a string representation of the vector.
str
: A string representation of the vector.
vector = Vector(1.234, 2.345, 3.456)
print(vector)
# Vector(X = 1.234, Y = 2.345, Z = 3.456)
Computes the angle in degrees between two vectors. The angle between two vectors is the angle required to rotate one vector onto the other, measured in degrees.
-
vector_1
(Vector
): The first vector. -
vector_2
(Vector
): The second vector.
float
: The angle in degrees between the input vectors.
vector1 = Vector(1, 0, 0)
vector2 = Vector(0, 1, 0)
angle = Vector.angle_between(vector1, vector2)
# 90
Computes the angle in degrees between two vectors projected onto the XY plane (Z-axis rotation).
-
vector_1
(Vector
): The first vector. -
vector_2
(Vector
): The second vector.
float
: The angle in degrees between the input vectors projected onto the XY plane (Z-axis rotation).
vector1 = Vector(1, 0, 1)
vector2 = Vector(0, 1, 1)
angle = Vector.angle_between_XY(vector1, vector2)
# 45
Computes the angle in degrees between two vectors projected onto the XZ plane (Y-axis rotation).
-
vector_1
(Vector
): The first vector. -
vector_2
(Vector
): The second vector.
float
: The angle in degrees between the input vectors projected onto the XZ plane (Y-axis rotation).
vector1 = Vector(1, 0, 1)
vector2 = Vector(0, 1, 1)
angle = Vector.angle_between_XZ(vector1, vector2)
# 90
Computes the angle in degrees between two vectors projected onto the YZ plane (X-axis rotation).
-
vector_1
(Vector
): The first vector. -
vector_2
(Vector
): The second vector.
float
: The angle in degrees between the input vectors projected onto the YZ plane (X-axis rotation).
vector1 = Vector(1, 1, 0)
vector2 = Vector(1, 0, 1)
angle = Vector.angle_between_YZ(vector1, vector2)
# 90
Computes the angle in radians between two vectors. The angle between two vectors is the angle required to rotate one vector onto the other, measured in radians.
-
vector_1
(Vector
): The first vector. -
vector_2
(Vector
): The second vector.
float
: The angle in radians between the input vectors.
vector1 = Vector(1, 0, 0)
vector2 = Vector(0, 1, 0)
angle = Vector.angle_radian_between(vector1, vector2)
# 1.5707963267948966
Computes a vector representing the direction of a given line. This method takes a Line object and returns a Vector representing the direction of the line.
-
line_1
(Line
): The Line object from which to extract the direction.
Vector
: A Vector representing the direction of the line.
line = Line(start=Point(0, 0, 0), end=Point(1, 1, 1))
direction_vector = Vector.by_line(line)
# Vector(X = 1, Y = 1, Z = 1)
Computes the vector between two points.
-
point_1
(Point
): The starting point. -
point_2
(Point
): The ending point.
Vector
: A new Vector object representing the vector between the two points.
point1 = Point(1, 2, 3)
point2 = Point(4, 6, 8)
vector = Vector.by_two_points(point1, point2)
# Vector(X = 3, Y = 4, Z = 5)
Computes the cross product of two vectors. The cross product of two vectors in three-dimensional space is a vector that is perpendicular to both original vectors. It is used to find a vector that is normal to a plane defined by the input vectors.
-
vector_1
(Vector
): The first vector. -
vector_2
(Vector
): The second vector.
Vector
: A new Vector object representing the cross product of the input vectors.
vector1 = Vector(1, 2, 3)
vector2 = Vector(4, 5, 6)
cross_product = Vector.cross_product(vector1, vector2)
# Vector(X = -3, Y = 6, Z = -3)
Converts a dictionary representation of a vector into a Vector object. This method takes a dictionary containing 'x', 'y', and 'z' keys with numeric values and creates a new Vector instance representing the vector described by these values. It's particularly useful for converting serialized vector data back into Vector objects, for instance, when loading vectors from a file or a database.
-
data
(dict): A dictionary with keys 'x', 'y', and 'z', corresponding to the components of the vector. Each key's value should be a number (int or float).
Vector: A new Vector object initialized with the x, y, and z values from the input dictionary.
data = {'x': 1.0, 'y': 2.0, 'z': 3.0}
vector = Vector.deserialize(data)
# Vector object with x=1.0, y=2.0, z=3.0
Calculates the difference between two Vector objects.
This method returns a new Vector object that is the result of subtracting the components of vector_2
from vector_1
.
-
vector_1
(Vector
): The minuend vector. -
vector_2
(Vector
): The subtrahend vector.
Vector
: A new Vector object resulting from the component-wise subtraction of vector_2
from vector_1
.
vector1 = Vector(5, 7, 9)
vector2 = Vector(1, 2, 3)
result = Vector.diff(vector1, vector2)
# Vector(X = 4.000, Y = 5.000, Z = 6.000)
Divides the components of the first vector by the corresponding components of the second vector.
This method performs component-wise division. If any component of vector_2
is 0, the result for that component will be undefined.
-
vector_1
(Vector
): The numerator vector. -
vector_2
(Vector
): The denominator vector.
Vector
: A new Vector object resulting from the component-wise division.
vector1 = Vector(10, 20, 30)
vector2 = Vector(2, 4, 5)
result = Vector.divide(vector1, vector2)
# Vector(X = 5.000, Y = 5.000, Z = 6.000)
Computes the dot product of two vectors. The dot product of two vectors is a scalar quantity equal to the sum of the products of their corresponding components. It gives insight into the angle between the vectors.
-
vector_1
(Vector
): The first vector. -
vector_2
(Vector
): The second vector.
float
: The dot product of the input vectors.
vector1 = Vector(1, 2, 3)
vector2 = Vector(4, 5, 6)
dot_product = Vector.dot_product(vector1, vector2)
# 32
Creates a Vector object from a list representation.
-
vector_list
(list): The list representing the vector.
Vector
: A Vector object created from the list representation.
vector_list = [1, 2, 3]
vector = Vector.from_matrix(vector_list)
# Vector(X = 1, Y = 2, Z = 3)
Computes the length (magnitude) of a vector. The length of a vector is the Euclidean norm or magnitude of the vector, which is calculated as the square root of the sum of the squares of its components.
-
vector_1
(Vector
): The vector whose length is to be computed.
float
: The length of the input vector.
vector1 = Vector(1, 2, 3)
length = vector1.length
# 3.7416573867739413
Rescales the vector to have the specified length.
-
vector_1
(Vector
): The vector to be rescaled. -
newlength
(float): The desired length of the vector.
Vector
: A new Vector object representing the rescaled vector.
vector = Vector(3, 4, 0)
new_vector = Vector.new_length(vector, 5)
# Vector(X = 3.000, Y = 4.000, Z = 0.000)
Returns the normalized form of the input vector. The normalized form of a vector is a vector with the same direction but with a length (magnitude) of 1.
-
vector_1
(Vector
): The vector to be normalized.
Vector
: A new Vector object representing the normalized form of the input vector.
vector1 = Vector(3, 0, 4)
normalized_vector = Vector.normalize(vector1)
# Vector(X = 0.600, Y = 0.000, Z = 0.800)
Computes two vectors perpendicular to the input vector.
-
vector_1
(Vector
): The input vector.
tuple
: A tuple containing two vectors perpendicular to the input vector.
vector1 = Vector(1, 2, 3)
perpendicular_vectors = Vector.perpendicular(vector1)
# (Vector(X = 2, Y = -1, Z = 0), Vector(X = -3, Y = 0, Z = 1))
Rotates a vector around the X-axis (pitch). This method rotates the vector around the X-axis (pitch) by the specified angle.
-
vector_1
(Vector
): The vector to be rotated. -
angle
(float): The angle of rotation in radians.
Vector
: A new Vector object representing the rotated vector.
vector1 = Vector(1, 2, 3)
rotated_vector = Vector.pitch(vector1, math.pi/2)
# Vector(X = 1.000, Y = -3.000, Z = 2.000)
Scales a vector by a scalar value. This method multiplies each component of the vector by the given scalar value.
-
number
(float): The scalar value to scale the vector by. -
vector_1
(Vector
): The vector to be scaled.
Vector
: A new Vector object representing the scaled vector.
vector1 = Vector(1, 2, 3)
scaled_vector = Vector.product(2, vector1)
# Vector(X = 2, Y = 4, Z = 6)
Returns the reverse (negation) of the vector.
-
vector_1
(Vector
): The vector.
Vector
: The reverse (negation) of the input vector.
vector1 = Vector(1, 2, 3)
reversed_vector = Vector.reverse(vector1)
# Vector(X = -1, Y = -2, Z = -3)
Rotates the vector in the XY plane by the specified angle.
-
vector
(Vector
): The vector to be rotated. -
Beta
(float): The angle of rotation in radians.
Vector
: A new Vector object representing the rotated vector.
vector = Vector(1, 0, 0)
rotated_vector = Vector.rotate_XY(vector, math.pi/2)
# Vector(X = 0, Y = 1, Z = 0)
Scales the vector by the specified scale factor.
-
vector
(Vector
): The vector to be scaled. -
scalefactor
(float): The scale factor.
Vector
: A new Vector object representing the scaled vector.
vector = Vector(1, 2, 3)
scaled_vector = Vector.scale(vector, 2)
# Vector(X = 2, Y = 4, Z = 6)
Serializes the Vector object into a dictionary.
dict
: A dictionary containing the serialized data of the Vector object.
vector = Vector(1, 2, 3)
serialized_data = vector.serialize()
# {'id': None, 'type': None, 'x': 1, 'y': 2, 'z': 3}
Computes the square of each component of the input vector.
-
vector_1
(Vector
): The input vector.
Vector
: A new Vector object representing the square of each component of the input vector.
vector = Vector(2, 3, 4)
squared_vector = Vector.square(vector)
# Vector(X = 4, Y = 9, Z = 16)
Subtracts the components of the second vector from the first.
This method is synonymous with diff
and serves the same purpose, providing an alternative naming convention.
-
vector_1
(Vector
): The vector from which to subtract. -
vector_2
(Vector
): The vector to be subtracted.
Vector
: The result of the component-wise subtraction.
vector1 = Vector(10, 20, 30)
vector2 = Vector(1, 2, 3)
result = Vector.subtract(vector1, vector2)
# Vector(X = 9.000, Y = 18.000, Z = 27.000)
Adds two vectors element-wise.
-
vector_1
(Vector): First vector. -
vector_2
(Vector): Second vector.
Returns:
Vector
: Sum of the two input vectors.
vector_1 = Vector(19, 18, 17)
vector_2 = Vector(8, 17, 1)
output = Vector.sum(vector_1, vector_2)
# Vector(X = 27.000, Y = 35.000, Z = 18.000)
Calculates the sum of three Vector objects. This method returns a new Vector object whose components are the sum of the corresponding components of the three input vectors.
-
vector_1
,vector_2
,vector_3
(Vector
): The vectors to be summed.
Vector
: A new Vector object resulting from the component-wise sum of the input vectors.
vector1 = Vector(1, 2, 3)
vector2 = Vector(4, 5, 6)
Vector = Vector(-1, -2, -3)
result = Vector.sum3(vector1, vector2, Vector)
# Vector(X = 4.000, Y = 5.000, Z = 6.000)
Creates a Line object from two vectors.
-
vector_1
(Vector
): The start vector of the line. -
vector_2
(Vector
): The end vector of the line.
Line
: A Line object connecting the two vectors.
vector1 = Vector(10, 20, 30)
vector2 = Vector(2, 4, 5)
line = Vector.to_line(vector1, vector2)
# Line(start=Point(X = 10.000, Y = 20.000, Z = 30.000), end=Point(X = 2.000, Y = 4.000, Z = 5.000))
Converts the vector to a list representation.
-
vector
(Vector
): The vector to be converted.
list
: A list representation of the vector.
vector = Vector(1, 2, 3)
vector_list = Vector.to_matrix(vector)
# [1, 2, 3]
Converts the vector to a Point object.
-
vector_1
(Vector
): The vector to be converted to a Point object.
Point
: A Point object with coordinates same as the vector.
vector1 = Vector(10, 20, 30)
point = Vector.to_point(vector1)
# Point(X = 10.000, Y = 20.000, Z = 30.000)
Returns the rounded values of the vector's components.
-
vector_1
(Vector
): The vector.
tuple
: A tuple containing the rounded values of the vector's components.
vector1 = Vector(1.123456, 2.345678, 3.987654)
rounded_values = Vector.value(vector1)
# (1.1235, 2.3457, 3.9877)