1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

円と直線の交点を求める(sympyの行列) Intersection of Circle and Line

Last updated at Posted at 2020-05-06

参考

1行で、できそうな気もします。

最初から値を代入

# 【例】中心(3,2) 半径 5 の円と、直線 3x+2y-16=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																																																										

後から値を代入(製作中)

# 円と直線の交点を求める(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)																													
#結果省略																													
#続く

--------------
【補足】(2020/05/07)

行列を使わず、solveの方法と classでintersectionの方法です。2通りです。

stackoverflow本家さんで教えてもらいました。

(参考)

Tupleだそうです


# 【例】中心(3,2) 半径 5 の円と、直線 3x+2y-16=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)
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?