Circles of given radius through two points
Given two points on a plane and a radius, at most two circles of given radius can be drawn through the points.
- Write a function/subroutine/method/... that takes two points and a radius and returns the circles through those points.
- Show here the output for the following inputs:
Circles of given radius through two points is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
p1 p2 r 0.1234, 0.9876 0.8765, 0.2345 2.0 0.1234, 0.9876 0.1234, 0.9876 2.0 0.1234, 0.9876 0.8765, 0.2345 0.5
- Ref
- Finding the Center of a Circle from 2 Points and Radius from Math forum @ Drexel
Python
<lang python>from collections import namedtuple from math import sqrt
Pt = namedtuple('Pt', 'x, y') Circle = Cir = namedtuple('Circle', 'x, y, r')
def circles_from_p1p2r(p1, p2, r):
'Following explanation at http://mathforum.org/library/drmath/view/53027.html' (x1, y1), (x2, y2) = p1, p2 if p1 == p2: return Cir(x1, y1, r), Cir(x1, y1, r) # delta x, delta y between points dx, dy = x2 - x1, y2 - y1 # dist between points q = sqrt(dx**2 + dy**2) if q > 2.0*r: # raise ValueError('separation of points > diameter') return None, None # halfway point x3, y3 = (x1+x2)/2, (y1+y2)/2 # distance along the mirror line d = sqrt(r**2-(q/2)**2) # One answer c1 = Cir(x = x3 - d*dy/q, y = y3 + d*dx/q, r = abs(r)) # The other answer c2 = Cir(x = x3 + d*dy/q, y = y3 - d*dx/q, r = abs(r)) return c1, c2
if __name__ == '__main__':
for p1, p2, r in [(Pt(0.1234, 0.9876), Pt(0.8765, 0.2345), 2.0), (Pt(0.1234, 0.9876), Pt(0.1234, 0.9876), 2.0), (Pt(0.1234, 0.9876), Pt(0.8765, 0.2345), 0.5)]: print('Through points:\n %r,\n %r\n and radius %f\nYou can construct the following circles:' % (p1, p2, r)) print(' %r\n %r\n' % circles_from_p1p2r(p1, p2, r))</lang>
- Output:
Through points: Pt(x=0.1234, y=0.9876), Pt(x=0.8765, y=0.2345) and radius 2.000000 You can construct the following circles: Circle(x=1.8631118016581893, y=1.974211801658189, r=2.0) Circle(x=-0.8632118016581896, y=-0.7521118016581892, r=2.0) Through points: Pt(x=0.1234, y=0.9876), Pt(x=0.1234, y=0.9876) and radius 2.000000 You can construct the following circles: Circle(x=0.1234, y=0.9876, r=2.0) Circle(x=0.1234, y=0.9876, r=2.0) Through points: Pt(x=0.1234, y=0.9876), Pt(x=0.8765, y=0.2345) and radius 0.500000 You can construct the following circles: None None