# MathGem:Vector Operations

## Symbols

A brief overview of mathematical symbols relevant to this page.

- common generic vectors

- visual representation of a vector

- generic unit vectors (length = 1)

- length of a vector

- angle between two vectors

- dot product (of and )

- cross product (of and )

## Length Of A Vector

A simple yet powerful operation, lengths of a vector are used in a variety of the more complex vector matrix operations. Also, they can be used to determine the distance between two objects, by creating a vector from the subtraction of the two objects' coordinates and then taking the length of that vector:

### C

```
```

```
```
- include <math.h>

typedef struct Vector3_
{

double x;
double y;
double z;

} Vector3;

double length(Vector3 *v)
{

return (sqrt(v->x*v->x + v->y*v->y + v->z*v->z));

```
}
```

### C++

```
```

```
```
- include <cmath>

struct Vector3
{

double x;
double y;
double z;

};

double length(Vector3 const &v)
{

return (std::sqrt(v.x*v.x + v.y*v.y + v.z*v.z));

```
}
```

Note that there's a Complete math::vector Class elsewhere on the wiki that you can use instead of writing your own.

### C#

```
public class Vector3
{
```

```
``` public double X;
public double Y;
public double Z;

public double Length
{
get{return System.Math.Sqrt(X*X + Y*Y + Z*Z);}
}

```
}
```

### Python

```
```

```
```
- v is a tuple representing a 3d vector

def length(v):

return (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]) ** 0.5

```
```

### Ruby

```
```

```
```
- v is a Vector (see http://www.ruby-doc.org/core/classes/Vector.html)

def length(v)

Math.sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2]) )

```
end
```

### Visual Basic

```
' In Public Object Module:
Public Type Vector3
```

```
``` X As Double
Y As Double
Z As Double

End Type

Public Function LENGTH(v as Vector3) As Double

LENGTH = (v.x^2 + v.y^2 + v.z^2) ^ .5

```
End Function
```

### PureBasic

```
Structure Vector
```

```
```x.f
y.f
z.f

EndStructure

Procedure.f Length(*v.vector) ;Return a decimal value

ProcedureReturn Sqr(Pow(*v\x,2) + Pow(*v\y,2) + Pow(*v\z,2))

```
EndProcedure
```

## Normalize

Normalizing a vector forms the basis of many more advanced vector operations. The process takes a vector of any length and preserves only its direction information (gives it a length of 1). This is useful when projecting one matrix into the axis of another with a dot product. Mathematically, a normalized vector is called a unit vector and can be obtained easily by dividing the vector by it's length: . In practice, however, each component (X, Y, and Z) of the vector is divided by it's length seperately.

### C

```
typedef struct Vector3_
{
```

```
``` double x;
double y;
double z;

} Vector3;

void Normalize(Vector3 *v)
{

double len = length(v);
v->x /= len;
v->y /= len;
v->z /= len;

```
}
```

### C++

```
struct Vector3
{
```

```
``` double x;
double y;
double z;

};

