in h3_coordijk.c [56:147]
void _hex2dToCoordIJK(const Vec2d *v, CoordIJK *h) {
double a1, a2;
double x1, x2;
int m1, m2;
double r1, r2;
// quantize into the ij system and then normalize
h->k = 0;
a1 = fabsl(v->x);
a2 = fabsl(v->y);
// first do a reverse conversion
x2 = a2 * M_RSIN60;
x1 = a1 + x2 / 2.0;
// check if we have the center of a hex
m1 = (int)x1;
m2 = (int)x2;
// otherwise round correctly
r1 = x1 - m1;
r2 = x2 - m2;
if (r1 < 0.5) {
if (r1 < 1.0 / 3.0) {
if (r2 < (1.0 + r1) / 2.0) {
h->i = m1;
h->j = m2;
} else {
h->i = m1;
h->j = m2 + 1;
}
} else {
if (r2 < (1.0 - r1)) {
h->j = m2;
} else {
h->j = m2 + 1;
}
if ((1.0 - r1) <= r2 && r2 < (2.0 * r1)) {
h->i = m1 + 1;
} else {
h->i = m1;
}
}
} else {
if (r1 < 2.0 / 3.0) {
if (r2 < (1.0 - r1)) {
h->j = m2;
} else {
h->j = m2 + 1;
}
if ((2.0 * r1 - 1.0) < r2 && r2 < (1.0 - r1)) {
h->i = m1;
} else {
h->i = m1 + 1;
}
} else {
if (r2 < (r1 / 2.0)) {
h->i = m1 + 1;
h->j = m2;
} else {
h->i = m1 + 1;
h->j = m2 + 1;
}
}
}
// now fold across the axes if necessary
if (v->x < 0.0) {
if ((h->j % 2) == 0) // even
{
long long int axisi = h->j / 2;
long long int diff = h->i - axisi;
h->i = (int)(h->i - 2.0 * diff);
} else {
long long int axisi = (h->j + 1) / 2;
long long int diff = h->i - axisi;
h->i = (int)(h->i - (2.0 * diff + 1));
}
}
if (v->y < 0.0) {
h->i = h->i - (2 * h->j + 1) / 2;
h->j = -1 * h->j;
}
_ijkNormalize(h);
}