Talk:Circles of given radius through two points: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎More special cases: One covered. One maybe not. Thanks.)
(→‎More special cases: forgot sqrt; a circle with zero radius is a circle)
Line 1: Line 1:
===More special cases===
===More special cases===
There may be more special cases. If p1==p2 and r==0, there is one unique answere that's a zero radius circle. If tow points are separated by exactly double the radius, there's only one answer. The latter can be treated as two identical circles, but then so can the former.
There may be more special cases. If p1==p2 and r==0, there is one unique answere that's a zero radius circle. If tow points are separated by exactly double the radius, there's only one answer. The latter can be treated as two identical circles, but then so can the former.
<lang python>def find_center(p1, p2, r):
<lang python>from math import sqrt

def find_center(p1, p2, r):
if p1 == p2:
if p1 == p2:
if r == 0: return [p1] # special special case
if r == 0: return [p1] # special special case
Line 14: Line 16:
if not a: return [(x0, y0)]
if not a: return [(x0, y0)]
if a < 0: return []
if a < 0: return []
a = sqrt(a)
return [(x + a*dy, y - a*dx), (x - a*dy, y + a*dx)]
return [(x + a*dy, y - a*dx), (x - a*dy, y + a*dx)]


Line 22: Line 25:
:Hi Ledrug. I have one of those covered - two points on a diameter is tested by the current second set of inputs. I'll have to adjust for the two coincident points with r == 0.0 case. Thanks.
:Hi Ledrug. I have one of those covered - two points on a diameter is tested by the current second set of inputs. I'll have to adjust for the two coincident points with r == 0.0 case. Thanks.
:Hmm r==0.0 might be treated as an exception too as it is the circle as a point, (If you don't want points). --[[User:Paddy3118|Paddy3118]] ([[User talk:Paddy3118|talk]]) 05:02, 17 April 2013 (UTC)
:Hmm r==0.0 might be treated as an exception too as it is the circle as a point, (If you don't want points). --[[User:Paddy3118|Paddy3118]] ([[User talk:Paddy3118|talk]]) 05:02, 17 April 2013 (UTC)
:: A circle with zero radius is still a perfectly valid circle, I don't see why it should be excluded. --[[User:Ledrug|Ledrug]] ([[User talk:Ledrug|talk]]) 19:55, 17 April 2013 (UTC)

Revision as of 19:55, 17 April 2013

More special cases

There may be more special cases. If p1==p2 and r==0, there is one unique answere that's a zero radius circle. If tow points are separated by exactly double the radius, there's only one answer. The latter can be treated as two identical circles, but then so can the former. <lang python>from math import sqrt

def find_center(p1, p2, r):

   if p1 == p2:
       if r == 0: return [p1] # special special case
       # maybe we can return a generator that yields random circles, eh?
       raise ValueError("infinite many answers")
   (x1,y1), (x2,y2) = p1, p2
   x, y = (x1 + x2)/2.0, (y1 + y2)/2.0
   dx, dy = x1 - x, y1 - y
   a = r*r / (dx*dx + dy*dy) - 1
   if not a: return [(x0, y0)]
   if a < 0: return []
   a = sqrt(a)
   return [(x + a*dy, y - a*dx), (x - a*dy, y + a*dx)]

print find_center((0, 0), (1, 1), 1) # normal case print find_center((0, 0), (0, 0), 0) # special case 1 print find_center((0, 0), (0, 2), 1) # special case 2</lang>

Hi Ledrug. I have one of those covered - two points on a diameter is tested by the current second set of inputs. I'll have to adjust for the two coincident points with r == 0.0 case. Thanks.
Hmm r==0.0 might be treated as an exception too as it is the circle as a point, (If you don't want points). --Paddy3118 (talk) 05:02, 17 April 2013 (UTC)
A circle with zero radius is still a perfectly valid circle, I don't see why it should be excluded. --Ledrug (talk) 19:55, 17 April 2013 (UTC)