How to resolve the algorithm Haversine formula step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Haversine formula step by step in the C programming language

Table of Contents

Problem Statement

The haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is a special case of a more general formula in spherical trigonometry, the law of haversines, relating the sides and angles of spherical "triangles".

Implement a great-circle distance function, or use a library function, to show the great-circle distance between:

Most of the examples below adopted Kaimbridge's recommended value of 6372.8 km for the earth radius. However, the derivation of this ellipsoidal quadratic mean radius is wrong (the averaging over azimuth is biased). When applying these examples in real applications, it is better to use the mean earth radius, 6371 km. This value is recommended by the International Union of Geodesy and Geophysics and it minimizes the RMS relative error between the great circle and geodesic distance.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Haversine formula step by step in the C programming language

This C code calculates the distance between two points on Earth's surface using the Haversine formula. It is a method of calculating the great-circle distance between two points on a sphere.

  1. Header Includes:

    • #include <stdio.h>: For input/output functions like printf.
    • #include <stdlib.h>: For exit function.
    • #include <math.h>: For mathematical functions like sin, cos, and asin.
  2. Constants:

    • #define R 6371: Radius of the Earth in kilometers.
    • #define TO_RAD (3.1415926536 / 180): Conversion factor from degrees to radians.
  3. dist Function:

    • Input: Four double-precision floating-point numbers: th1, ph1, th2, and ph2. These represent the latitudes and longitudes of two points on Earth in decimal degrees.
    • Return: The distance between the two points in kilometers, calculated using the Haversine formula.
    • Formula:
      dist = 2 * R * asin(sqrt(dx^2 + dy^2 + dz^2) / 2)
      
      where:
      • dx = cos(ph1) * cos(th1) - cos(th2)
      • dy = sin(ph1) * cos(th1)
      • dz = sin(th1) - sin(th2)
  4. main Function:

    • Calculates the distance between two points on Earth using the dist function:
      • Nashville, TN: (36.12, -86.67)
      • Los Angeles, CA: (33.94, -118.4)
    • Prints the distance in both kilometers and miles, converting kilometers to miles using a factor of 1.609344.

Source code in the c programming language

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define R 6371
#define TO_RAD (3.1415926536 / 180)
double dist(double th1, double ph1, double th2, double ph2)
{
	double dx, dy, dz;
	ph1 -= ph2;
	ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD;

	dz = sin(th1) - sin(th2);
	dx = cos(ph1) * cos(th1) - cos(th2);
	dy = sin(ph1) * cos(th1);
	return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R;
}

int main()
{
	double d = dist(36.12, -86.67, 33.94, -118.4);
	/* Americans don't know kilometers */
	printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344);

	return 0;
}


  

You may also check:How to resolve the algorithm Literals/String step by step in the C++ programming language
You may also check:How to resolve the algorithm Longest string challenge step by step in the Nim programming language
You may also check:How to resolve the algorithm Write to Windows event log step by step in the PureBasic programming language
You may also check:How to resolve the algorithm McNuggets problem step by step in the MAD programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Julia programming language