subroutine aptcirl (r1, r2, r3, tol, u3, v3, nerr)

c.
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c                             SUBROUTINE APTCIRL
c
c     call aptcirl (r1, r2, r3, tol, u3, v3, nerr)
c
c     Version:  aptcirl  Updated    2001 May 29 14:40.
c               aptcirl  Originated 1998 December 15 14:10.
c
c     Author:   Arthur L. Edwards, LLNL, L-298, Telephone (925) 422-4123.
c
c
c     Purpose:  For the three mutually tangent circles in the u-v plane, with
c               radii r1, r2 and r3, and with the center of the first circle at
c               the origin, and the center of the second circle at coordinate
c               r1 + r2 on the u axis, find the center point p3 = (u3, v3) of
c               the third circle, with positive v3.
c               Flag nerr indicates any input error, if not zero.
c
c     Method:   The center of the third circle, point p3 = (u3, v3) is at:
c                 u3 = r1 + r3 * (r1 - r2) / (r1 + r2)
c                    = r1                                if r1 = r2
c                    = r1                                if r1 + r2 = 0.
c                    = r2                                if r1 + r2 + r3 = 0.
c                 v3 = 2.0 * sqrt (r1*r2*r3*(r1+r2+r3)) / abs (r1 + r2)
c                    = sqrt (r3 * (2.0 * r1 + r3))       if r1 = r2
c                    = 0                                 if r1 + r2 = 0.
c                    = 0                                 if r1 + r2 + r3 = 0.
c
c     Input:    r1, r2, r3, tol.
c
c     Output:   u3, v3, nerr.
c
c     Calls: none 
c
c     Glossary:
c
c     nerr      Output   Indicates an input error, if not 0.
c                          1 if two or three of the radii are negative, or if
c                            one radius is negative, but not large enough in
c                            magnitude for that circle to enclose the other two.
c                          2 if any of the radii are zero.
c                          3 if r1 + r2 = 0.
c
c     r1,r2,r3  Input    The radii of three mutually tangent circles, all in
c                          the u-v plane.  A negative radius indicates a circle
c                          that surrounds the other two circles.  A very large
c                          radius, relative to the other two, indicates a
c                          straight line.
c
c     u3        Output   The u coordinate of the center of the third circle.
c                          The origin is at the center of the first circle, and
c                          the u axis is in the direction of the center of the
c                          second circle.  Returned as -1.e99 if nerr = 1.
c
c     v3        Output   The positive v coordinate of the center of the third
c                          circle.  The v axis is perpendicular to the u axis.
c                          Returned as -1.e99 if nerr = 1.
c
c     tol       Input    Numerical tolerance limit.
c                          On computers with 64-bit floating point numbers,
c                          recommend 1.e-5 to 1.e-11.
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
ccend.

cbugc***DEBUG begins.
cbug 9901 format (/ 'aptcirl finding coordinates of 3rd of three',
cbug     &  ' tangent circles with radii:' /
cbug     &  '  r1, r2, r3 =',1p3e22.14 /
cbug     &  '  tol =       ',1pe22.14 )
cbug      write ( 3, 9901) r1, r2, r3, tol
cbugc***DEBUG ends.

c.... Initialize.

      nerr = 0

      u3    = -1.e99
      v3    = -1.e99

c.... Test for input errors.

      if (((r1 .lt. 0.0) .and. (-r1 .lt. (r2 + r3)))  .or. 
     &    ((r2 .lt. 0.0) .and. (-r2 .lt. (r3 + r1)))  .or. 
     &    ((r3 .lt. 0.0) .and. (-r3 .lt. (r1 + r2)))) then
        nerr = 1
        go to 210
      endif

      if (r1 * r2 * r3 .eq. 0.0) then
        nerr = 2
        go to 210
      endif

      s12 = r1 + r2
      if (abs (s12) .le. tol * (abs (r1) + abs (r2))) s12 = 0.0
      if (s12 .eq. 0.0) then
        nerr = 3
        go to 210
      endif

c.... Find the center of the third circle.

      d12  = r1 - r2
      errd = tol * (abs (r1) + abs (r2))
      if (abs (d12) .le. errd) d12 = 0.0

      s123 = r1 + r2 + r3
      errs = tol * (abs (r1) + abs (r2) + abs (r3))
      if (abs (s123) .le. errs) s123 = 0.0

      if (d12 .eq. 0.0)  then
        u3 = r1
      elseif (s123 .eq. 0.0) then
        u3 = r2
      else
        u3   = r1 + r3 * d12 / s12
        err3 = tol *  (abs (r1) + abs (r3 * d12 / s12))
        if (abs (u3) .le. err3) u3 = 0.0
      endif

      arg4 = r1 * r2 * r3 * s123

      if (arg4 .lt. 0.0) then
        nerr = 1
        go to 210
      endif

      if (d12 .eq. 0.0) then
        v3 = sqrt (r3 * (2.0 * r1 + r3))
      elseif (s123 .eq. 0.0) then
        v3 = 0.0
      else
        v3 = 2.0 * sqrt (arg4) / abs (s12)
      endif

  210 continue

cbugc***DEBUG begins.
cbug 9902 format (/ 'aptcirl results:  nerr=',i3 /
cbug     &  '  u3, v3 =    ',1p2e22.14 )
cbug      write ( 3, 9902) nerr, u3, v3
cbugc***DEBUG ends.

      return

c.... End of subroutine aptcirl.      (+1 line.)
      end

UCRL-WEB-209832