Challenge: Map Polar Points to Cartesian Points

This lesson brings you a challenge to solve.

We'll cover the following

Problem statement

Write an interactive console program that asks the user for the polar coordinates of a 2-dimensional point (radius and angle (degrees)). Calculate the corresponding Cartesian coordinates x and y, and print out the result. Use structs called polar and Cartesian to represent each coordinate system. Use channels and a goroutine:

  • A channel1 to receive the polar coordinates
  • A channel2 to receive the Cartesian coordinates

The conversion itself must be done with a goroutine, which reads from channel1 and sends it to channel2. In reality, for such a simple calculation it is not worthwhile to use a goroutine and channels, but this solution would be quite appropriate for heavy computation.

Formulae

Θ = Angle of polar coordinates * π / 180.0 , where π=3.1416…

x of Cartesian = Radius of polar coordinates * Cos(Θ)

y of Cartesian = Radius of polar coordinates * Sin(Θ)

Note: To understand the conversion, you can read this Wikipedia page.

Try to attempt the challenge below. Good Luck!

Get hands-on with 1400+ tech skills courses.