Open In App

Program to calculate distance between two points

Last Updated : 28 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 : 5

Input : x1, y1 = (3, 4)
x2, y2 = (4, 3)
Output : 1.41421




Calculate the distance between two points.

We will use the distance formula derived from Pythagorean theorem. The formula for distance between two point (x1, y1) and (x2, y2) is
Distance = $\sqrt{(x2-x1)^{2} + (y2-y1)^{2}}$
We can get above formula by simply applying Pythagoras theorem

calculate distance between two points

calculate distance between two points

Below is the implementation of above idea.

Method 1: Without using the inbuilt library,

C++

#include <iostream>
#include <cmath>
 
// Function to calculate distance
double distance(double x1, double y1, double x2, double y2) {
    return std::sqrt(std::pow((x2 - x1), 2) + std::pow((y2 - y1), 2));
}
 
// Driver Code
int main() {
    std::cout << distance(3, 4, 4, 3) << std::endl;
    return 0;
}

                    

Java

// Java program for the above approach
import java.lang.Math;
 
public class GFG {
 
    // Function to calculate distance
    public static double distance(double x1, double y1,
                                  double x2, double y2)
    {
        return Math.sqrt(Math.pow((x2 - x1), 2)
                         + Math.pow((y2 - y1), 2));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println(distance(3, 4, 4, 3));
    }
}
 
// This code is contributed by Susobhan Akhuli

                    

Python3

def distance(x1, y1, x2, y2):
   
  # Calculating distance
   
  return (((x2 - x1)**2 +(y2 - y1)**2)**0.5)
 
# Drivers Code
 
print( distance(3, 4, 4, 3))

                    

C#

using System;
 
class Program {
    // Function to calculate distance
    static double Distance(double x1, double y1, double x2,
                           double y2)
    {
        // Calculate and return the Euclidean distance
        return Math.Sqrt(Math.Pow((x2 - x1), 2)
                         + Math.Pow((y2 - y1), 2));
    }
 
    // Main method
    static void Main()
    {
        // Call the Distance function with coordinates (3,
        // 4) and (4, 3)
        double result = Distance(3, 4, 4, 3);
 
        // Print the result to the console
        Console.WriteLine(result);
    }
}

                    

Javascript

// Function to calculate distance
function distance(x1, y1, x2, y2) {
    return Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
}
 
// Driver code
console.log(distance(3, 4, 4, 3));

                    

Output
1.41421

Time Complexity: O(1)
Auxiliary Space: O(1)

Method 2: Using the inbuilt library,

C++

#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate distance
float distance(int x1, int y1, int x2, int y2)
{
    // Calculating distance
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);
}
 
// Drivers Code
int main()
{
    cout << distance(3, 4, 4, 3);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

                    

C

#include <math.h>
#include <stdio.h>
 
// Function to calculate distance
float distance(int x1, int y1, int x2, int y2)
{
    // Calculating distance
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);
}
 
