import { Airport } from "../../types/airport"; interface GetDistanceBetweenAirportsOptions { originAirport: Airport; destinationAirport: Airport; } export const getDistanceBetweenAirportsInKm = ({ originAirport, destinationAirport, }: GetDistanceBetweenAirportsOptions) => { if ( originAirport.latitude_deg === null || destinationAirport.latitude_deg === null || originAirport.longitude_deg === null || destinationAirport.longitude_deg === null ) { return 1000; } const earthRadiusInKm = 6371; const lat1 = (originAirport.latitude_deg * Math.PI) / 180; const lat2 = (destinationAirport.latitude_deg * Math.PI) / 180; const deltaLat = ((destinationAirport.latitude_deg - originAirport.latitude_deg) * Math.PI) / 180; const deltaLon = ((destinationAirport.longitude_deg - originAirport.longitude_deg) * Math.PI) / 180; const a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) + Math.cos(lat1) * Math.cos(lat2) * Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return earthRadiusInKm * c; };