# How to Calculate the Distance Between Two Points

Omkar Phatak Jan 25, 2019
How is the distance between two geometrical points measured on any surface? What is the most useful formula for that purpose? Read to find all the answers.
One of the most basic problems in geometry, is finding the distance that separates points on a graph. Here we will discuss the calculation of segment length, connecting points on a 2D graph and 3D grid. The case of distance measurement on a sphere, is also discussed.

### On a Graph

The formula is based on the Pythagoras theorem. Let us draw points on a graph. Let there be a reference frame, in the form of the X and Y axes intersecting at the origin. Every point will have an X-Coordinate and a Y-coordinate.
The X-coordinate is the distance of the point from the Y axis and the Y-coordinate is the distance of the point from the X axis.
Now, draw perpendiculars from both points, on both the axes. Then, draw a straight line joining the two points. As you can see, the perpendiculars drawn from the points and the segment joining the points, form a right-angled triangle. The distance is the length of the hypotenuse of the triangle.
Using the Pythagorean theorem formula, the distance can be calculated as:.
Distance Separating Points [A(x1, y1), B (x2, y2)] = √[(x2 - x1)2 + (y2 - y1)2]

the coordinates of both the points must be known. Then, using the formula, the distance can be calculated.

### Measuring Distance on a 3D Cartesian Grid

What if the two points are situated on a three-dimensional grid? To locate a point on this grid, you will have to know three coordinates, instead of two.
This distance is calculated by modifying the Pythagoras formula:

Distance Between Points [A(x1, y1, z1), B(x2, y2, z2)] = √[(x2 - x1)2 + (y2 - y1)2 + (z2 - z1)2]

The knowledge of the three coordinates of both points, will enable you to calculate the distance.

### Measuring Distance On a Sphere

The formula for points on a graph  can not be used to measure distance on a sphere because a sphere is not a flat surface and Euclidean geometry is not applicable there. It has a 'Curvature', which makes calculation difficult.
Hence a complicated formula is required for calculating this distance. This formula can be used to calculate the great circle distance that separates two places on Earth (assuming the Earth is perfectly spherical).
The data you need is the knowledge of latitude and longitude of both those points and the radius of the sphere. The formula is as follows:

Distance Separating Two Points on a Sphere (D)
= R * Δσ

where r is the radius of the sphere and Δσ is the central angle subtended by the two points, with the center of the sphere.
The points are P (a1, b1) and Q (a2, b2), where a and b are latitude and longitude coordinates. Δσ is calculated by using the following complicated formula (Also known as the Vincenty formula):

Δσ = arctan (A/B)

where A = √[(cos a1 sin Δb)2 + (cos a2 sin a1 - sin a2 cos a1 cos Δb)2]

B = sin a2 sin a1 + cos a2 cos a1 cos Δb
Here, Δb = b1 - b2. This might be a bit overwhelming, but it is as simple as it gets. While using the formula, convert the latitude and longitudes into radians, before substituting values.