math - 你如何在数学上从一个点找到一个圆中的外部点?
解决方案
如果你想要这样的东西:
然后下面的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[:, np.newaxis]*(1-s[np.newaxis, :]) + T2[:, np.newaxis]*s[np.newaxis, :]
# 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()
推荐阅读
- ruby-on-rails - rspec + rails_admin 在 /lib 中测试自定义 rails_admin 操作会出错
- javascript - 点击外部选择标签不会触发点击事件
- ag-grid - angular 6 ag-grid 企业 19.0.0
- css - Angular 6:ngx-translate 不适用于工具提示的 data-title 属性
- python - AttributeError:模块'networkx'在vs代码中没有属性'Graph'
- python - 如何为 3D 体积阵列恢复(重新打包)view_as_block?
- r - 置换方形 2 路列联表(矩阵)的列以最大化其对角线
- c++ - Vulkan:vkCreateGraphicsPipelines:访问冲突读取位置 0x00000016.(nvoglv32.dll)
- java - 在不使用上下文的情况下加载自定义字体
- python - 即使使用 .strip、.replace 和一切可能摆脱它们的文件,在保存到文件时也会不断添加空格