Geometry

November 12, 2022

Basic Vector Operations

Magnitude

The magnitude or length of a vector is calculated by multiplying each component by itself, summing all together, and then taking the square root. It is basically the Pythagorean theorem in n dimensions.

// Example for a Vec2D a(x,y);
float x = 4.0f;
float y = 2.0f;

float length = sqrt(a.x * a.x + a.y * a.y);

printf("The length of vector a is: %f.\n", length);
// Output: The length of my_vector is: 4.472136.

We place two vertical bars on each side to express the magnitude of a vector:

$\lVert a \rVert = \sqrt{x^2+y^2}$

Since each term is a squared quantity the magnitude can never be negative.

Vector Multiplication

Dot Product

The dot product or scalar product is a scalar quantity. Each component of the two vectors are multiplied together and summed.

Here an example for a 2D vector:

$a \cdot b = a_x * b_x + a_y * b_y$

Projections

The dot product of vector a with unit vector u is the projection of a in the direction of u.

$a \cdot u$

In other words it is the amount that a is pointing in the direction of u.

This calculation is used to get the length of the ray segment when intersecting a ray with a sphere.