Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Distance between two points travelled by a boat
Next article icon

Program to calculate distance between two points in 3 D

Last Updated : 20 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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



Next Article
Distance between two points travelled by a boat

A

anamika9988
Improve
Article Tags :
  • DSA
  • Mathematical
  • School Programming
  • school-programming
Practice Tags :
  • Mathematical

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.41421 Calculate the distance between two points.We will use the distance formul
    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 [Tex]d = \sqrt{[(x_2 - x_1 )^2 +(y_2 - y_1)^2]}[/Tex]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 coordina
    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
  • Program to calculate the area between two Concentric Circles
    Given two concentric circles with radius X and Y where (X > Y). Find the area between them. You are required to find the area of the green region as shown in the following image: Examples: Input : X = 2, Y = 1 Output : 9.42478 Input : X = 4, Y = 2 Output : 37.6991 Approach:The area between the tw
    4 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
  • Minimum Distance between Two Points
    You are given an array arr[] of n distinct points in a 2D plane, where each point is represented by its (x, y) coordinates. Find the minimum Euclidean distance between two distinct points. Note: For two points A(px,qx) and B(py,qy) the distance Euclidean between them is: Distance = [Tex]\sqrt{(p_{x}
    15+ min read
  • Distance between two parallel Planes in 3-D
    You are given two planes P1: a1 * x + b1 * y + c1 * z + d1 = 0 and P2: a2 * x + b2 * y + c2 * z + d2 = 0. The task is to write a program to find distance between these two Planes. Examples : Input: a1 = 1, b1 = 2, c1 = -1, d1 = 1, a2 = 3, b2 = 6, c2 = -3, d2 = -4 Output: Distance is 0.952579344416 I
    8 min read
  • Shortest distance between a point and a circle
    Given a circle with a given radius has its centre at a particular position in the coordinate plane. In the coordinate plane, another point is given. The task is to find the shortest distance between the point and the circle.Examples: Input: x1 = 4, y1 = 6, x2 = 35, y2 = 42, r = 5 Output: 42.5079 Inp
    4 min read
  • Find distance between two nodes of a Binary Tree
    Given a Binary tree, the task is to find the distance between two keys in a binary tree, no parent pointers are given. The distance between two nodes is the minimum number of edges to be traversed to reach one node from another. The given two nodes are guaranteed to be in the binary tree and all nod
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences