1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import math
def calc_circles_insec(p1, r1, p2, r2): x1 = p1[0] y1 = p1[1] x2 = p2[0] y2 = p2[1] d = math.sqrt((abs(x2 - x1)) ** 2 + (abs(y2 - y1)) ** 2) if d > (r1 + r2) or d < (abs(r1 - r2)): print("Two circles have no intersection") return None, None elif d == 0: print("Two circles have same center!") return None, None else: A = (r1 ** 2 - r2 ** 2 + d ** 2) / (2 * d) h = math.sqrt(r1 ** 2 - A ** 2) x_g = x1 + A * (x2 - x1) / d y_g = y1 + A * (y2 - y1) / d x3 = round(x_g - h * (y2 - y1) / d, 2) y3 = round(y_g + h * (x2 - x1) / d, 2) x4 = round(x_g + h * (y2 - y1) / d, 2) y4 = round(y_g - h * (x2 - x1) / d, 2) c1 = [x3, y3] c2 = [x4, y4] return c1, c2
print(calc_circles_insec([0, 0], 3, [2, 0], 0.5)) print(calc_circles_insec([0, 0], 3, [0, 0], 1)) print(calc_circles_insec([0, 0], 3, [2, 0], 1)) print(calc_circles_insec([0, 0], 3, [4, 0], 2))
|