Normalizing a vector can derive the direction of the vector in 2D or 3D space. A normalized vector is also known as a unit vector. We’re going to normalize and calculate the angles of a 2D vector at position (5,5) and a 3D vector at position (5,5,5).

This can come in very handy when, for example, you have a giant death robot at vector (5,5) and you need to known how many degrees you need to rotate to shoot the robot. But of course, you can also use this in many other situations.

**Graphical reprensentation of our vectors**

**Formula to normalize a vector**

Where **|v|** is the length of vector **v**. As you can see, we need to calculate the length of the vector to normalize it. The formulas to calculate the length of a vector in 2D and 3D are listed below.

**Formulas to calculate the length of a vector in 2D and 3D**

I’ve created no prescription required only canadaian. a class to represent a 2D vector and one to represent a 3D vector. They are listed below.

**2D vector header file and class**

#pragma once #include <math.h> class Vector2 { public: Vector2(void); Vector2(float X, float Y); ~Vector2(void); float Length(); Vector2 Normalize(); float X,Y; };

// Vector2.cpp #include "StdAfx.h" #include "Vector2.h" Vector2::Vector2(void) { } Vector2::Vector2(float X, float Y){ this->X = X; this->Y = Y; } // Returns the length of the vector float Vector2::Length(){ return sqrt(X * X + Y * Y); } // Normalizes the vector Vector2 Vector2::Normalize(){ Vector2 vector; float length = this->Length(); if(length != 0){ vector.X = X/length; vector.Y = Y/length; } return vector; } Vector2::~Vector2(void) { }

**3D vector header file and class**

#pragma once #include <math.h> class Vector3 { public: Vector3(void); Vector3(float X, float Y, float Z); ~Vector3(void); float Length(); Vector3 Normalize(); float X,Y,Z; };

// Vector3.cpp #include "StdAfx.h" #include "Vector3.h" Vector3::Vector3(void) { } Vector3::Vector3(float X, float Y, float Z){ this->X = X; this->Y = Y; this->Z = Z; } // Returns the length of the vector float Vector3::Length(){ return sqrt(X * X + Y * Y + Z * Z); } // Normalizes the vector Vector3 Vector3::Normalize(){ Vector3 vector; float length = this->Length(); if(length != 0){ vector.X = X/length; vector.Y = Y/length; vector.Z = Z/length; } return vector; } Vector3::~Vector3(void) { }

**Calculating angles in 2D and 3D**

I’ve used the arctan2 function to calculate the angle from the x-axis in 2D and I’ve calculated spherical coordinates for angle calculation in 3D. The formulas are listed below.

The spherical coordinate system is illustrated below.

**The main program (this is where all the action happens)**

// VectorNormalization.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Vector2.h" #include "Vector3.h" #include <iostream> #include <math.h> using namespace std; float Rad2Deg(float radians); int _tmain(int argc, _TCHAR* argv[]) { Vector2* vector2D = new Vector2(5,5); Vector3* vector3D = new Vector3(5,5,5); printf("2D vector at (%.2f,%.2f)\n",vector2D->X,vector2D->Y); printf("3D vector at (%.2f,%.2f,%.2f)\n",vector3D->X,vector3D->Y,vector3D->Z); printf("Length of 2D vector: %f\n",vector2D->Length()); printf("Length of 3D vector: %f\n",vector3D->Length()); // Normalize vectors Vector2* vector2DNormalized = &vector2D->Normalize(); Vector3* vector3DNormalized = &vector3D->Normalize(); printf("Normalized 2D vector (%f,%f)\n", vector2DNormalized->X,vector2DNormalized->Y); printf("Normalized 3D vector (%f,%f,%f)\n", vector3DNormalized->X,vector3DNormalized->Y,vector3DNormalized->Z); // Calculate angle for the 2D vector float angle = atan2(vector2DNormalized->X,vector2DNormalized->Y); angle = Rad2Deg(angle); printf("(2D) Angle from X-axis: %f\n",angle); // Calculate spherical coordinates for the 3D vector // Radius float r = vector3DNormalized->Length(); // Polar angle float theta = atan2(sqrt(pow(vector3DNormalized->X,2) +pow(vector3DNormalized->Y,2)),vector3DNormalized->Z); // Azimuthal angle float phi = atan2(vector3D->Y,vector3D->X); theta = Rad2Deg(theta); phi = Rad2Deg(phi); printf("(3D) Spherical coordinates: (%f,%f,%f)\n",r,theta,phi); system("pause"); return 0; } // Converts radians to degrees float Rad2Deg(float radians){ return radians*(180/3.141592653589793238); }

Output:

2D vector at (5.00,5.00) 3D vector at (5.00,5.00,5.00) Length of 2D vector: 7.071068 Length of 3D vector: 8.660254 Normalized 2D vector (0.707107,0.707107) Normalized 3D vector (0.577350,0.577350,0.577350) (2D) Angle from X-axis: 45.000000 (3D) Spherical coordinates: (1.000000,54.735611,45.000000) Press any key to continue . . .

On a side note, I’ve used a function for converting radians to degrees. The formula to convert radians to degrees is illustrated below.

You may download my VS 2010 project from here.

## 2 Comments

super machi

Thank you very much, this has helped me a lot!