(Reference) Find the intersection of a circle and a straight line https://qiita.com/tydesign/items/36b42465b3f5086bd0c5 (Reference) The intersection of a circle and a straight line with sympy https://qiita.com/mrrclb48z/items/2d5bd66507166913573b
I feel like I can do it in one line.
#[Example] Center(3,2)A circle with a radius of 5 and a straight line 3x+2y-16=Find the intersection of 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
#Find the intersection of a circle and a straight line(sympy matrix)
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)
#Result omitted
#Continue
-------------- Supplement
I was taught by stackoverflow Honke. (Reference) sympy solve intersection line circle ---> why AttributeError:'tuple' object has no attribute'subs' https://stackoverflow.com/questions/61634904/sympy-solve-intersection-line-circle-why-attributeerror-tuple-object-has-n It ’s Tuple.
#[Example] Center(3,2)A circle with a radius of 5 and a straight line 3x+2y-16=Find the intersection of 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