简体   繁体   中英

Calculating tangent circle formula of line and circle by Python

I have line and circle model (I know the equation that is formed by using data). I would like to find tangent points or equation of yellow circle (radius is determined by me) which tangent with my line and circle.

At the beginning I tried to solve mutual equation separately line and yellow circle and circle and yellow circle reduce to 3 equation to 2 equation but I couldn't find a solution maybe calculation error.

Is there any more clear way or opinion to find these points by python?

For example:

Circle-1: (x-8,98)^2 + (y-42,53)^2 = 6,4^2

Line-1: y=22,904x-115,9707

Tangent Circle: (xa)^2 + (yb)^2 = 14^2

Tangent points (Founded in AutoCAD):

X1=6,2028 Y1=26,0978

X2=12,4953 Y2=37,1832

在此处输入图像描述

Your problem has multiple solutions.

You have 6 unknowns , so the simplest way to solve the problem (not necessarily the fastest) is to craft 6 equations using the constraints defined. After that, you can use a symbolic Math engine like Sympy to solve the system of equations.

Let's start by defining the equations.

  • We know that the line-2 between P1 and the center of the second circle (its center is hereafter noted as (x_a2, ya_2)) is perpendicular to the line 1 whose slope is equal to a. So the slope of the perpendicular line-2 is equal to -1/a . And since A2 and P1 are on line-2 , we have the following

在此处输入图像描述

  • P1 is on circle-2, so

在此处输入图像描述

  • P1 is on line-1, so

在此处输入图像描述

  • We know that the Euclidean distance between the center of the two circles is equal to r1 + r2, so

在此处输入图像描述

  • P2 is on circle-1,

在此处输入图像描述

  • P2 is on circle-2,

在此处输入图像描述

We have finally the 6 equations, so we can use Sympy to solve the system of equations.

I recommend running the following code on a Jupyter notebook

#%%
#imports

import sympy as sp
from sympy import Eq
from sympy.physics.mechanics import dynamicsymbols
from sympy.physics.vector import init_vprinting
init_vprinting(use_latex='mathjax', pretty_print=False)

#%%
#Unknows and variables definition 

x_a1, y_a1, r_1, r_2, a, b = 8.98, 42.53, 6.4, 14, 22.904, -115.97

x_a2, y_a2, x_p1, y_p1, x_p2, y_p2 = dynamicsymbols('x_a2 y_a2 x_p1 y_p1 x_p2 y_p2')

#%%
#equations definition

eq1 = Eq(y_p1-y_a2,(1/a)*(x_a2-x_p1))

eq2 = Eq((x_p1-x_a2)**2+(y_p1-y_a2)**2,r_2**2)

eq3 = Eq(y_p1, a * x_p1 + b)

eq4 = Eq((x_a2-x_a1)**2+(y_a2-y_a1)**2,(r_1 + r_2)**2)

eq5 = Eq((x_p2-x_a1)**2+(y_p2-y_a1)**2,r_1**2)

eq6 = Eq((x_p2-x_a2)**2+(y_p2-y_a2)**2,r_2**2)

eq4 = sp.expand(eq4)

eq5 = sp.expand(eq5)

#%%
#solve the system

sp.solve((eq1,eq2,eq3,eq4,eq5,eq6), (x_a2, y_a2, x_p1, y_p1, x_p2, y_p2))

Here are the solutions (in this order: [x_a2, y_a2, x_p1, y_p1, x_p2, y_p2]) I got,

[(−7.61137900888098, 30.6604531432888, 6.37529636566885, 30.0497879592794, 3.77486148740989, 38.8062205939729), (−6.51375646949495, 55.8003997853864, 7.47291890505488, 55.1897346013771, 4.11921365662903, 46.6932626777683), (20.1893606892033, 25.4856392628135, 6.20268531465346, 26.0963044468229, 12.496662177005, 37.1827495726474), (21.6322053306201, 58.5325529298243, 7.64552995607028, 59.1432181138336, 12.9493193194102, 47.5504087622978)]

The third solution is the one given by AutoCAD (and the one represented in your figure)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM