ref: 2376db3167bee07ce18758a32ef05916eb325a3f
dir: /tile.c/
#include <u.h>
#include <libc.h>
#include <draw.h>
#include "dat.h"
#include "fns.h"
static double
asinh(double x)
{
double s = sqrt(x*x + 1);
return log(x + s);
}
int
lon2tilex(double lon, int z, int *offset)
{
double tile = (lon + 180.0) / 360.0 * (1<<z);
int t = floor(tile);
*offset = (tile - t) * tilesize;
return t;
}
int
lat2tiley(double lat, int z, int *offset)
{
double latrad = lat * PI/180.0;
double tile = (1.0 - asinh(tan(latrad)) / PI) / 2.0 * (1<<z);
int t = floor(tile);
*offset = (tile - t) * tilesize;
return t;
}
double tilex2long(int x, int z)
{
return x / (double)(1<<z) * 360.0 - 180.0;
}
double tiley2lat(int y, int z)
{
double n = PI - 2.0 * PI * y / (double)(1<<z);
return 180.0 / PI * atan(0.5 * (exp(n) - exp(-n)));
}
GBundle
getbundle(GPos pos, int z, Point *offset)
{
GBundle ret;
ret.x = lon2tilex(pos.lon, z, &offset->x);
ret.y = lat2tiley(pos.lat, z, &offset->y);
ret.z = z;
return ret;
}