Code Explainer | Excel

Haversine Distance Calculator

This code calculates the great-circle distance between two points on Earth using the Haversine formula, factoring in latitude and longitude in degrees to derive the shortest path distance in kilometers.


Empty image or helper icon

Prompt

=6371 * ACOS(COS(RADIANS(Lat1)) * COS(RADIANS(Lat2)) * COS(RADIANS(Lon2) - RADIANS(Lon1)) + SIN(RADIANS(Lat1)) * SIN(RADIANS(Lat2)))

Answer

Code Explanation

The provided code calculates the great-circle distance between two points on the Earth's surface using the Haversine formula. This distance is computed based on the latitude and longitude of the two points, given in degrees.

Breakdown of the Code

= 6371 * ACOS(
    COS(RADIANS(Lat1)) * COS(RADIANS(Lat2)) *
    COS(RADIANS(Lon2) - RADIANS(Lon1)) +
    SIN(RADIANS(Lat1)) * SIN(RADIANS(Lat2))
)

Components of the Code

  1. 6371:

    • This is the approximate radius of the Earth in kilometers. The distance calculated will be expressed in kilometers as a result of multiplying by this constant.
  2. ACOS Function:

    • ACOS(x) returns the angle whose cosine is x. It is essential for calculating the distance on the spherical surface of the Earth.
  3. RADIANS Function:

    • The RADIANS(angle) function converts degrees into radians. Since trigonometric functions in most programming languages and mathematical calculations use radians, this conversion is crucial.
  4. COS and SIN Functions:

    • COS(x) calculates the cosine of x (in radians).
    • SIN(x) calculates the sine of x (in radians).
  5. Lat1, Lat2, Lon1, Lon2:

    • These variables represent the latitudes and longitudes of the two points.
    • Lat1 and Lon1 correspond to the first point, and Lat2 and Lon2 correspond to the second point.

Step-by-Step Calculation

  1. Convert Latitude and Longitude:

    • Convert all latitude and longitude values from degrees to radians using the RADIANS function.
  2. Calculate Components:

    • Compute the cosine of both latitudes and the cosine of the difference of longitudes.
    • Calculate the sine of both latitudes.
  3. Evaluate the Cosine of the Central Angle:

    • Combine the components using the formula implemented, which corresponds to the cosine of the central angle between the two points.
  4. Apply ACOS:

    • Use the ACOS function on the calculated value to find the angle in radians.
  5. Multiply by Earth Radius:

    • Finally, multiply the result by 6371 to convert the angle from radians to kilometers, yielding the great-circle distance.

Key Concepts

  • Great-Circle Distance: The shortest distance between two points on the surface of a sphere. It takes the curvature of the Earth into account.

Alternative Example

If you have a coding environment such as Python, the equivalent calculation could be implemented as follows:

import math

def haversine_distance(lat1, lon1, lat2, lon2):
    R = 6371  # Radius of the Earth in kilometers
    dlat = math.radians(lat2 - lat1)  # Difference in latitude
    dlon = math.radians(lon2 - lon1)  # Difference in longitude
    
    a = (math.sin(dlat / 2) ** 2 + 
         math.cos(math.radians(lat1)) * 
         math.cos(math.radians(lat2)) * 
         (math.sin(dlon / 2) ** 2))
    
    c = 2 * math.acos(math.sqrt(a))  # Central angle in radians
    distance = R * c  # Distance in kilometers
    
    return distance

This example uses a slightly different approach to compute the same distance but emphasizes clear logic based on the Haversine theorem.

Conclusion

The provided code snippet effectively computes the great-circle distance between two geographical points on the Earth using trigonometric functions and the properties of a circle. Understanding each component enhances insights into

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This code calculates the great-circle distance between two points on Earth using the Haversine formula, factoring in latitude and longitude in degrees to derive the shortest path distance in kilometers.