In mathematics, any vector used to represent spatial direction is a direction vector. By convention, direction vectors have magnitude = 1. Given that a point vector p = [ x , y ] blends both direction and magnitude into its two numbers, this vector can be teased apart (factored) into two distinct quantities, separating direction from magnitude (length). The length of vector p, p_len, is its distance from the origin, and is obtained using Pythagorean distance:
p_len <-- sqrt ( x*x + y*y )
The direction from the origin to point p, p_dir, is computed by normalizing vector p. Once p_len is established, direction vector p_dir is obtained by scalar multiplying p by 1/p_len to obtain a unit-length vector preserving the direction of p but erasing its length:
p_dir <-- [ x/p_len , y/p_len ]
The factorization of p into direction p_dir and magnitude p_len is quite generally useful. The original vector p can be reconstituted by scalar multiplying its direction by its length:
p <-- p_len * p_dir
Visually and intuitively, 2D direction vectors are equivalent numerically to the set of points on the unit circle, and provide an alternative to using angles for representing directions. Examples of 2D direction vectors:
[ 0 , -1 ]
[ -0.7071 , 0.7071 ]
[ -0.9938 , 0.1104 ]
The appeal of 2D direction vectors (as compared to angles) is that they may be obtained directly from points without appealing to trig functions. Direction vectors may be used to represent any possible spatial direction. Examples are the run direction of a line, the direction perpendicular to a line, and when defining coordinate rotations, specifying the newXaxis you wish to adopt. From an informatics perspective, direction vectors are a more natural way to represent 2D information than angles, since the latter overcompresses 2D directional information into a 1D scalar. Software algorithms benefit from avoiding overcompressed representations, by eschewing singularities and discontinuities as exceptions needing to be handled. For example, slope squeezes the tilt of a line into a single number, but then has to appeal to infinity as a value (for slopes of vertical lines). Direction vectors distribute the information about line tilt more comfortably across two numbers using the finite number range ( -1 .. +1). Vertical lines do not have to be made into a special case, resulting in algorithmic simplification.
The utility of direction vectors grows considerably when used in 3D geometry. They are computed the same way as in 2D (additionally processing z components). 3D direction vectors are equivalent to points on the unit sphere. In 3D, they can be used to represent the orientations of planes, 3D circles, and other tilted objects, as well as the direction going from point a to point b.
Direction vectors and 2D line equations
Any line in two-dimensional Euclidean space can be described as the set of solutions to an equation of the form
where a, b, c are real numbers. The run direction of the line is normalized to unit length. The perpendicular direction to the line is ( a, b ) normalized to unit length.
Parametric equation for a line
In Euclidean space (any number of dimensions), given a point a and a nonzero vector v, a line is defined parametrically by (a+tv), where the parameter t varies between -∞ and +∞. This line has v as a direction vector.
Generative vs. Predicate Forms
The line equation a+tv is a generative form, but not a predicate form. Points may be generated along the line given values for a, t and v:
p ← a +tv
However, in order to function as a predicate, the representation must be sufficient to easily determine ( T / F ) whether any specified point p is on the line. If you substitute a known point into the above equation, it cannot be evaluated for equality because t was not supplied, only p .
Predicate form of 2D Line Equation
An example of a predicate form of the vector line equation in 2D is:
p • o == L
Here, the line is represented by two features: o and L.
o is the line's orientation, a normalized direction vector (unit vector) pointing perpendicular to its run direction.
The orientation is computed using the same two quantities dx and dy that go into computing slope m:
o ← ( dy , -dx ) norm = ( dy , -dx ) / || ( dy , -dx ) || (orientation of a 2D line)
Orientation o has the advantage of not overcompressing the information vested in dx and dy into a single scalar as slope does, avoiding the need to appeal to infinity as a value. Numerical algorithms benefit by avoiding such ill-behaved exceptions (e.g. , slope of a vertical line).
The 2nd feature of a 2D line represented this way is its location L. Intuitively and visually, L is the signed distance of the line from the origin (with positive distance increasing along direction o). Orientation must be solved before determining location. Once o is known, L can be computed given any known point p on the line:
L ← p • o (location of a 2D line)
Lines may be represented as feature pair ( o , L ) in all cases. Every line has an equivalent representation ( -o , -L ).
To determine if a point p is on the line, plug the value of p into the vector line predicate and evaluate it:
p • o == L (vector line predicate)
In computation, the predicate must tolerate finite math error by incorporating an epsilon signifying acceptable equality:
abs (p • o - L) < epsilon (finite precision vector line predicate)
The predicate form for 2D lines can be extended to higher dimensions, using coordinate rotation (by matrix rotation). The general form is:
R p == L
where R is the matrix rotation that aligns the line with an axis, and L is the invariant vector Location of points on the line under this rotation.