works:programmer:cpp:point3d

Подсчёт дистанции между точками в 2х и 3х мерном пространстве

#include <iostream>
#include <math.h>
 
using namespace std;
 
struct Point {
    float X;
    float Y;
};
 
struct Point3D {
    float X;
    float Y;
    float Z;
};
 
float distance(Point a, Point b) {
    return sqrt(pow(a.X - b.X, 2) + pow(a.Y - b.Y, 2));
}
 
float distance3d(Point3D a, Point3D b) {
    return sqrt(pow(a.X - b.X, 2) + pow(a.Y - b.Y, 2) + pow(a.Z - b.Z, 2));
}
 
int main()
{
    auto a2 = Point{0, 0};
    auto b2 = Point{5, 5};
    cout << "distance 2d = " << distance(a2, b2) << endl;
 
    auto a3 = Point3D{0, 0, 0};
    auto b3 = Point3D{5, 5, 5};
    cout << "distance 3d = " << distance3d(a3, b3) << endl;
 
    cout << "done!" << endl;
    return 0;
}
works/programmer/cpp/point3d.txt · Last modified: 2019/07/07 11:58 by Chugreev Eugene