Find the intersection of a circle and a straight line (sympy matrix)

(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.

Substitute a value from the beginning

#[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																																																										

Substitute the value later (under construction)

#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

It is a solve method and an intersection method with class without using a matrix. There are two ways.

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

Find the intersection of a circle and a straight line (sympy matrix)
I want to find the intersection of a Bezier curve and a straight line (Bezier Clipping method)
Find the eigenvalues of a real symmetric matrix in Python
Find the point of contact of the common tangent of two circles (sympy matrix)
Find the rank of a matrix in the XOR world (rank of a matrix on F2)
[Python] A simple function to find the center coordinates of a circle
Find the number of days in a month
[Python] Find the transposed matrix in a comprehension
A discussion of the strengths and weaknesses of Python
Python --Read data from a numeric data file and find the multiple regression line.
Be careful when differentiating the eigenvectors of a matrix
Find the Hermitian matrix and its eigenvalues in Python
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
[Python] A program to find the number of apples and oranges that can be harvested
Find out the apparent width of a string in python
A rough summary of the differences between Windows and Linux
How to find the scaling factor of a biorthogonal wavelet
I made a Line bot that guesses the gender and age of a person from an image
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
Find out the age and number of winnings of prefectural governors nationwide
Find the optimal value of a function with a genetic algorithm (Part 2)
How to find the memory address of a Pandas dataframe value
Dig the directory and create a list of directory paths + file names
Find the distance from latitude and longitude (considering the roundness of the earth).
Find the waypoint from latitude and longitude (considering the roundness of the earth).
Use AWS lambda to scrape the news and notify LINE of updates on a regular basis [python]
Find the definition of the value of errno
The story of Python and the story of NaN
Danger of mixing! ndarray and matrix
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
Find the inertial spindle and moment of inertia from the inertial tensor with NumPy
Find the general terms of the Tribonacci sequence with linear algebra and Python
The story of making a sound camera with Touch Designer and ReSpeaker
Find the minimum value of a function by particle swarm optimization (PSO)
Make a DNN-CRF with Chainer and recognize the chord progression of music
A story about calculating the speed of a small ball falling while receiving air resistance with Python and Sympy
Gently explain the process of making a simple serverless surveillance camera using Raspberry Pi, Gmail API and Line API