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.
#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;
}
Comments
Leave a comment