Skip to content

Instantly share code, notes, and snippets.

@manpages
Created May 28, 2012 23:16
Show Gist options
  • Save manpages/2821617 to your computer and use it in GitHub Desktop.
Save manpages/2821617 to your computer and use it in GitHub Desktop.
Not tested coordinate rotation module
-module(coordinates).
-export([
to_cartesian/2,
to_polar/2,
rotate_against/5
]).
to_cartesian(Angle, R) ->
{R*math:cos(Angle), R*math:sin(Angle)}.
to_polar(X, Y) ->
{trimaths:rad2deg(math:atan2(Y,X)), math:sqrt(X*X + Y*Y)}.
rotate_against(X, Y, AgainstX, AgainstY, Angle) ->
{ShiftX, ShiftY} = {X-AgainstX, Y-AgainstY}, %% Shift x,y to 0,0
{ShiftAngle, ShiftR} = to_polar(ShiftX, ShiftY), %% transform it to polar
{ShiftRotateX, ShiftRotateY} = to_cartesian(ShiftAngle+Angle, ShiftR} %% rotate shifted vector
{ShiftRotateX+AgainstX, ShiftRotateY+AgainstY}. %% return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment