private Flights CreateFlight (DateTime relativeDate, DateTime lastFlightDate, FlightPlan flightPlan, int numSeg, List <Segment> flightSegments) { double longitude1 = 0, longitude2, latitude1 = 0, latitude2, longitude3, latitude3; double secoInSegment, timeRatio, distance, midDistance; // Find how much time passed from segment till now. secoInSegment = relativeDate.Subtract(lastFlightDate).TotalSeconds; // Find the time ratio. timeRatio = secoInSegment / flightSegments[numSeg - 1].Timespan_seconds; // Check if we are in the first segment. if (numSeg == 1) { // The last coordinate is from Initial_Location. longitude1 = flightPlan.Initial_location.Longitude; latitude1 = flightPlan.Initial_location.Latitude; } else { // The last coordinate is from last segment. longitude1 = flightSegments[numSeg - 2].Longitude; latitude1 = flightSegments[numSeg - 2].Latitude; } // The current segment's coordinates. longitude2 = flightSegments[numSeg - 1].Longitude; latitude2 = flightSegments[numSeg - 1].Latitude; // Linear interpolation distance = Math.Sqrt((Math.Pow(longitude2 - longitude1, 2) + Math.Pow(latitude2 - latitude1, 2))); midDistance = timeRatio * distance; latitude3 = latitude2 - ((midDistance) * (latitude2 - latitude1) / distance); longitude3 = longitude2 - ((midDistance) * (longitude2 - longitude1) / distance); // Create new Flight with the details we found. Flights flight = new Flights(); flight.Longitude = longitude3; flight.Latitude = latitude3; flight.Flight_id = flightPlan.GetId(); flight.Is_external = false; flight.Company_name = flightPlan.Company_Name; flight.Passengers = flightPlan.Passengers; flight.Date_time = flightPlan.Initial_location.Date_time; return(flight); }