-
-
Notifications
You must be signed in to change notification settings - Fork 48.5k
Python script that performs haversine calculations #13119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
renitreni
wants to merge
6
commits into
TheAlgorithms:master
Choose a base branch
from
renitreni:renitreni-algorithm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c6132f
Python script that performs haversine calculations
renier-30kft 48575ad
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3ccbc42
I have successfully created a Python package for haversine calculatio…
renier-30kft 20e6c62
Merge remote-tracking branch 'origin/renitreni-algorithm' into renitr…
renier-30kft 0bbe184
Adding hints
renier-30kft 54c5504
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# This file makes haversine_calculation a Python package |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import math | ||
|
||
|
||
def haversine(lat1, lon1, lat2, lon2): | ||
""" | ||
Calculate the great circle distance in kilometers between two points | ||
on the earth (specified in decimal degrees) | ||
""" | ||
# Convert decimal degrees to radians | ||
lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2]) | ||
|
||
# Haversine formula | ||
dlat = lat2 - lat1 | ||
dlon = lon2 - lon1 | ||
a = ( | ||
math.sin(dlat / 2) ** 2 | ||
+ math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2 | ||
) | ||
c = 2 * math.asin(math.sqrt(a)) | ||
r = 6371 # Radius of earth in kilometers. Use 3956 for miles. | ||
return c * r | ||
|
||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
# Coordinates of New York City and London | ||
lat1, lon1 = 40.7128, -74.0060 # New York City | ||
lat2, lon2 = 51.5074, -0.1278 # London | ||
|
||
distance = haversine(lat1, lon1, lat2, lon2) | ||
print(f"Distance between New York City and London: {distance:.2f} km") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.