@@ -271,19 +271,31 @@ geohex_coordinate_t geohex_get_coordinate_by_code(const char *code) {
271271 return geohex_adjust_coordinate (geohex_coordinate (h_x , h_y ), level );
272272}
273273
274- geohex_t geohex_get_zone_by_coordinate (const geohex_coordinate_t coordinate , const geohex_level_t level ) {
274+ static geohex_location_t geohex_get_location_by_coordinate (const geohex_coordinate_t coordinate , const geohex_level_t level ) {
275275 const long double h_k = _deg ();
276276 const long double h_size = geohex_hexsize (level );
277-
278- const long double h_x = (long double )coordinate .x ;
279- const long double h_y = (long double )coordinate .y ;
277+ const long double x = (long double )coordinate .x ;
278+ const long double y = (long double )coordinate .y ;
280279
281280 const long double unit_x = 6.0L * h_size ;
282281 const long double unit_y = 6.0L * h_size * h_k ;
283282
284- const long double h_lat = (h_k * h_x * unit_x + h_y * unit_y ) / 2.0L ;
285- const long double h_lon = (h_lat - h_y * unit_y ) / h_k ;
286- geohex_location_t z_loc = _geohex_coordinate2location (_geohex_coordinate_ld (h_lon , h_lat ));
283+ const long double h_y = (h_k * x * unit_x + y * unit_y ) / 2.0L ;
284+ const long double h_x = (h_y - y * unit_y ) / h_k ;
285+
286+ geohex_location_t z_loc = _geohex_coordinate2location (_geohex_coordinate_ld (h_x , h_y ));
287+
288+ const long max_hsteps = geohex_pow3 (level + 2 );
289+ const long hsteps = labs (coordinate .x - coordinate .y );
290+ if (hsteps == max_hsteps ) {
291+ z_loc .lng = -180.0L ;
292+ }
293+
294+ return z_loc ;
295+ }
296+
297+ geohex_t geohex_get_zone_by_coordinate (const geohex_coordinate_t coordinate , const geohex_level_t level ) {
298+ const geohex_location_t z_loc = geohex_get_location_by_coordinate (coordinate , level );
287299
288300 long mod_x = coordinate .x ;
289301 long mod_y = coordinate .y ;
@@ -292,11 +304,10 @@ geohex_t geohex_get_zone_by_coordinate(const geohex_coordinate_t coordinate, con
292304 const long hsteps = labs (mod_x - mod_y );
293305 if (hsteps == max_hsteps ) {
294306 if (mod_x > mod_y ) {
295- const long tmp = h_x ;
307+ const long tmp = coordinate . x ;
296308 mod_x = mod_y ;
297309 mod_y = tmp ;
298310 }
299- z_loc .lng = -180.0L ;
300311 }
301312
302313 static char code3_x [GEOHEX3_DEC3_BUFSIZE /2 ];
@@ -365,7 +376,7 @@ geohex_t geohex_get_zone_by_coordinate(const geohex_coordinate_t coordinate, con
365376 .location = geohex_location (z_loc .lat , z_loc .lng ),
366377 .coordinate = coordinate ,
367378 .level = level ,
368- .size = h_size
379+ .size = geohex_hexsize ( level )
369380 };
370381 geohex .code [0 ] = GEOHEX3_HASH_KEY [(int )floor (global_code /30 )];
371382 geohex .code [1 ] = GEOHEX3_HASH_KEY [global_code %30 ];
0 commit comments