//------------------------------------------------------------------------------ // Moves our reference lat lon based on the distance moved. //------------------------------------------------------------------------------ void MapPage::moveMap(const int startX, const int startY, const int endX, const int endY) { double eLat = 0, eLon = 0, lat = 0, lon = 0, sLat = 0, sLon = 0, deltaLat = 0, deltaLon = 0; // figure our lat/lon positioning pixelsToLatLon(startX, startY, sLat, sLon); pixelsToLatLon(endX, endY, eLat, eLon); // get our delta movement deltaLat = sLat - eLat; deltaLon = sLon - eLon; lat = getReferenceLatDeg(); lon = getReferenceLonDeg(); lat += deltaLat; lon += deltaLon; // As the world turns.... we need to flip the maps! if (lon > 180) lon = -180; else if (lon < -180) lon = 180; if (lat > 90) lat = -90; else if (lat < -90) lat = 90; setReferenceLatDeg(lat); setReferenceLonDeg(lon); }
void Display::mouseMotionEvent(const int x, const int y) { if (dragging) { const auto page = static_cast<MapPage*>(subpage()); if (page != nullptr) { // get our ref lat, because we won't go passed 70 degrees lat (either way); const double lat = page->getReferenceLatDeg(); if (lat < 70 && lat > -70) { page->moveMap(startX, startY, x, y); } else { if (lat > 0) page->setReferenceLatDeg(65); else page->setReferenceLatDeg(-65); } startX = x; startY = y; } } setMouse(x, y); }
//------------------------------------------------------------------------------ // converts screen to lat/lon coordinates //------------------------------------------------------------------------------ bool MapPage::screen2LatLon(const LCreal x, const LCreal y, double* const lat, double* const lon) const { bool ok = false; const double scale = getScale(); const double cosLat = getCosRefLat(); if (lat != nullptr && lon != nullptr && scale != 0 && cosLat != 0) { // buffer the inputs double screenX = x; double screenY = y; // Adjust for the decentered displayment if ( !getCentered() ) screenY -= getDisplacement(); // Scale from screen (inches) to A/C (NM) and // transpose the X and Y from screen to A/C const double acX = screenY/scale; const double acY = screenX/scale; // Rotate A/C to NED double earthX = 0.0; double earthY = 0.0; if (getNorthUp()) { earthX = acX; earthY = acY; } else { earthX = (acX * headingCos) - (acY * headingSin); earthY = (acX * headingSin) + (acY * headingCos); } // Convert nautical miles (NED) from ref point to lat/lon *lat = (earthX/60.0) + getReferenceLatDeg(); *lon = (earthY/(60.0*cosLat)) + getReferenceLonDeg(); ok = true; } return ok; }
//------------------------------------------------------------------------------ // converts lat/lon to screen coordinates //------------------------------------------------------------------------------ bool MapPage::latLon2Screen(const double lat, const double lon, LCreal* const x, LCreal* const y) const { bool ok = false; if (x != nullptr && y != nullptr) { // Convert to nautical miles (NED) centered on ownship const double earthX = ((lat - getReferenceLatDeg()) * 60.0); const double earthY = ((lon - getReferenceLonDeg()) * 60.0 * getCosRefLat()); // Rotate to aircraft coordinates double acX = 0; double acY = 0; if (getNorthUp()) { acX = earthX; acY = earthY; } else { acX = (earthX * headingCos) + (earthY * headingSin); acY = -(earthX * headingSin) + (earthY * headingCos); } // Scale from nautical miles to inches and // transpose the X and Y from A/C to screen double screenY = acX * getScale(); const double screenX = acY * getScale(); // Adjust for the decentered displayment if ( !getCentered() ) screenY += getDisplacement(); *x = static_cast<LCreal>(screenX); *y = static_cast<LCreal>(screenY); ok = true; } return ok; }