Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Radius first, circumscribed as the magic word solution in Creative category for Three Points Circle by maurice.makaay
from math import sqrt
from operator import add, sub
X, Y, VERTICAL = 0, 1, None
def checkio(data):
p1, p2, p3 = eval(data)
r = _radius_of_circumscribed_circle(p1, p2, p3)
x, y = _center_of_circumscribed_circle(p1, p2, p3, r)
return "(x-{0:g})^2+(y-{1:g})^2={2:g}^2".format(x, y, round(r, 2))
def _radius_of_circumscribed_circle(p1, p2, p3):
# Circumscribed circle: http://en.wikipedia.org/wiki/Circumscribed_circle
a = _distance(p1, p2)
b = _distance(p2, p3)
c = _distance(p3, p1)
area = _triange_area(a, b, c)
return a * b * c / (4 * area)
def _distance(p1, p2):
# Pythagoras: http://en.wikipedia.org/wiki/Pythagorean_theorem
a_square = (p1[X] - p2[X]) ** 2
b_square = (p1[Y] - p2[Y]) ** 2
return sqrt(a_square + b_square)
def _triange_area(a, b, c):
# Heron: http://en.wikipedia.org/wiki/Heron%27s_formula
s = (a + b + c) / 2;
return sqrt(s * (s-a) * (s-b) * (s-c));
def _center_of_circumscribed_circle(p1, p2, p3, radius):
# When drawing circles around two of the points using the provided radius,
# two intersection points can be found. One of these is the center of the
# circle that we're looking for (the circumcenter). By doing this for two
# sets of points, we can find the circumcenter (this one is in both the
# result sets).
i1 = set(_circle_intersections(p1, p2, radius))
i2 = set(_circle_intersections(p2, p3, radius))
return (i1 & i2).pop()
def _circle_intersections(p1, p2, r):
# http://mathforum.org/library/drmath/view/51836.html
(a, b), (c, d) = p1, p2
e = c - a
f = d - b
p = _distance(p1, p2)
k = p ** 2 / (2 * p)
for op in (add, sub):
x = op(a + e*k/p, (f/p) * sqrt(r**2 - k**2))
y = op(b + f*k/p, -(e/p) * sqrt(r**2 - k**2))
yield round(x, 2), round(y, 2)
Nov. 5, 2014
Comments: