
Given two points on a plane and a radius, usually two circles of given radius can be drawn through the points.

Exceptions

 r==0.0 should be treated as never describing circles (except in the case where the points are coincident).
 If the points are coincident then an infinite number of circles with the point on their circumference can be drawn, unless r==0.0 as well which then collapses the circles to a point.
 If the points form a diameter then return two identical circles or return a single circle, according to which is the most natural mechanism for the implementation language.
 If the points are too far apart then no circles can be drawn.


Task detail

 Write a function/subroutine/method/... that takes two points and a radius and returns the two circles through those points, or some indication of special cases where two, possibly equal, circles cannot be returned.
 Show here the output for the following inputs:
      p1                p2           r
0.1234, 0.9876    0.8765, 0.2345    2.0
0.0000, 2.0000    0.0000, 0.0000    1.0
0.1234, 0.9876    0.1234, 0.9876    2.0
0.1234, 0.9876    0.8765, 0.2345    0.5
0.1234, 0.9876    0.1234, 0.9876    0.0



Related task

   Total circles area.


See also

   Finding the Center of a Circle from 2 Points and Radius from Math forum @ Drexel


T Circle
   Float x, y, r
 
   F String()
      R ‘Circle(x=#.6, y=#.6, r=#.6)’.format(.x, .y, .r)
 
   F (x, y, r)
      .x = x
      .y = y
      .r = r
 
T Error
   String msg
   F (msg)
      .msg = msg
 
F circles_from_p1p2r(p1, p2, r)
   ‘Following explanation at http://mathforum.org/library/drmath/view/53027.html’
   I r == 0.0
      X Error(‘radius of zero’)
   V (x1, y1) = p1
   V (x2, y2) = p2
   I p1 == p2
      X Error(‘coincident points gives infinite number of Circles’)
   V (dx, dy) = (x2 - x1, y2 - y1)
   V q = sqrt(dx ^ 2 + dy ^ 2)
   I q > 2.0 * r
      X Error(‘separation of points > diameter’)
   V (x3, y3) = ((x1 + x2) / 2, (y1 + y2) / 2)
   V d = sqrt(r ^ 2 - (q / 2) ^ 2)
   V c1 = Circle(x' x3 - d * dy / q,
                 y' y3 + d * dx / q,
                 r' abs(r))
   V c2 = Circle(x' x3 + d * dy / q,
                 y' y3 - d * dx / q,
                 r' abs(r))
   R (c1, c2)
 
L(p1, p2, r) [((0.1234, 0.9876), (0.8765, 0.2345), 2.0),
              ((0.0000, 2.0000), (0.0000, 0.0000), 1.0),
              ((0.1234, 0.9876), (0.1234, 0.9876), 2.0),
              ((0.1234, 0.9876), (0.8765, 0.2345), 0.5),
              ((0.1234, 0.9876), (0.1234, 0.9876), 0.0)]
   print("Through points:\n  #.,\n  #.\n  and radius #.6\nYou can construct the following circles:".format(p1, p2, r))
   X.try
      V (c1, c2) = circles_from_p1p2r(p1, p2, r)
      print("  #.\n  #.\n".format(c1, c2))
   X.catch Error v
      print("  ERROR: #.\n".format(v.msg))