void Normalize(Vector3 &v)
{

double len = length(v);
v.x /= len;
v.y /= len;
v.z /= len;

```
}
```

Note that there's a Complete math::vector Class elsewhere on the wiki that you can use instead of writing your own.

### C#

```
public class Vector3
{
```

```
``` public double X;
public double Y;
public double Z;

public void Normalize()
{
// Vector3.Length property is under length section
double length = this.Length;

X /= length;
Y /= length;
Z /= length;
}

```
}
```

### Ruby

```
```

```
```
- v is a Vector (see http://www.ruby-doc.org/core/classes/Vector.html)

def normalize( v )

len = length( v )
return Vector[v[0]/len,v[1]/len,v[2]/len]

```
end
```

### Python

```
```

```
```
- v is a tuple representing a 3d vector

def normalize(v):

len = length(v);
return (v[0] / len, v[1] / len, v[2] / len)

```
```

### Visual Basic

```
' In Public Object Module:
Public Type Vector3
```

```
``` X As Double
Y As Double
Z As Double

End Type

Public Function NORMALIZE(ByRef v as Vector3)

Dim VectorLen As Double
VectorLen = LENGTH(v)
v.X = v.X / VectorLen
v.Y = v.Y / VectorLen
v.Z = v.Z / VectorLen

```
End Function
```

### PureBasic

```
Structure Vector
```

```
```x.f
y.f
z.f

EndStructure

Procedure.f Normalize(*v.Vector)

Dist.f = Length(*v)
*v\x / Dist
*v\y / Dist
*v\z / Dist

```
EndProcedure
```

## The Dot Product

The dot product is very useful in game programming as it gives the angle between two vectors: where is the angle between vectors and . On its own, the dot product is the length of the projection of onto the unit vector when the two vectors are placed so that their tails coincide.

### C

```
typedef struct Vector3_
{
```

```
``` double x;
double y;
double z;

} Vector3;

double dot(Vector3 *v, Vector3 *w)
{

return (v->x*w->x + v->y*w->y + v->z*w->z);

```
}
```

### C++

```
struct Vector3
{
```

```
``` double x;
double y;
double z;

};

double dot(Vector3 const &v, Vector3 const &w)
{

return (v.x*w.x + v.y*w.y + v.z*w.z);

```
}
```

Note that there's a Complete math::vector Class elsewhere on the wiki that you can use instead of writing your own.

### C#

```
public class Vector3
{
```

```
``` public double X;
public double Y;
public double Z;

public static double Dot(Vector3 v, Vector3 w)
{
return (v.X*w.X + v.Y*w.Y + v.Z*w.Z);
}

```
}
```

### Ruby

```
```

```
```
- v and w are Vectors (see http://www.ruby-doc.org/core/classes/Vector.html)

def dot( v, w )

v[0]*w[0] + v[1]*w[1] + v[2]*w[2]

```
```

### Python

```
```

```
```
- v and w are tuples representing 3d vectors

def dot(v, w):

return v[0]*w[0] + v[1]*w[1] + v[2]*w[2]

```
```

### Visual Basic

```
' In Public Object Module:
Public Type Vector3
```

```
``` X As Double
Y As Double
Z As Double

End Type

Public Function DOT(v as Vector3, w as Vector3) As Double

DOT = v.x*w.x + v.y*w.y + v.z*w.z

```
End Function
```

### PureBasic

```
Structure Vector
```

```
```x.f
y.f
z.f

EndStructure

Procedure Dot(*v.Vector,*w.Vector)

ProcedureReturn (*v\x * *w\x + *v\y * *w\y + *v\z * *w\z)

```
EndProcedure
```

## The Cross Product

The cross product is a mathematical operation that will return a resultant vector that is orthogonal to the two vectors used to calculate it and has a length relative to the lengths of the vectors and the angles between them:

or (in terms of dot product):

Visually, the orientation of a cross product resultant can be seen here:

The cross product becomes very useful when calculating normals (vector orthogonal to a surface), which are used extensively in 3D game programming.

### C

```
typedef struct Vector3_
{
```

```
``` double x;
double y;
double z;

} Vector3;

Vector3 cross(Vector3 *v, Vector3 *w)
{

Vector3 c = {
v.y*w.z - v.z*w.y,
v.z*w.x - v.x*w.z,
v.x*w.y - v.y*w.x };
return c;

```
}
```

### C++

```
struct Vector3
{
```

```
``` double x, y, z;
Vector3(double set_x, double set_y, double set_z):
x(set_x), y(set_y), z(set_z)
{}

};

Vector3 cross(Vector3 const &v, Vector3 const &w)
{

return Vector3(
v.y*w.z - v.z*w.y,
v.z*w.x - v.x*w.z,
v.x*w.y - v.y*w.x );

```
}
```

Note that there's a Complete math::vector Class elsewhere on the wiki that you can use instead of writing your own.

### C#

```
public class Vector3
{
```

```
``` public double X;
public double Y;
public double Z;

public static Vector3 Cross(Vector3 v, Vector3 w)
{
return new Vector3(
v.Y*w.Z - v.Z*w.Y,
v.Z*w.X - v.X*w.Z,
v.X*w.Y - v.Y*w.X );
}

```
}
```

### Ruby

```
```

```
```
- v is a Vector (see http://www.ruby-doc.org/core/classes/Vector.html)

def cross( v, w )

x = v[1]*w[2] - v[2]*w[1]
y = v[2]*w[0] - v[0]*w[2]
z = v[0]*w[1] - v[1]*w[0]
Vector[x,y,z]

```
end
```

### Python

```
```

```
```
- v and w are tuples representing 3d vectors

def cross(v, w):

x = v[1]*w[2] - v[2]*w[1]
y = v[2]*w[0] - v[0]*w[2]
z = v[0]*w[1] - v[1]*w[0]

return (x, y, z)

```
```

### Visual Basic

```
' In Public Object Module:
Public Type Vector3
```

```
``` X As Double
Y As Double
Z As Double

End Type

Public Function CROSS(v as Vector3, w as Vector3) As Vector3

CROSS.X = v.Y*w.Z - v.Z*w.Y
CROSS.Y = v.Z*w.X - v.X*w.Z
CROSS.Z = v.X*w.Y - v.Y*w.X

```
End Function
```

Back To Programming Techniques