Distance between 2D and 3D vectors in C++

This example extends the Vector2 and Vector3 classes from the 2D and 3D vector normalization and angle calculation in C++ example. The formulas for distance calculation in 2D and 3D are listed below.

Distance calculation in 2D and 3D

Header files and classes for 2D and 3D vectors

#pragma once

#include <math.h>

class Vector2
{
public:
	Vector2(void);
	Vector2(float X, float Y);
	~Vector2(void);
	float Length();
	Vector2 Normalize();
	float DistanceTo(Vector2* vector);
	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;
}

float Vector2::DistanceTo(Vector2* v){
	float distance = sqrt(pow(v->X-X,2)+pow(v->Y-Y,2));
	return distance;
}

Vector2::~Vector2(void)
{
}
#pragma once

#include <math.h>

class Vector3
{
public:
	Vector3(void);
	Vector3(float X, float Y, float Z);
	~Vector3(void);
	float Length();
	Vector3 Normalize();
	float DistanceTo(Vector3* vector);
	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;
}

float Vector3::DistanceTo(Vector3* v){
	float distance = sqrt(pow(v->X-X,2)+pow(v->Y-Y,2)+pow(v->Z-Z,2));
	return distance;
}

Vector3::~Vector3(void)
{
}

The main program

// 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);

	// Create a second 2D vector
	Vector2* distanceVector2D = new Vector2(10,10);

	// Calculate the distance between the vectors
	float distance2D = vector2D->DistanceTo(distanceVector2D);

	printf("(2D) Distance: %f\n",distance2D);

	// Create a second 3D vector
	Vector3* distanceVector3D = new Vector3(10,10,10);

	// Calculate the distance between the vectors
	float distance3D = vector3D->DistanceTo(distanceVector3D);

	printf("(3D) Distance: %f\n",distance3D);

	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)
(2D) Distance: 7.071068
(3D) Distance: 8.660254
Press any key to continue . . .

You may download my VS 2010 project here.

This entry was posted in C++ and tagged , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?