Last active
November 16, 2023 11:44
-
-
Save juanfal/4b55b90e61369616e051b7d41d727be6 to your computer and use it in GitHub Desktop.
distance between 2 points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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