Nov 27, 2021 5:39 PM
Nov 15, 2025 7:27 PM

Vector operations

· Mathematics · Vector · Radford Mathematics - Vectors & Matrices · Khan Academy Linear Algebra ·

TOC

Definition

Vectors are quantities that are defined by two things:

A couple of examples of vectors are velocity and force.
In 2 dimensions, a Vector is described by two components:

Properties

All perpendicular vectors are orthogonal but not vice versa
A zero vector is orthogonal to anything including itself
The (non zero) vectors are collinear if one of them is scalar multiple of the other (same Vector of different length), the angle between collinear vectors is zero


ijk vector representation

as a sum of its components multiplied by the corresponding unit vectors i,j,k

i=(100),j=(010),k=(001)

a=a1i+a2j+a3k

Vector length

Vector length is equal to the square root of the sums of its squared coordinates

a=a12+a22++an2

and also follows that

a2=aa

Vector operations

Vector addition

c=a+b=(a1+b2a2+b2an+bn)

Vector subtraction

c=ab=a+(b)=(a1b2a2b2anbn)

The resulting vector c 's head is at the vector a 's head

Vector to scalar multiplication (vector result)

c=ba(ba1ba2ban)

Dot product (scalar product)

Defined for any dimensionality

c=ab=(a1a2an)(b1b2bn)=a1b1+a2b2++anbn

Properties

Usage

Alignment

With normalized vectors

1.0
Parallel
1.0...
0.7
0.7
0.0
Perpendicular
0.0...
-0.7
-0.7
Text is not SVG - cannot display

Cosine of the angle

With normalized vectors

C
C
cos(   ) =
cos(   ) =
C
C
a
a
b
b
a
a
b
b
Text is not SVG - cannot display

cos(C) * length(a) * length(b) == dtot(a, b); // When not normalized
dot(a, b) > cos(PI, 0.25);                    // check if the angle is less than 45 degrees
Project vector onto a line

=    +    (       )
=    +    (...
P
P
S
S
P
P
S
S
a
a
b
b
a
a
a
a
b
b
Text is not SVG - cannot display

float2 P = S + a * dot(a, b);
Project ray onto a plane

P
P
O
O
d
d
n
n
k
k
O
O
Ray origin
Ray origin
d
d
Ray direction
Ray direction
n
n
Plane normal
Plane normal
k
k
distance from the plane to the center of the coordinate space
distance from the plane to th...
P
P
Projected point
Projected point
Text is not SVG - cannot display

float t = -(dot(n, O) + k) / dot (n, d);
P = O + d * t;*
Shading

Fresnel

float fresnel = pow(1.0 - dot(viewDirectionWS, normalWS), 4.0)

Lightweight shading for low-end and mobile, cheaper than the Unity's PBR

// Geometry term
float diffuse = max(0.0, dot(normalWS, lightDirectionWS)) * (1.0 - metalness);

// Specular term
float3 halfwayWS = normalize(lightDirectionWS + viewDirectionWS);
float specular = pow(max(0.0, dot(halfwayWS, normalWS)), lerp(1.0, 64.0, gloss)) * metalness;
specular = sqrt(specular);

// Reflection of ambient light on metallic parts
float fresnel = pow(1.0 - dot(viewDirectionWS, input.normalWS), 3.0);
float3 ambientReflection = ambientColor * specularColor * fresnel * metalness;

// Combine all the lighting
float3 light = 0.0;
light += diffuse * lightColor * basemap; // Geometry term
light += sqrt(specular) * specularColor * lightColor; // Specular term
light += ambientReflection; // Ambient reflection
light += basemap.rgb * ambientColor.rgb * occlusion; // Ambient light

Cross product (vector product)

Defined only for 3D space, order dependent

c=a×b=(a2b3a3b2a3b1a1b3a1b2a2b2)

Properties

Cross product to calculate the area of a

S=a×b

S=a×b2

The triangle's area is half of the parallelogram's area (see above)

Angle between vectors

Given that a and b are not zero

A line crossing 2 points

Given 2 position vectors, and parametric tR the line L is given by:

L=a+t(ab)

the order is not important here, it can be =b+ or even +(ba)

Vectors blend (unversal for no unit vectors)

Source

Vector3 Slerp(Vector3 start, Vector3 end, float percent)
{
     // Dot product - the cosine of the angle between 2 vectors.
     float dot = Vector3.Dot(start, end);

     // Clamp it to be in the range of Acos()
     // This may be unnecessary, but floating point
     // precision can be a fickle mistress.
     Mathf.Clamp(dot, -1.0f, 1.0f);

     // Acos(dot) returns the angle between start and end,
     // And multiplying that by percent returns the angle between
     // start and the final result.
     float theta = Mathf.Acos(dot) * percent;
     Vector3 RelativeVec = end - start * dot;
     RelativeVec.Normalize();

     // Orthonormal basis
     // The final result.
     return ((start*Mathf.Cos(theta)) + (RelativeVec * Mathf.Sin(theta)));
}