// Drivers Code
int main()
{
    printf("%f", distance(3, 4, 4, 3));
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

                    

Java

// Java code to compute distance
 
class GFG {
    // Function to calculate distance
    static double distance(int x1, int y1, int x2, int y2)
    {
        // Calculating distance
        return Math.sqrt(Math.pow(x2 - x1, 2)
                         + Math.pow(y2 - y1, 2) * 1.0);
    }
    // Driver code
    public static void main(String[] args)
    {
        System.out.println(
            Math.round(distance(3, 4, 4, 3) * 100000.0)
            / 100000.0);
    }
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

                    

Python3

# Python3 program to calculate
# distance between two points
 
import math
 
# Function to calculate distance
def distance(x1 , y1 , x2 , y2):
 
    # Calculating distance
    return math.sqrt(math.pow(x2 - x1, 2) +
                math.pow(y2 - y1, 2) * 1.0)
 
# Drivers Code
print("%.6f"%distance(3, 4, 4, 3))
 
# This code is contributed by "Sharad_Bhardwaj".

                    

C#

// C# code to compute distance
using System;
 
class GFG
{
    // Function to calculate distance
    static double distance(int x1, int y1, int x2, int y2)
    {
        // Calculating distance
        return Math.Sqrt(Math.Pow(x2 - x1, 2) +
                      Math.Pow(y2 - y1, 2) * 1.0);
    }
     
    // Driver code
    public static void Main ()
    {
        Console.WriteLine(Math.Round(distance(3, 4, 4, 3)
                                   * 100000.0)/100000.0);
    }
}
 
// This code is contributed by
// vt_m.

                    

Javascript

<script>
 
// Function to calculate distance
function distance(x1, y1, x2,  y2)
{
    // Calculating distance
    return Math.sqrt(Math.pow(x2 - x1, 2) +
                Math.pow(y2 - y1, 2) * 1.0);
}
 
// Drivers Code
document.write(distance(3, 4, 4, 3));
 
// This code is contributed by noob2000.
</script>

                    

PHP

<?php
// PHP code to compute distance
 
// Function to calculate distance
function distance($x1, $y1, $x2, $y2)
{
     
    // Calculating distance
    return sqrt(pow($x2 - $x1, 2) +
                pow($y2 - $y1, 2) * 1.0);
}
 
// Driver Code
echo(distance(3, 4, 4, 3));
 
// This code is contributed by Ajit.
?>

                    

Output
1.41421

Time Complexity: O(1)
Auxiliary Space: O(1)



Similar Reads

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 (x
5 min read
Minimize the maximum distance between adjacent points after adding K points anywhere in between
Given an array arr[] of N integers representing the position of N points along a straight line and an integer K, the task is to find the minimum value of the maximum distance between adjacent points after adding K points anywhere in between, not necessarily on an integer position. Examples: Input: arr[] = {2, 4, 8, 10}, K = 1Output: 2Explanation: A
9 min read
How to calculate the distance between Two Points?
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 used to compute the distance between two points in coordinate geometry. Let's look at the formula for calculating the distance between two points in a two-dimensional or three-dimen
6 min read
Ways to choose three points with distance between the most distant points &lt;= L
Given a set of n distinct points x1, x2, x3... xn all lying on the X-axis and an integer L, the task is to find the number of ways of selecting three points such that the distance between the most distant points is less than or equal to L Note: Order is not important i.e the points {3, 2, 1} and {1, 2, 3} represent the same set of three points Exam
16 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 Kilometers Problem can be solved using Haversine fo
7 min read
C program to find the Euclidean distance between two points
Given four integers x1, y1, x2 and y2, which represents two coordinates (x1, y1) and (x2, y2) of a two-dimensional graph. The task is to find the Euclidean distance between these two points. Euclidean distance between two points is the length of a straight line drawn between those two given points. Examples:  Input: x1, y1 = (3, 4)            x2, y
2 min read
Number of Integral Points between Two Points
Given two points p (x1, y1) and q (x2, y2), calculate the number of integral points lying on the line joining them. Example: If points are (0, 2) and (4, 0), then the number of integral points lying on it is only one and that is (2, 1). Similarly, if points are (1, 9) and (8, 16), the integral points lying on it are 6 and they are (2, 10), (3, 11),
8 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 = 2.1 Formula for distance is D = T*(B^2 - S^2)/ (
4 min read
Haversine formula to find distance between two points on a sphere
The Haversine formula calculates the shortest distance between two points on a sphere using their latitudes and longitudes measured along the surface. It is important for use in navigation. The haversine can be expressed in trigonometric function as: [Tex]haversine(\theta)=sin^2\Big(\frac{\theta}{2}\Big) [/Tex]The haversine of the central angle (wh
5 min read
Check whether it is possible to join two points given on circle such that distance between them is k
Given two circles and a length, K. Find whether we can join two points (one on perimeter of each circle), so that distance between the points is K. (Coordinates of both points need not be an integer value). Examples: Input: Circle-1 Center (0, 0) Radius = 5 Circle-2 Center (8, 3) Radius = 2 K = 3 Output: Yes Maximum Distance: 15 Minimum Distance: 2
10 min read
Practice Tags :