Skip to content

Instantly share code, notes, and snippets.

@osdrv
Created December 30, 2011 08:38
Show Gist options
  • Save osdrv/1538784 to your computer and use it in GitHub Desktop.
Save osdrv/1538784 to your computer and use it in GitHub Desktop.
c++ libcinder mercator coordinates mapper
#include "Mercator.h"
#include "cinder/app/AppBasic.h"
#include "cinder/Vector.h"
using namespace ci;
using namespace std;
/* static method to map latitude and longitude to mercator 2d coords */
Vec2f Mercator::mapLatLon( const Vec2f lat_lon ) {
/* mercator projection center was screen centered */
Vec2f offset = Vec2f( app::getWindowWidth() / 2, app::getWindowHeight() / 2 );
/* we need to place all the earth loft exactly at window area */
float radius = app::getWindowWidth() / ( 2 * M_PI );
/* yap, the simplest action in this method
* lat_lon.x === latitude, lat_lon.y === longitude
*/
float x = radius * lat_lon.y * M_PI / 180 + offset.x;
/* here is the black magick */
float y = offset.y - radius * ( log( tan( ( 90 + lat_lon.x ) * M_PI / 360 ) ) / M_PI ) * 2.8f;
return Vec2f( x, y );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment