public double Distance(City other) { double latdif = latitude - other.latitude; double longdif = longitude - other.longitude; return Math.Sqrt(latdif * latdif + longdif * longdif); }
public City FindCentralCity() { double totLat = 0, totLong = 0; // work out average latitude and longitude. // This will be the center of the cities for (int i = 0; i < cities.Length; i++) { totLat += cities[i].Latitude; totLong += cities[i].Longitude; } totLat /= cities.Length; totLong /= cities.Length; // create a new city located at center City central = new City("", totLat, totLong); // find city closest to center City closest = cities[0]; double tempDist, minDist = central.Distance(closest); for (int i = 1; i < cities.Length; i++) { tempDist = central.Distance(cities[i]); if (tempDist < minDist) { closest = cities[i]; minDist = tempDist; } } return closest; }
private void LoadCities(string cityFile) { cities = null; StreamReader fin; try { string line, name; int cityCount = 0; double lat, lng; // get count of cities; fin = new StreamReader(cityFile); while (!fin.EndOfStream) { line = fin.ReadLine(); line.Trim(); if (line.Length > 0) cityCount++; } fin.Close(); // create cities array cityCount /= 3; if (cityCount == 0) return; cities = new City[cityCount]; // load cities into array fin = new StreamReader(cityFile); for (int i = 0; i < cityCount; i++) { name = fin.ReadLine(); line = fin.ReadLine(); if (!double.TryParse(line, out lat)) break; line = fin.ReadLine(); if (!double.TryParse(line, out lng)) break; cities[i] = new City(name, lat, lng); } fin.Close(); } catch (IOException e) { MessageBox.Show("Unable to read city file with the following error\n" + e.Message, "City Central 2", MessageBoxButtons.OK, MessageBoxIcon.Error); } }