private static bool AeroWithinTol(Aero aero, double lat, double lon, double latTol, double lonTol) { double maxLat = lat + latTol, minLat = lat - latTol; double maxLon = lon + lonTol, minLon = lon - lonTol; double aLat = aero.Latitude, aLon = aero.Longitude; if (aLat > maxLat) { return(false); } else if (aLat < minLat) { return(false); } else if (aLon > maxLon) { return(false); } else if (aLon < minLon) { return(false); } else { return(true); } }
private static void DisplayNearest(Aero nearest, double lat, double lon) { Console.WriteLine("From:"); Console.WriteLine("Latitude: {0} degrees", lat); Console.WriteLine("Longitude: {0} degrees", lon); Console.WriteLine("The closest aeroplane is:"); Console.WriteLine("Latitude: {0} degrees", nearest.Latitude); Console.WriteLine("Longitude: {0} degrees", nearest.Longitude); Console.WriteLine("Relative distance: {0} km", nearest.RelativeGeoDistance); Console.WriteLine("Geometric Altitude: {0} m", nearest.GeoAltitude); Console.WriteLine("Callsign: {0}", nearest.Callsign); Console.WriteLine("ICAO24 ID: {0}", nearest.ICAO24); Console.WriteLine("Country of origin: {0}", nearest.OriginCountry); }
private static Aero GetNearest(List <Aero> aeroList, double lat, double lon) { Aero aero; Aero nearest = null; for (var i = 0; i < aeroList.Count; i++) { aero = aeroList[i]; if (i == 0) { aero.RelativeGeoDistance = AeroTracker.GeoDistance(lat, lon, aero.Latitude, aero.Longitude); nearest = aero; continue; } if (nearest == null) { throw new NullReferenceException("Could not find nearest during loop"); } double latTol = Math.Max(nearest.Latitude, lat) - Math.Min(nearest.Latitude, lat); double lonTol = Math.Max(nearest.Longitude, lon) - Math.Min(nearest.Longitude, lon); if (AeroTracker.AeroWithinTol(aero, lat, lon, latTol, lonTol)) { aero.RelativeGeoDistance = AeroTracker.GeoDistance(lat, lon, aero.Latitude, aero.Longitude); if (aero.RelativeGeoDistance < nearest.RelativeGeoDistance) { nearest = aero; } } } if (nearest == null) { throw new NullReferenceException("Could not find nearest during return"); } return(nearest); }