Program to calculate distance between two points
Last Updated :
28 Feb, 2024
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 =Â
We can get above formula by simply applying Pythagoras theorem
calculate distance between two points
Below is the implementation of above idea.
Method 1: Without using the inbuilt library,
C++
#include <iostream>
#include <cmath>
double distance( double x1, double y1, double x2, double y2) {
return std:: sqrt (std:: pow ((x2 - x1), 2) + std:: pow ((y2 - y1), 2));
}
int main() {
std::cout << distance(3, 4, 4, 3) << std::endl;
return 0;
}
|
Java
import java.lang.Math;
public class GFG {
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 ));
}
public static void main(String[] args)
{
System.out.println(distance( 3 , 4 , 4 , 3 ));
}
}
|
Python3
def distance(x1, y1, x2, y2):
return (((x2 - x1) * * 2 + (y2 - y1) * * 2 ) * * 0.5 )
print ( distance( 3 , 4 , 4 , 3 ))
|
C#
using System;
class Program {
static double Distance( double x1, double y1, double x2,
double y2)
{
return Math.Sqrt(Math.Pow((x2 - x1), 2)
+ Math.Pow((y2 - y1), 2));
}
static void Main()
{
double result = Distance(3, 4, 4, 3);
Console.WriteLine(result);
}
}
|
Javascript
function distance(x1, y1, x2, y2) {
return Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
}
console.log(distance(3, 4, 4, 3));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 2: Using the inbuilt library,
C++
#include <bits/stdc++.h>
using namespace std;
float distance( int x1, int y1, int x2, int y2)
{
return sqrt ( pow (x2 - x1, 2) + pow (y2 - y1, 2) * 1.0);
}
int main()
{
cout << distance(3, 4, 4, 3);
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
float distance( int x1, int y1, int x2, int y2)
{
return sqrt ( pow (x2 - x1, 2) + pow (y2 - y1, 2) * 1.0);
}
int main()
{
printf ( "%f" , distance(3, 4, 4, 3));
return 0;
}
|
Java
class GFG {
static double distance( int x1, int y1, int x2, int y2)
{
return Math.sqrt(Math.pow(x2 - x1, 2 )
+ Math.pow(y2 - y1, 2 ) * 1.0 );
}
public static void main(String[] args)
{
System.out.println(
Math.round(distance( 3 , 4 , 4 , 3 ) * 100000.0 )
/ 100000.0 );
}
}
|
Python3
import math
def distance(x1 , y1 , x2 , y2):
return math.sqrt(math. pow (x2 - x1, 2 ) +
math. pow (y2 - y1, 2 ) * 1.0 )
print ( "%.6f" % distance( 3 , 4 , 4 , 3 ))
|
C#
using System;
class GFG
{
static double distance( int x1, int y1, int x2, int y2)
{
return Math.Sqrt(Math.Pow(x2 - x1, 2) +
Math.Pow(y2 - y1, 2) * 1.0);
}
public static void Main ()
{
Console.WriteLine(Math.Round(distance(3, 4, 4, 3)
* 100000.0)/100000.0);
}
}
|
Javascript
<script>
function distance(x1, y1, x2, y2)
{
return Math.sqrt(Math.pow(x2 - x1, 2) +
Math.pow(y2 - y1, 2) * 1.0);
}
document.write(distance(3, 4, 4, 3));
</script>
|
PHP
<?php
function distance( $x1 , $y1 , $x2 , $y2 )
{
return sqrt(pow( $x2 - $x1 , 2) +
pow( $y2 - $y1 , 2) * 1.0);
}
echo (distance(3, 4, 4, 3));
?>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...