- Read the guideline before start
In order to get information about vectors follow this:
- Vectors on coordinate plane
- Vector by two points
- Adding vectors
- Normalized vector
- Vectors dot product
- Angle between two vectors
- Rotating vectors
You recently got a job at a game development company as a graphic engineer. You have to calculate light tracing, but it is impossible without using vectors.
Vector on coordinate plane is directed line segment, that always starts in (0, 0) and ends in coordinates (x, y).
Implement Vector class, its __init__ method takes and stores two
coordinates: x, y - coordinates of end of the vector, rounded
to two decimals.
vector = Vector(-2.343, 8.008)
vector.x == -2.34
vector.y == 8.0Vector class should have such magic methods:
__add__
Addition of two Vectors should return Vector.
vector1 = Vector(2, 4)
vector2 = Vector(-1, 3)
vector3 = vector1 + vector2
isinstance(vector3, Vector) is True
vector3.x == 1
vector3.y == 7__sub__
Subtraction of two Vectors should return Vector.
vector1 = Vector(2, 4)
vector2 = Vector(-1, 3)
vector3 = vector1 - vector2
isinstance(vector3, Vector) is True
vector3.x == 3
vector3.y == 1__mul__
Multiplying Vector on a number should return another Vector.
vector1 = Vector(2, 4)
vector2 = vector1 * 3.743
isinstance(vector2, Vector) is True
vector2.x == 7.49
vector2.y == 14.97Multiplying Vector on Vector should return their dot product.
vector1 = Vector(2.11, 4.55)
vector2 = Vector(-3.51, 10.33)
dot_product = vector1 * vector2
dot_product == 39.5954Also, Vector class should have such methods:
create_vector_by_two_pointsTakesstart_point- tuple of point coordinates, start of the vector,end_point- tuple of point coordinates, end of the vector. It returns Vector.
start_point = (5.2, 2.6)
end_point = (10.7, 6)
vector = Vector.create_vector_by_two_points(start_point, end_point)
isinstance(vector, Vector) is True
vector.x == 5.5
vector.y == 3.4NOTE: create_vector_by_two_points should be a classmethod
get_length
Returns length of the vector.
vector = Vector(2, 4)
vector.get_length() == 4.47213595499958get_normalized
Returns normalized copy of vector.
vector1 = Vector(13, -4)
vector1.get_length() == 13.6
vector2 = vector1.get_normalized()
vector2.x == 0.96
vector2.y == -0.29
vector2.get_length() == 1.0angle_between
Takes vector and returns angle between current vector and vector
in integer degrees.
vector1 = Vector(13, -4)
vector2 = Vector(-6, -11)
vector1.angle_between(vector2) == 102Most likely you get cosine of the angle. To correct calculate angle
use math library.
For your cosine cos_a use math.degrees(math.acos(cos_a)) in order
to get degrees.
NOTE: In this method round only returning degrees.
get_angle
Returns angle between current vector and positive Y axis.
vector = Vector(33, 8)
vector.get_angle() == 76rotate
Takes degrees that is integer rotation degrees.
It returns rotated Vector by degrees.
vector = Vector(33, 8)
vector2 = vector.rotate(45)
vector2.x == 17.68
vector2.y == 28.99You may use math.cos, math.sin here, but they use radians.
In order to convert degrees to radians use math.radians.