C++ (Qt)#include <QtGUI>#include <math.h>#include <vector> double Mercant2Sphere( double iTY, const double maxAng = 85.0 ){ static std::vector <double> vec; if (!vec.size()) { vec.push_back(0.0); double unit = M_PI / 180; for (double i = 1; i < maxAng; ++i) vec.push_back(vec[i - 1] + unit / cos(i * unit)); // normalize double scale = M_PI_2 / vec.back(); for (size_t i = 0; i < vec.size(); ++i) vec[i] *= scale; } double angle = (iTY * 2 - 1) * M_PI_2; // texture V to angle double angle_degree = fabs(angle) * 180 / M_PI; int beg = (int) angle_degree; double val; if (beg >= (int) vec.size() - 1) val = M_PI_2; else { double w = angle_degree - beg; val = vec[beg] * (1.0 - w) + vec[beg + 1] * w; } if (angle < 0.0) val = -val; return (1 + val / M_PI_2) * 0.5; // angle to texture V} int main( int argc, char **argv ) { (void) argc; (void) argv; return 0;}
inline double mercator(double x) { return 0.5*log((1.0+sin(x))/(1.0-sin(x)));}
// vec.push_back(vec[i - 1] + unit / cos(i * unit));vec.push_back(mercator(i * unit));
C++ (Qt)const double defMercAngle = 85 * M_PI / 180;const double defMercScale = M_PI_2 / mercator(defMercAngle); double Mercant2SphereAnalytic( double iTY, const double scale = defMercScale, const double maxAng = defMercAngle ){ double angle = (iTY * 2 - 1) * M_PI_2; // texture V to angle double angle2 = fabs(angle); double val = (angle2 > maxAng) ? M_PI_2 : (mercator(angle2) * scale); if (angle < 0.0) val = -val; return (1 + val / M_PI_2) * 0.5; // angle to texture V}