Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active November 16, 2023 11:44
Show Gist options
  • Save juanfal/4b55b90e61369616e051b7d41d727be6 to your computer and use it in GitHub Desktop.
Save juanfal/4b55b90e61369616e051b7d41d727be6 to your computer and use it in GitHub Desktop.
distance between 2 points
// t9e11.pointsDistance.cpp
// juanfc 2023-11-14
// https://gist.github.com/juanfal/4b55b90e61369616e051b7d41d727be6
#include <iostream>
#include <cmath>
#include <array>
using namespace std;
// consts
const int N=3;
// types
typedef array<float,N> TPos;
// prototypes
void writeArr(TPos a);
float distance(TPos a, TPos b);
int main()
{
TPos a = {{0, 0, 1}},
b = {{0, 1, 0}};
writeArr(a); cout << endl;
writeArr(b); cout << endl;
cout << "Distance: "
<< distance(a, b) << endl; // 1
return 0;
}
void swap(int& a, int& b);
float distance(TPos a, TPos b)
{
int d = 0;
for (int i = 0; i < N; ++i)
d += (b[i]-a[i])*(b[i]-a[i]);
return sqrt(d);
}
void writeArr(TPos a)
{
for (int i = 0; i < N; ++i)
cout << a[i] << " ";
}
void swap(int& a, int& b)
{
int t = a;
a = b; b = t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment