Program to calculate distance between two points in 3 D
Given two coordinates (x1, y1, z1) and (x2, y2, z2) in 3 dimension. The task is to find the distance between them.
Examples :
Input: x1, y1, z1 = (2, -5, 7)
x2, y2, z1 = (3, 4, 5)
Output: 9.2736184955
Input: x1, y1, z1 = (0, 0, 0)
x2, y2, z1 = (1, 1, 1)
Output: 1.73205080757
Approach: The formula for distance between two points in 3 dimension i.e (x1, y1, z1) and (x2, y2, z2) has been derived from Pythagorean theorem which is:
Distance = ![]()
Below is the implementation of above formulae:
C++
// C++ program to find // distance between // two points in 3 D. #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; } // This code is contributed // by Amber_Saxena. |
chevron_right
filter_none
C
// C program to find // distance between // two points in 3 D. #include <stdio.h> #include<math.h> // 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); printf("Distance is %f", 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; } // This code is contributed // by Amber_Saxena. |
chevron_right
filter_none
Java
// Java program to find // distance between // two points in 3 D. import java .io.*; import java.lang.Math; class GFG { // Function for // distance static void distance(float x1, float y1, float z1, float x2, float y2, float z2) { double d = Math.pow((Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2) + Math.pow(z2 - z1, 2) * 1.0), 0.5); System.out.println("Distance is "+ d); return; } // Driver code public static void main(String[] args) { 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); } } // This code is contributed // by Amber_Saxena. |
chevron_right
filter_none
Python
# Python program to find distance between # two points in 3 D. import math # Function to find distance def distance(x1, y1, z1, x2, y2, z2): d = math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2) + math.pow(z2 - z1, 2)* 1.0) print("Distance is ") print(d) # Driver Code x1 = 2 y1 = -5z1 = 7x2 = 3y2 = 4z2 = 5 # function call for distance distance(x1, y1, z1, x2, y2, z2) |
chevron_right
filter_none
C#
// C# program to find // distance between // two points in 3 D. using System; class GFG { // Function for // distance static void distance(float x1, float y1, float z1, float x2, float y2, float z2) { double d = Math.Pow((Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2) + Math.Pow(z2 - z1, 2) * 1.0), 0.5); Console.WriteLine("Distance is \n" + d); return; } // Driver code public static void 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); } } // This code is contributed // by chandan_jnu. |
chevron_right
filter_none
PHP
<?php // PHP program to find // distance between // two points in 3 D. // function to print distance function distance($x1, $y1, $z1, $x2, $y2, $z2) { $d = sqrt(pow($x2 - $x1, 2) + pow($y2 - $y1, 2) + pow($z2 - $z1, 2) * 1.0); echo "Distance is ". $d; } // Driver Code $x1 = 2; $y1 = -5; $z1 = 7; $x2 = 3; $y2 = 4; $z2 = 5; // function call for distance distance($x1, $y1, $z1, $x2, $y2, $z2); // This code is contributed // by Mahadev. ?> |
chevron_right
filter_none
Output:
Distance is 9.2736184955
Recommended Posts:
- Program to calculate distance between two points
- Program for distance between two points on earth
- Distance between two points travelled by a boat
- Hammered distance between N points in a 2-D plane
- Calculate speed, distance and time
- Find points at a given distance on a line of given slope
- Check whether it is possible to join two points given on circle such that distance between them is k
- Find the maximum possible distance from origin using given points
- Haversine formula to find distance between two points on a sphere
- Program to calculate age
- Program to calculate value of nCr
- C program to calculate the value of nPr
- Program to calculate the value of nCr Efficiently
- Program to Calculate e^x by Recursion
- Program to calculate the value of sin(x) and cos(x) using Expansion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



