0

I want to calculate the distance between two locations with their coordinates. I have seen this question and it has implementations in different languages. Since I'm a beginner in prolog, it would be really helpful if anyone can build a prolog clause that can do such operation.

I've found this python function simpler:

from math import cos, asin, sqrt
def distance(lat1, lon1, lat2, lon2):
    p = 0.017453292519943295
    a = 0.5 - cos((lat2 - lat1) * p)/2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
    return 12742 * asin(sqrt(a))
Community
  • 1
  • 1

1 Answers1

1

I have made the predicate myself. It's given below:

distance(Lat1, Lon1, Lat2, Lon2, Dis):-
    P is 0.017453292519943295,
    A is (0.5 - cos((Lat2 - Lat1) * P) / 2 + cos(Lat1 * P) * cos(Lat2 * P) * (1 - cos((Lon2 - Lon1) * P)) / 2),
    Dis is (12742 * asin(sqrt(A))).

A sample run for coordinates (23.700042,90.452103) and (23.767968, 90.425657); the output is:

?- main.
Distance is: 8.01840452822046
true.

I have given an online coordinate distance calculator's result below which shows prolog's result is sufficiently accurate. Online coordinate distance calculator