Vectors
2D Vectors
A two-dimensional vector is a point relative to the origin (which may be defined anywhere on the plane). This is equivalent to an arrow that starts at the origin and ends at the above point.
Consider the (2,7) point to be the origin. The (4,4) point is a point relative to the origin, which can be represented as a point or as an arrow from the origin to the point.
dino_vectors = [
(6,4), (3,1), (1,2), (-1,5), (-2,5), (-3,4), (-4,4),
(-5,3), (-5,2), (-2,2), (-5,1), (-4,0), (-2,1), (-1,0), (0,-3),
(-1,-4), (1,-4), (2,-3), (1,-2), (3,-1), (5,1)
]
draw(
Points(*dino_vectors),
)draw(
Polygon(*dino_vectors)
)curve_vectors = [(x, x**2) for x in range(-10, 11)]
draw(
Points(*curve_vectors),
grid=(1, 10),
nice_aspect_ratio=False
)Adding / translating vectors
def add_vectors(v1, v2):
# Add the x coordinates from v1 and v2 for the translated x coordinate
# and pair with the translated y coordinates similarly
return (v1[0] + v2[0], v1[1] + v2[1])dino_vectors2 = [add_vectors((-1.5, -2.5), v) for v in dino_vectors]# Plot the translated dino vectors
draw(
Points(*dino_vectors, color=blue),
Polygon(*dino_vectors, color=blue),
Points(*dino_vectors2, color=red),
Polygon(*dino_vectors2, color=red)
)Breaking up vectors into components
A vector can be thought of as two component vectors added together. The y component and x component (in red). When added together, you get the original, resulting vector (in blue).
vector = (4,3)
y_component = (4,0)
x_component = (0,3)
draw(
Arrow(vector, color=blue),
Arrow(y_component),
Arrow(vector,(y_component[0], x_component[0])) # Note, this is tip-to-tail
)
These two components form the two shorter sides of a right triangle. Therefore, the length of the resultant vector can be found using the Pythagorean Theorum:
from math import sqrt
def vector_length(v):
y_squared = v[0]**2
x_squared = v[1]**2
return sqrt(y_squared + x_squared)
print(f"Two vectors, {y_component}, and {x_component}, summed give the blue vector {vector} with length: {vector_length(vector)}")Two vectors, (4, 0), and (0, 3), summed give the blue vector (4, 3) with length: 5.0Multiplying vectors: repeated tip-to-tail stacking
Scalar multiplication: Each vector component is scaled by the same amount - effectively enlarging the right triangle mentioned above.
This can also be thought of as repeating the same vector over and over X times, stacking it tip-to-tail.
vector_v = (6,4)
# 5 * vector_v is a vector in the same direction, but five times as long
draw(Arrow(vector_v, color=blue),
Polygon((0,0), (6,0), color=green),
Polygon((6,0), (6,4), color=green)
)
vector_2v = (vector_v[0] * 2, vector_v[1] * 2)
draw(
Arrow(vector_v, color=blue),
Polygon((0,0), (6,0), color=green),
Polygon((6,0), (6,4), color=green),
Arrow(vector_2v, color=red),
Polygon((vector_v[0],0), (vector_2v[0], 0), color=purple),
Polygon((vector_2v[0],0), (vector_2v[0],vector_2v[1]), color=purple)
)