# cart2polar Function

## public function cart2polar(coord, n) result(polar)

Convert from cartesian coordinates (in the x-y plane) to polar coordinates (radius,angle)

### Arguments

Type IntentOptional AttributesName
real(kind=rk), intent(in) :: coord(n,3)
integer, intent(in) :: n

### Return Value real(kind=rk) (n,2)

Polar coordinates, radius (first entry) and angle (second entry)

## Source Code

  function cart2polar(coord, n) result(polar)
! --------------------------------------------------------------------------
integer, intent(in) :: n
real(kind=rk),intent(in) :: coord(n,3)
!> Polar coordinates, radius (first entry) and angle (second entry)
real(kind=rk) :: polar(n,2)
! --------------------------------------------------------------------------
!> X coordinate
real(kind=rk) :: x
!> Y coordinate
real(kind=rk) :: y
integer :: i
! --------------------------------------------------------------------------

do i = 1,n

x = coord(i,1)
y = coord(i,2)

polar(i,1) = sqrt(x*x + y*y)

! Atan2 is not defined when both coordinates are zero. To cover
! this situation correctly, we define the angle to be 0.
if(polar(i,1) > epsilon(1.0_rk) ) then
polar(i,2) = atan2(y,x)
else
polar(i,2) = 0.0_rk
end if

end do

end function cart2polar