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 <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <math.h>
using namespace std;
// function to print distance
void distance(float x1, float y1,
float z1, float x2,
float y2, float z2)
{
float d = sqrt(pow(x2 - x1, 2) +
pow(y2 - y1, 2) +
pow(z2 - z1, 2) * 1.0);
std::cout << std::fixed;
std::cout << std::setprecision(2);
cout << " Distance is " << d;
return;
}
// Driver Code
int main()
{
float x1 = 2;
float y1 = -5;
float z1 = 7;
float x2 = 3;
float y2 = 4;
float z2 = 5;
// function call for distance
distance(x1, y1, z1,
x2, y2, z2);
return 0;
}
Comments
Leave a comment