Skip to content

Instantly share code, notes, and snippets.

@kdmkdmkdm
Created May 1, 2012 18:09
Show Gist options
  • Select an option

  • Save kdmkdmkdm/2570146 to your computer and use it in GitHub Desktop.

Select an option

Save kdmkdmkdm/2570146 to your computer and use it in GitHub Desktop.
3D Distance
// gi3dDistance.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
float dist3(float ux, float uy, float uz, float vx, float vy, float vz);
int main()
{
int first[] = {1, 2, 3, 0, 0, 0};
cout << "Distance between (";
for(int i = 0; i <= 2; ++i)
{
if(i < 2)
{
cout << first[i] << ", ";
}
else
{
cout << first[i];
}
}
cout << ") and (";
for(int i = 3; i <= 5; ++i)
{
if(i < 5)
{
cout << first[i] << ", ";
}
else
{
cout << first[i];
}
}
cout << ") = " << dist3(1, 2, 3, 0, 0, 0) << endl;
int second[] = {1, 2, 3, 1, 2, 3};
cout << "Distance between (";
for(int i = 0; i <= 2; ++i)
{
if(i < 2)
{
cout << second[i] << ", ";
}
else
{
cout << second[i];
}
}
cout << ") and (";
for(int i = 3; i <= 5; ++i)
{
if(i < 5)
{
cout << second[i] << ", ";
}
else
{
cout << second[i];
}
}
cout << ") = " << dist3(1, 2, 3, 1, 2, 3) << endl;
int third[] = {1, 2, 3, 7, -4, 5};
cout << "Distance between (";
for(int i = 0; i <= 2; ++i)
{
if(i < 2)
{
cout << third[i] << ", ";
}
else
{
cout << third[i];
}
}
cout << ") and (";
for(int i = 3; i <= 5; ++i)
{
if(i < 5)
{
cout << third[i] << ", ";
}
else
{
cout << third[i];
}
}
cout << ") = " << dist3(1, 2, 3, 7, -4, 5) << endl;
}
float dist3(float ux, float uy, float uz, float vx, float vy, float vz)
{
float distance = sqrtf(powf ((vx - ux), 2.0f) + powf ((vy - uy), 2.0f) + powf ((vz - uz), 2.0f));
return distance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment