import sympy as sp r,R,d=sp.symbols('r R d', positive=True) # Intersection point A between circles centered at M=(0,0) radius r and N=(d,0) radius R. x0=(r**2 - R**2 + d**2)/(2*d) y0=sp.sqrt(r**2 - x0**2) # Correct choice respecting order C,M,N,D: C=(-r,0), D=(d+R,0). u=(d+R-r)/2 v= -u*(x0+r)/y0 # Direction vector w = P-A wx=u-x0 wy=v-y0 w2=wx**2+wy**2 # Second intersection parameters A_dot= x0*wx + y0*wy AxN=x0-d A_dotN= AxN*wx + y0*wy tE=sp.simplify(-2*A_dot/w2) tF=sp.simplify(-2*A_dotN/w2) # Points Ex=sp.simplify(x0 + tE*wx) Ey=sp.simplify(y0 + tE*wy) Fx=sp.simplify(x0 + tF*wx) Fy=sp.simplify(y0 + tF*wy) Bx=x0 By=-y0 # Orthocenter H of triangle P M N Hx=u Hy=sp.simplify(-u*(u-d)/v) # Circle through B,E,F: x^2+y^2+αx+βy+γ=0 α,β,γ=sp.symbols('α β γ') def eq_point(x,y): return sp.Eq(x**2+y**2 + α*x + β*y + γ, 0) sol=sp.solve([eq_point(Bx,By), eq_point(Ex,Ey), eq_point(Fx,Fy)], [α,β,γ], simplify=True) αs,βs,γs=sol[α],sol[β],sol[γ] # Circle center/radius^2 xc=-αs/2 yc=-βs/2 rad2=sp.simplify(xc**2+yc**2-γs) # Line through H parallel to AP: direction w, normal n=(wy,-wx) a=wy b=-wx c= -wy*Hx + wx*Hy # Tangency condition (cleared denominators) expr=sp.simplify((a*xc + b*yc + c)**2 - (a**2+b**2)*rad2) print(expr)