Distance or Magnitude of Vectors

I’ll briefly explain vectors here.  Plenty of information out there on the web, and they are quite easy to understand.  Basically, a vector in computer graphics is an array of three numbers [X,Y,Z] that carry a direction and a magnitude.  The magnitude can mean a few different things, depending on context.  For instance, the magnitude can indicate the speed an object is traveling at, when talking about moving objects.  It can also indicate the distance from one point to another point (the components tell you which direction to travel to reach that point).

There’s a formula for calculating the magnitude of a vector, and I will show a couple ways of implementing this formula.

The basic formula is:

Given vector [X, Y, Z], the magnitude is the square root of the dot-product of the vector with itself.

√(X^2 + Y^2 + Z^2)

I won’t get into what a dot-product is here, but let’s just say that this formula works.

So, how does this give us distance?

Consider two points in space, PointA with coordinates (X, Y, Z) and PointB with coordinates (A, B, C).  To get the distance between these points we get the difference between each component and create a new vector with that information, then plug it into our magnitude formula, which will spit out a scalar number (A number with only one component, as opposed to our vector, which has three).

So, our distance vector is: [A-X, B-Y, C-Z]

The magnitude of this is: √[(A-X)^2 + (B-Y)^2 + (C-Z)^2]

Written as a dot-product, if [A-X, B-Y, C-Z] = Vector V,  the magnitude is: √(V•V)

These two diagrams also illustrate two ways of calculating distance. Since most computer programs and computing languages already have functions to calculate dot products, you’ll see you can do this quicker with less nodes/code.