Distance Between Two Points Travelling on a Polar Graph Chart Brief Problem Explanation

Write a program to find the minimum distance between two points traveling only on rays emanating from the origin and circles centered on the origin.

Explanation of Premise

Now let's imagine we are on a plane, and on this plane we are only allowed to travel in special ways. We are allowed to travel on any ray emanating from the origin.



We can also travel on any circle centered at a circle



Now our goal is to travel from one point on this plane to another. However, we can't just travel in a simple Euclidian path, we can only do this if the points happen to fall on a ray emanating from the center.



We can travel on this one because it falls on a one of our rays.



We can also travel on circles centered at the origin.



Examples

Now here is the challenge:

We've got to get from one point to another in the shortest path; often this is a combination of traveling on circles and rays.



This, however, it could also be traveling on two rays.



Sometimes there exist two paths that travel the minimum distance.



Problem

Your challenge is to write a program that when given two points will give us the minimum distance between them if we follow these rules. The inputs can be given in either rectangular or polar forms and the output ought to be one number, the distance between.

Test Cases

(with rectangular input)

(1,1) (1,-1) -&gt; ~ 2.22144
(0,0) (1, 1) -&gt; ~ 1.41421
(1,0) (-0.4161 , 0.90929) -&gt; ~ 2
(1,1) (1, 0) -&gt; ~ 1.19961
(1,2) (3, 4) -&gt; ~ 3.16609
Haskell, 49 48 bytes

(a!q)c r=min(q+r)$abs(q-r)+acos(cos$a-c)*min q r


Usage:

&gt; let rect2polar (x,y)=(atan2 y x, sqrt(x^2+y^2))
&gt; let test c1 c2=let [(a1,r1),(a2,r2)]=rect2polar&lt;$&gt;[c1,c2] in (a1!r1)a2 r2
&gt; test (1,0) (-0.4161, 0.90929)
1.9999342590038496


Thanks to @Zgarb for saving a byte