MathGem:Vector Operations
From GDWiki
Contents |
[edit] 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
)
[edit] 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:
[edit] 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)); }
[edit] C++
#include <cmath> class Vector3 { public: // Members double x, y, z; // Constructors Vector3() { } Vector3( const Vector3 &r ) : x(r.x), y(r.y), z(r.z) { } Vector3( const double x, const double y, const double z ) : x(x), y(y), z(z) { } ~Vector3() { } // Methods double length() const { return std::sqrt( x * x + y * y + z * z ); } };
Note that there's a Complete math::vector Class elsewhere on the wiki that you can use instead of writing your own.
[edit] 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);} } }
[edit] 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
[edit] 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
[edit] 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.
[edit] 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; }
[edit] C++
#include <cmath> class Vector3 { public: // Members double x, y, z; // Constructors Vector3() { } Vector3( const Vector3 &r ) : x(r.x), y(r.y), z(r.z) { } Vector3( const double x, const double y, const double z ) : x(x), y(y), z(z) { } ~Vector3() { } // Operations Vector3 &operator = ( const Vector3 &r ) { x = r.x; y = r.y; z = r.z; return *this;} Vector3 operator / ( const double r ) { return Vector3( x / r, y / r, z / r ); } // Methods double length() const { return std::sqrt( x * x + y * y + z * z ); } Vector3 normalize() const { return *this / length(); } };
Note that there's a Complete math::vector Class elsewhere on the wiki that you can use instead of writing your own.
[edit] 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; } }
[edit] 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)
[edit] 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
[edit] 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.
[edit] 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); }
[edit] 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.
[edit] 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); } }
[edit] 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]
[edit] 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
[edit] 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.
[edit] 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; }
[edit] 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.
[edit] 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 ); } }
[edit] 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)
[edit] 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

