Answer to Question #174675 in C++ for zain ul abdeen

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.



1
Expert's answer
2021-03-28T04:09:15-0400
#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.f, 0.f, 0.f),
            pt2(2.f, 2.f, 2.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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS