cart2polar Function

private function cart2polar(x, y) result(polar)

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

Arguments

Type IntentOptional Attributes Name
real(kind=rk) :: x

X coordinate

real(kind=rk) :: y

Y coordinate

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

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


Source Code

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

    polar(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(1) > epsilon(1.0_rk) ) then
      polar(2) = atan2(y,x)
    else
      polar(2) = 0.0_rk
    end if

  end function cart2polar