Skip to content

Instantly share code, notes, and snippets.

@xslogic
Created December 8, 2010 05:55
Show Gist options
  • Save xslogic/732947 to your computer and use it in GitHub Desktop.
Save xslogic/732947 to your computer and use it in GitHub Desktop.
problem 2
-module(grid).
-export([grid_id/4]).
%% Rather than provide 1000 blocks as input,
%% it would be easier to provide a Height and breadth
%% of the bounding box.. making it easier to scale
grid_id(Lat, Long, XMax, YMax) ->
{X, Y} = scale(Lat, Long, XMax, YMax),
grid_id(X, Y, XMax div 2, YMax div 2, []).
grid_id(_, _, X, Y, Val) when (X =< 1) andalso (Y =< 1)->
list_to_integer(lists:reverse(Val));
grid_id(X, Y, XTh, YTh, Val) when (X > XTh) andalso (Y > YTh) ->
grid_id(X - XTh, Y - YTh, XTh div 2, YTh div 2, [$4|Val]);
grid_id(X, Y, XTh, YTh, Val) when (X > XTh) andalso (Y =< YTh) ->
grid_id(X - XTh, Y, XTh div 2, YTh div 2, [$3|Val]);
grid_id(X, Y, XTh, YTh, Val) when (X =< XTh) andalso (Y > YTh) ->
grid_id(X, Y - YTh, XTh div 2, YTh div 2, [$2|Val]);
grid_id(X, Y, XTh, YTh, Val) ->
grid_id(X, Y, XTh div 2, YTh div 2, [$1|Val]).
scale(X, Y, XMax, YMax) ->
{(X / 90.0) * XMax, (Y / 90.0) * YMax}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment