Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Perpendicular solution in Uncategorized category for Three Points Circle by bryukh
import re
from math import hypot
def perpendicular(p1, p2):
"""Search cooficents for equation of line perpendicular for given"""
#Ax+By=C
#A=y2-y1
#B=x1-x2
#C=A*x1+B*y1
#Midpoint x12
a, b = p2[1] - p1[1], p1[0] - p2[0]
c = a * p1[0] + b * p1[1]
m = (p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2
d = -b * m[0] + a * m[1]
return -b, a, d
def checkio(data):
x1, y1, x2, y2, x3, y3 = (int(ch) for ch in re.findall(r"\d+", data))
#search median perpendicular
a12, b12, d12 = perpendicular((x1, y1), (x2, y2))
a13, b13, d13 = perpendicular((x1, y1), (x3, y3))
#Search intersection
determinant = a12 * b13 - a13 * b12
#if determinant == 0, then lines are parallel
if not determinant:
return None
raise ValueError("The points lies at the same line")
x0 = (b13 * d12 - b12 * d13) / determinant
y0 = (a12 * d13 - a13 * d12) / determinant
r = hypot(x1 - x0, y1 - y0)
#rounding
x0 = "{:+.2f}".format(-x0).rstrip("0").rstrip(".")
y0 = "{:+.2f}".format(-y0).rstrip("0").rstrip(".")
r = "{:.2f}".format(r).rstrip("0").rstrip(".")
return "(x{0})^2+(y{1})^2={2}^2".format(x0, y0, r)
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio("(2,2),(4,2),(2,4)") == "(x-4)^2+(y-4)^2=2.82^2"
assert checkio("(3,7),(6,9),(9,7)") == "(x-6)^2+(y-5.75)^2=3.25^2"
June 13, 2013
Comments: