/*
    QAD - Qt ADvanced

	Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef osm_math_p_h
#define osm_math_p_h

#include <QPointF>
#include <math.h>

// lon -> -180..+180
// lat -> -85.0511..+85.0511

namespace OSM {


inline double lng2x(double lon) {
	return (lon + 180.0) / 360.0;
}


inline double lat2y(double lat) {
	double latrad = lat * M_PI / 180.0;
	return (1.0 - asinh(tan(latrad)) / M_PI) / 2.0;
}


inline double x2lng(double x) {
	return x * 360.0 - 180;
}


inline double y2lat(double y) {
	double n = M_PI - 2.0 * M_PI * y;
	return 180.0 / M_PI * atan(0.5 * (exp(n) - exp(-n)));
}


inline QPointF xy2geo(QPointF p) {
	return QPointF(y2lat(p.y()), x2lng(p.x()));
}


inline QPointF geo2xy(QPointF p) {
	return QPointF(lng2x(p.y()), lat2y(p.x()));
}


} // namespace OSM

#endif
