To Polar and Back
code-golf math geometry
There are 2 main ways to represent a single point on a plane:
- in Cartesian form, with an x and y value,
- and in Polar form, with an angle and a magnitude
For example, the point
(5, 5) can also be represented with the angle
π / 4 and magnitude
5 * sqrt(2)
In this challenge, we will be using radians, and not degrees.
This challenge deals with the mapping of Polar form onto a Cartesian grid, by using the angle as the x value, and the magnitude as the y value.
For example, to change the point
(sqrt(3), 1) into Polar form and back, we first convert it into polar form, with an angle of
π / 6 and magnitude of
Then, these values are mapped into Cartesian form, yielding the Cartesian point
Create a program or function that, given a rectangle in Cartesian form, outputs it's area after it has been converted to Polar form and mapped back onto the Cartesian plane by equating angle with x and magnitude with y.
The input will be a rectangle in Cartesian form whose legs are parallel to the x and y axes. Also, all 4 corners will be in the first quadrant, meaning that all x and y values will be strictly positive.
This means that you may take
- The Cartesian coordinates of all 4 corners
- The Cartesian coordinates of opposite corners
- The Cartesian coordinates of one corner, and a width and height value
or some other reasonable form, such as a rectangle object.
The important thing is that you don't take Polar coordinates as input.
You can take the points or values in any order you choose.
Please include the input format you used in your answer.
Output will be a single, decimal number rounded to at least the hundredth's place (2 digits after the decimal).
Note: For the sake of clarity, I will use symbols like
sqrt(3) instead of their decimal equivalents.
Also keep in mind that this is probably one of many ways to go about this problem, and it doesn't matter what method you use to find the area as long as you do. This example is mostly to explain the above by showing it in action.
Anyways, here is out input, in the form of 4 points:
(1, sqrt(3)), (4, sqrt(3)), (1, 1), (4, 1)
First, we might convert each point into polar form, and map that back onto the Cartesian plane:
(x, y) -> (angle, magnitude)
(1, sqrt(3)) -> (π/3, 2)
(4, sqrt(3)) -> (arctan(sqrt(3)/4), sqrt(19))
(1, 1) -> (π/4, sqrt(2))
(4, 1) -> (arctan(1/4), sqrt(17))
This should be relatively easy using trigonometry and the Pythagorean theorem.
These new Cartesian points give us the corners of our new shape, which looks something like this:
Notice that the sides are not straight, meaning you will have to figure out how to get the equations for the sides as well as how to find the area. I'll leave that up to you.
In any case, the area of this shape is about
0.81810689, but you only need to print
0.82 (without the leading
0, if you prefer).
Other Test Cases
The input will be taken in the form of 4 points, in reading order (left to right, then top to bottom)
input -> output
(1, 1.732), (4, 1.732), (1, 1), (4, 1) -> 0.82
Sandbox: I will add more test cases later - any suggestions about certain edge cases I should include would be helpful.
This is code-golf, so the shortest answer in bytes wins.
Standard loopholes, as always, apply.
- Any name ideas?
- Any tags I should add/remove?
- Suggestions/help with creating test cases?
- Is the explanation sufficient?
- Should I change the accuracy requirement to 3 digits after the decimal point, or would that be too hard?