Last updated
What Is a Vector?
A vector is a mathematical object with both magnitude and direction. In 2D it's represented as (x, y), in 3D as (x, y, z). The Vector Calculator performs addition, subtraction, dot product, cross product, magnitude, normalization, and projection — with step-by-step solutions.
Vector Addition
A = (3, 4, 0)
B = (1, -2, 5)
A + B = (3+1, 4+(-2), 0+5)
= (4, 2, 5)
Use case: Combining velocity vectors
Player velocity: (3, 0, 0) — moving right
Wind velocity: (1, 0, 2) — wind pushing right and forward
Resultant: (4, 0, 2) — combined movement
Vector Subtraction
A = (5, 3, 1)
B = (2, 7, 4)
A - B = (5-2, 3-7, 1-4)
= (3, -4, -3)
Use case: Direction from point B to point A
Position A: (5, 3, 1)
Position B: (2, 7, 4)
Direction B→A: A - B = (3, -4, -3)
(normalize this to get a unit direction vector)
Vector Magnitude (Length)
2D vector: v = (3, 4)
|v| = √(3² + 4²) = √(9 + 16) = √25 = 5
3D vector: v = (1, 2, 2)
|v| = √(1² + 2² + 2²) = √(1 + 4 + 4) = √9 = 3
General formula: |v| = √(x² + y² + z²)
Use cases:
Distance between two points: |B - A|
Speed from velocity vector: |velocity|
Force magnitude: |force vector|
Vector Normalization (Unit Vector)
v = (3, 4, 0)
|v| = √(9 + 16 + 0) = 5
v̂ = v / |v| = (3/5, 4/5, 0/5) = (0.6, 0.8, 0.0)
Verify: |v̂| = √(0.36 + 0.64 + 0) = √1 = 1 ✓
Use cases:
Surface normals in 3D graphics (must be unit vectors)
Direction vectors for ray casting
Normalizing feature vectors in machine learning
Dot Product (Scalar Product)
A = (1, 2, 3)
B = (4, -5, 6)
A · B = (1×4) + (2×(-5)) + (3×6)
= 4 + (-10) + 18
= 12
Angle between vectors:
cos(θ) = (A · B) / (|A| × |B|)
|A| = √(1+4+9) = √14 ≈ 3.742
|B| = √(16+25+36) = √77 ≈ 8.775
cos(θ) = 12 / (3.742 × 8.775) = 12 / 32.83 ≈ 0.3655
θ = arccos(0.3655) ≈ 68.6°
Special cases:
A · B = 0 → vectors are perpendicular (90°)
A · B > 0 → angle < 90° (same general direction)
A · B < 0 → angle > 90° (opposite general direction)
Cross Product (Vector Product)
A = (1, 2, 3)
B = (4, 5, 6)
A × B = |i j k |
|1 2 3 |
|4 5 6 |
A × B = i(2×6 - 3×5) - j(1×6 - 3×4) + k(1×5 - 2×4)
= i(12 - 15) - j(6 - 12) + k(5 - 8)
= i(-3) - j(-6) + k(-3)
= (-3, 6, -3)
Verify perpendicularity:
A · (A × B) = (1×-3) + (2×6) + (3×-3) = -3+12-9 = 0 ✓
B · (A × B) = (4×-3) + (5×6) + (6×-3) = -12+30-18 = 0 ✓
Use cases:
Surface normal for a triangle (given two edge vectors)
Torque = r × F (position × force)
Checking if two vectors are parallel (cross product = zero vector)
Vector Projection
Project vector A onto vector B:
A = (3, 4)
B = (1, 0) (unit vector along x-axis)
Scalar projection: A · B̂ = (3×1) + (4×0) = 3
Vector projection: (A · B̂) × B̂ = 3 × (1, 0) = (3, 0)
Non-unit B:
A = (3, 4)
B = (2, 1)
B̂ = B / |B| = (2, 1) / √5 = (0.894, 0.447)
Scalar proj = A · B̂ = (3×0.894) + (4×0.447) = 2.683 + 1.789 = 4.472
Vector proj = 4.472 × (0.894, 0.447) = (3.998, 1.999) ≈ (4, 2)
Use cases:
Shadow calculation in 3D graphics
Decomposing force into parallel/perpendicular components
Finding closest point on a line to a point
Vector Operations in Code
// JavaScript vector utilities
const vec3 = {
add: (a, b) => [a[0]+b[0], a[1]+b[1], a[2]+b[2]],
sub: (a, b) => [a[0]-b[0], a[1]-b[1], a[2]-b[2]],
dot: (a, b) => a[0]*b[0] + a[1]*b[1] + a[2]*b[2],
mag: (v) => Math.sqrt(v[0]**2 + v[1]**2 + v[2]**2),
norm: (v) => { const m = vec3.mag(v); return [v[0]/m, v[1]/m, v[2]/m]; },
cross: (a, b) => [
a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0]
]
};
vec3.dot([1,2,3], [4,-5,6]); // 12
vec3.mag([3,4,0]); // 5
vec3.norm([3,4,0]); // [0.6, 0.8, 0]
Common Use Cases
- 3D graphics: surface normals, ray casting, lighting calculations
- Game development: collision detection, AI direction, physics
- Physics simulation: forces, velocities, torque
- Machine learning: feature vectors, cosine similarity
- Engineering: structural analysis, fluid dynamics
- Navigation: GPS coordinates, bearing calculations
Practical Example: 3D Game Development
// Check if enemy is in front of player
const playerForward = normalize(playerDirection); // unit vector
const toEnemy = subtract(enemyPos, playerPos); // direction to enemy
const dotProduct = dot(playerForward, normalize(toEnemy));
if (dotProduct > 0) {
console.log("Enemy is in front of player");
} else {
console.log("Enemy is behind player");
}
// Calculate surface normal for lighting
const edge1 = subtract(v2, v1); // triangle edge 1
const edge2 = subtract(v3, v1); // triangle edge 2
const normal = normalize(cross(edge1, edge2)); // perpendicular to surface