(Référence) Trouvez l'intersection d'un cercle et d'une ligne droite https://qiita.com/tydesign/items/36b42465b3f5086bd0c5 (Référence) L'intersection d'un cercle et d'une ligne droite avec sympy https://qiita.com/mrrclb48z/items/2d5bd66507166913573b
Je sens que je peux le faire en une seule ligne.
#[Exemple] Centre(3,2)Un cercle avec un rayon de 5 et une ligne droite 3x+2y-16=Trouvez l'intersection de 0
from sympy import *																													
var('x0 y0 r x1 y1 x2 y2 c s tx ty d sx sy')																													
x0=3																													
y0=2																													
r=5																													
y1=0																													
v=solve([3*x1+2*y1-16])																													
x1=v[x1]																													
x2=0																													
v=solve([3*x2+2*y2-16])																													
y2=v[y2]																													
d=sqrt((x1-x2)**2+(y1-y2)**2)																													
v=solve([c*0-s*0+tx-x1,s*0+c*0+ty-y1,c*d-s*0+tx-x2,s*d+c*0+ty-y2],[c,s,tx,ty])																													
A=Matrix([																													
        [v[c],-v[s],v[tx]],																													
        [v[s], v[c],v[ty]],																													
        [0   ,    0,    1]																													
])																													
B=Matrix([																													
       [x0],																													
       [y0],																													
       [ 1]																													
])																													
AinvB=A.inv()*B																													
sx=AinvB[0].subs([(c**2 + s**2,1)])																													
sy=AinvB[1].subs([(c**2 + s**2,1)])																													
																													
B=Matrix([																													
         [sx+sqrt(r**2-sy**2)],																													
         [0],																													
         [1]																													
])																													
AB=A*B																													
print(float(AB[0]),float(AB[1]))																													
B=Matrix([																													
         [sx-sqrt(r**2-sy**2)],																													
         [0],																													
         [1]																													
])																													
AB=A*B																													
print(float(AB[0]),float(AB[1]))																													
# 0.9574786408259727 6.563782038761041																													
# 6.427136743789412 -1.640705115684118																																																										
#Trouvez l'intersection d'un cercle et d'une ligne droite(matrice sympy)																													
from sympy import *																													
var('x y x0 y0 r x1 y1 x2 y2 co si tx ty d sx sy a b c')																													
y1=0																													
v=solve([a*x+b*y1-c],[x])																													
x1=v[x]																													
x2=0																													
v=solve([a*x2+b*y-c],[y])																													
y2=v[y]																													
																													
d=sqrt((x1-x2)**2+(y1-y2)**2)																													
v=solve([co*0-si*0+tx-x1,si*0+co*0+ty-y1,co*d-si*0+tx-x2,si*d+co*0+ty-y2],[co,si,tx,ty])																													
A=Matrix([																												
    [v[co],-v[si],v[tx]],																													
    [v[si], v[co],v[ty]],																													
    [0    ,     0,    1]																													
])																													
B=Matrix([																													
    [x0],																													
    [y0],																													
    [ 1]																													
])																													
AinvB=A.inv()*B																													
sx=AinvB[0].subs([(co**2 + si**2,1)])																													
sy=AinvB[1].subs([(co**2 + si**2,1)])																													
																													
B=Matrix([																													
    [sx+sqrt(r**2-sy**2)],																													
    [0],																													
    [1]																													
])																													
AB=A*B																													
print(AB)																													
#Résultat omis
#Continuer
-------------- Supplément
stackoverflow J'ai appris du chef de famille. (Référence) sympy résoudre le cercle de la ligne d'intersection ---> pourquoi AttributeError: l'objet 'tuple' n'a pas d'attribut'subs ' https://stackoverflow.com/questions/61634904/sympy-solve-intersection-line-circle-why-attributeerror-tuple-object-has-n C'est Tuple.
#[Exemple] Centre(3,2)Un cercle avec un rayon de 5 et une ligne droite 3x+2y-16=Trouvez l'intersection de 0
from sympy import *
var('v0 x y x0 y0 r a b c')
v=solve([(x-x0)**2+(y-y0)**2-r**2,a*x+b*y+c],[x,y])
print(Tuple(*v[0]).subs({x0: 3.0, y0: 2.0, r: 5.0, a: 3.0, b: 2.0, c: -16.0}))
print(Tuple(*v[1]).subs({x0: 3.0, y0: 2.0, r: 5.0, a: 3.0, b: 2.0, c: -16.0}))
# (6.42713674378941, -1.64070511568412)
        Recommended Posts