Program to calculate distance between two points in 3 D
Last Updated :
20 Aug, 2022
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 = $\sqrt{(x2-x1)^{2} + (y2-y1)^{2} + (z2-z1)^{2}}$
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.
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.
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.
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 = -5
z1 = 7
x2 = 3
y2 = 4
z2 = 5
# function call for distance
distance(x1, y1, z1, x2, y2, z2)
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.
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.
?>
JavaScript
<script>
// javascript program to find
// distance between
// two points in 3 D.
// Function for distance
function distance(x1 , y1 , z1 , x2 , y2 , z2) {
var d = Math.pow((Math.pow(x2 - x1, 2) +
Math.pow(y2 - y1, 2) +
Math.pow(z2 - z1, 2) * 1.0), 0.5);
document.write("Distance is " + d.toFixed(10));
return;
}
// Driver code
var x1 = 2;
var y1 = -5;
var z1 = 7;
var x2 = 3;
var y2 = 4;
var z2 = 5;
// function call
// for distance
distance(x1, y1, z1, x2, y2, z2);
// This code contributed by aashish1995
</script>
Output: Distance is
9.2736184955
Time complexity: O(logn) as the inbuilt pow and sqrt function takes logarithmic time to complete all the operations hence the overall time taken by the algorithm is logarithmic.
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant
Similar Reads
Program to calculate distance between two points You are given two coordinates (x1, y1) and (x2, y2) of a two-dimensional graph. Find the distance between them.Examples: Input : x1, y1 = (3, 4) x2, y2 = (7, 7)Output : 5Input : x1, y1 = (3, 4) x2, y2 = (4, 3)Output : 1.41421Calculate the distance between two points.We will use the distance formula
2 min read
How to Calculate the Distance Between Two Points? Answer: To calculate the distance between Two Points, Distance Formula is used, which is d = \sqrt{[(x_2 - x_1 )^2 +(y_2 - y_1)^2]}The length of the line segment connecting two points is defined as the distance between them. The length of the line segment connecting the specified coordinates can be
6 min read
Program for distance between two points on earth Given latitude and longitude in degrees find the distance between two points on the earth. Image Source : WikipediaExamples: Input : Latitude 1: 53.32055555555556 Latitude 2: 53.31861111111111 Longitude 1: -1.7297222222222221 Longitude 2: -1.6997222222222223 Output: Distance is: 2.0043678382716137 K
7 min read
Distance between two points travelled by a boat Write a program to determine the distance(D) between two points traveled by a boat, given the speed of boat in still water(B), the speed of the stream(S), the time taken to row a place and come back i.e T. Examples: Input : B = 5, S = 1, T = 1 Output : D = 2.4 Input : B = 5, S = 2, T = 1 Output : D
4 min read
Calculate the Manhattan Distance between two cells of given 2D array Given a 2D array of size M * N and two points in the form (X1, Y1) and (X2 , Y2) where X1 and X2 represents the rows and Y1 and Y2 represents the column. The task is to calculate the Manhattan distance between the given points. Examples: Input: M = 5, N = 5, X1 = 1, Y1 = 2, X2 = 3, Y2 = 3Output: 3Ex
4 min read