Question #174675

Make two 3D-points in a 3-Dimensional space. Calculate the distance between these two points. Instead of integer data type, use any other datatype for x,y,z values. Create an object pt, and call distance function as pt.Distance(pt2), which will calculate the distance between two 3-D points.



Expert's answer

#include <iostream>
#include <cmath>

struct Point {
    float x, y, z;

    Point(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}

    float Distance(Point p) const {
        float sum = (p.x - x) * (p.x - x) + (p.y - y) * (p.y - y) + (p.z - z) * (p.z - z);;
        return sqrtf(sum);
    }
};
int main() {
    Point   pt(0.f0.f0.f),
            pt2(2.f2.f2.f);
    std::cout << pt.Distance(pt2);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS