问题描述
试图找到一组点,使一个点看似连接到一个圆,如下图所示,但是没有重叠,它与圆的边缘对齐。抱歉,如果这很难理解,很难解释。
解决方法
如果你想要这样的东西:
那么下面的python代码就是这样做的。
import numpy as np
# function that calcuates the points at which the lines are tangent to the circle
def tangent_points(Point,Circle):
Rot_90 = np.array([[0,-1],[1,0]])
O = Circle[0]
r = Circle[1]
unit_OP = Point - O
OP = np.sqrt( unit_OP.dot(unit_OP) )
unit_OP = unit_OP / OP
a = r**2 / OP
unit_perp = Rot_90.dot(unit_OP)
b = np.sqrt(r**2 - a**2)
return O + a*unit_OP + b*unit_perp,O + a*unit_OP - b*unit_perp
# Test example
O = np.array([0,0])
r = 2
P = np.array([7,5])
Circ = (O,r)
T1,T2 = tangent_points(P,Circ)
# plotting preparations:
# prepare circle
s = np.linspace( 0,2 * np.pi,150 )
xc = O[0] + r*np.cos( s )
yc = O[1] + r*np.sin( s )
# prepare tangents
s = np.linspace( 0,1,150)
L1 = P[:,np.newaxis]*(1-s[np.newaxis,:]) + T1[:,np.newaxis]*s[np.newaxis,:]
L2 = P[:,:]) + T2[:,:]
# actual plotting
# setup a figure environment
figure,axes = plt.subplots()
axes.set_aspect( 1 )
# plotting circle and center
axes.plot( xc,yc )
axes.plot(O[0],O[1],'bo')
# plotting point ouside circle
axes.plot(P[0],P[1],'bo')
# plotting tagnetn lines
axes.plot( L1[0],L1[1] )
axes.plot( L2[0],L2[1] )
# plotting tangent points
axes.plot(T1[0],T1[1],'ro')
axes.plot(T2[0],T2[1],'ro')
plt.show()