Skip to content

Instantly share code, notes, and snippets.

@tvandervossen
Created December 29, 2019 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvandervossen/77eb3916f9c551cfd2e52f06981e79de to your computer and use it in GitHub Desktop.
Save tvandervossen/77eb3916f9c551cfd2e52f06981e79de to your computer and use it in GitHub Desktop.
def vector_to_coordinates(distance, angle_in_degrees)
angle_in_radians = angle_in_degrees * Math::PI / 180
x = (distance * Math.cos(angle_in_radians)).round
y = (distance * Math.sin(angle_in_radians)).round
[x, y]
end
require "test/unit"
class TestVectorConversion < Test::Unit::TestCase
def test_vector_to_coordinates
assert_equal [10, 0], vector_to_coordinates(10, 0)
assert_equal [0, 10], vector_to_coordinates(10, 90)
assert_equal [-10, 0], vector_to_coordinates(10, 180)
assert_equal [7, 7], vector_to_coordinates(10, 45)
assert_equal [9, 5], vector_to_coordinates(10, 30)
assert_equal [7, -7], vector_to_coordinates(10, -45)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment