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 : 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 = [Tex]$\sqrt + (y2-y1)^>$ [/Tex]
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++

// 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 std::cout << distance(3, 4, 4, 3) << std::endl;

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, // 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, 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

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

Method 2: Using the inbuilt library,

C++

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 cout << distance(3, 4, 4, 3); // This code is contributed by Aditya Kumar (adityakumar129)

C

// 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 printf ( "%f" , distance(3, 4, 4, 3)); // This code is contributed by Aditya Kumar (adityakumar129)

Java

// Java code to compute distance // 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 ) // 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; // 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

Javascript

// 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.

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

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

Like Article -->

Please Login to comment.

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?

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 coordinates can be used to compute the distance between tw

6 min read Ways to choose three points with distance between the most distant points <= 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 represent the same set of three points Exam

15+ 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 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. Input: x1 = 2, y1 = 2, x2 = 5, y2 = 5 Output: 2 Explanation: There are only 2 integral points on the line joining (2,2) and (5,5). The points are (3,3) and (4,4). Input: x1 = 1, y1 = 9, x2 = 8, y2 = 16 Output: 6 Explanation: There are

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\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 Maximum distance between two points in coordinate plane using Rotating Caliper's Method

Prerequisites: Graham Scan's Convex Hull, Orientation.Given a set of N points in a coordinates plane, the task is to find the maximum distance between any two points in the given set of planes. Examples: Input: n = 4, Points: (0, 3), (3, 0), (0, 0), (1, 1) Output: Maximum Distance = 4.24264 Explanation: Points having maximum distance between them a

15+ 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: 3Explanation: As per the definition, the Manhattan th

4 min read Prime points (Points that split a number into two primes)

Given a n-digit number. Prime point is the index of the digit whose left and right side numbers are prime. Print all the prime points of the number. If no prime point exists print -1. Examples: Input : 2317 Output : 1 2 Explanation : Left and right side numbers of index point 1 are 2 and 17 respectively and both are primes. Left and right side numb

10 min read Count of obtuse angles in a circle with 'k' equidistant points between 2 given points

A circle is given with k equidistant points on its circumference. 2 points A and B are given in the circle. Find the count of all obtuse angles (angles larger than 90 degree) formed from /_ACB, where C can be any point in circle other than A or B. Note : A and B are not equal. A < B. Points are between 1 and K(both inclusive). Examples : Input :

5 min read Hammered distance between N points in a 2-D plane

Given n number of point in 2-d plane followed by Xi, Yi describing n points. The task is to calculate the hammered distance of n points. Note: Hammered distance is the sum of the square of the shortest distance between every pair of the point. Examples: Input: n = 30 10 01 0Output: 4 Input: n = 41 02 03 04 0Output: 20 Basic Approach:As we have to f

10 min read Distance between end points of Hour and minute hand at given time

Given four integers H, M, L1, and L2, which denotes the time as an hour and minutes in a Clock of 12-Hour format and L1 and L2 denotes the length of the hour hand and minute hand respectively. The task is to find the distance between the endpoints of the hour and minutes hand.Examples: Input: H = 3, M = 30, L1 = 3, L2 = 4 Output: 4.33499 Explanatio

11 min read Count pairs of points having distance between them equal to integral values in a K-dimensional space

Given an array points[] representing N points in a K-dimensional space, the task is to find the count of pairs of points in the space such that the distance between the points of each pair is an integer value. Examples: Input: points[] = < <1, 2>, , >, K = 2Output: 1Explanation: Distance between points of the pair(points[0], points[1])

7 min read Find 4 points with equal Manhattan distance between any pair

Given an integer N, Find 4 points in a 2D plane having integral coordinates, such that the Manhattan distance between any pair of points is equal to N. Examples: Input: N = 6Output: < <0, -3>, , , <0, 3>>Explanation: It can be easily calculated that Manhattan distance between all possible pairs is 6. Input: N = 11Output: -1Explanation

7 min read Distance of chord from center when distance between center and another equal length chord is given

Given two equal length chords of a circle and Distance between the centre and one chord. The task is here to find the distance between the centre and the other chord. Examples: Input: 48 Output: 48 Input: 82 Output: 82 Below is the implementation of the above approach:Approach: Let AB & CD be the two equal chords of the circle having center at

3 min read Generate string with Hamming Distance as half of the hamming distance between strings A and B

Given two Binary string A and B of length N, the task is to find the Binary string whose Hamming Distance to strings A and B is half the Hamming Distance of A and B.Examples: Input: A = "1001010", B = "0101010" Output: 0001010 Explanation: The hamming distance of the string A and B is 2. The hamming distance of the output string to A is 1. The hamm

7 min read Steps required to visit M points in order on a circular ring of N points

Given an integer 'n', consider a circular ring containing 'n' points numbered from '1' to 'n' such that you can move in the following way : 1 -> 2 -> 3 -> . -> n -> 1 -> 2 -> 3 -> . Also, given an array of integers (of size 'm'), the task is to find the number of steps it'll take to get to every point in the array i

5 min read Find the point on X-axis from given N points having least Sum of Distances from all other points

Given an array arr[] consisting of N integers, denoting N points lying on the X-axis, the task is to find the point which has the least sum of distances from all other points. Example: Input: arr[] = <4, 1, 5, 10, 2>Output: (4, 0) Explanation: Distance of 4 from rest of the elements = |4 - 1| + |4 - 5| + |4 - 10| + |4 - 2| = 12 Distance of 1 from

11 min read Minimum number of points to be removed to get remaining points on one side of axis

We are given n points in a Cartesian plane. Our task is to find the minimum number of points that should be removed in order to get the remaining points on one side of any axis. Examples : Input : 4 1 1 2 2 -1 -1 -2 2 Output : 1 Explanation : If we remove (-1, -1) then all the remaining points are above x-axis. Thus the answer is 1. Input : 3 1 10

6 min read Pick points from array such that minimum distance is maximized

Given C magnets and an array arr[] representing N index positions where C ? N. The task is to place these magnets at these available indices in such a way that the distance between the two nearest magnets is as large as possible.Examples: Input: C = 4, arr[] = <1, 2, 5, 8, 10, 18>Output: 4 We can place 4 magnets to positions 1, 5, 10, 18 so maximu

11 min read Find the maximum possible distance from origin using given points

Given N 2-Dimensional points. The task is to find the maximum possible distance from the origin using given points. Using the ith point (xi, yi) one can move from (a, b) to (a + xi, b + yi). Note: N lies between 1 to 1000 and each point can be used at most once.Examples: Input: arr[][] = <<1, 1>, , , > Output: 14.14 The farthest p 6 min read Find the integer points (x, y) with Manhattan distance atleast N

Given a number N, the task is to find the integer points (x, y) such that 0 <= x, y <= N and Manhattan distance between any two points will be atleast N.Examples: Input: N = 3 Output: (0, 0) (0, 3) (3, 0) (3, 3) Input: N = 4 Output: (0, 0) (0, 4) (4, 0) (4, 4) (2, 2) Approach: Manhattan Distance between two points (x1, y1) and (x2, y2) is: |x

6 min read Sort an Array of Points by their distance from a reference Point

Given an array arr[] containing N points and a reference point P, the task is to sort these points according to their distance from the given point P. Examples: Input: arr[] = <<5, 0>, , , , >, P = (0, 0) Output: (1, 0) (2, 0) (3, 0) (4, 0) (5, 0) Explanation: Distance between (0, 0) and (1, 0) = 1 Distance between (0, 0) an 7 min read Find integral points with minimum distance from given set of integers using BFS

Given an integer array A[] of length N and an integer K. The task is to find K distinct integral points that are not present in the given array such that the sum of their distances from the nearest point in A[] is minimized. An integral point is defined as the point with both the coordinates as integers. Also, in the x-axis, we don't need to consid

9 min read Minimum distance to visit given K points on X-axis after starting from the origin

Given a sorted array, arr[] of size N representing the positions of the ith point on X-axis and an integer K, the task is to find the minimum distance required to travel to visit K point starting from the origin of X-axis. Examples : Input: arr[]=<-30, -10, 10, 20, 50>, K = 3Output: 40 Explanation: Moving from origin to the second point. Therefore,

6 min read Check if any point exists in a plane whose Manhattan distance is at most K from N given points

Given two arrays A[] and B[] consisting of X and Y coordinates of N distinct points in a plane, and a positive integer K, the task is to check if there exists any point P in the plane such that the Manhattan distance between the point and all the given points is at most K. If there exists any such point P, then print "Yes". Otherwise, print "No". E

7 min read Count of integral points that lie at a distance D from origin

Given a positive integer D, the task is to find the number of integer coordinates (x, y) which lie at a distance D from origin. Example: Input: D = 1Output: 4Explanation: Total valid points are <1, 0>, , , Input: D = 5Output: 12Explanation: Total valid points are , , , , , , ,