如果你想要这样的东西:
然后下面的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()
@Futurlogger,我们如何用 Javascript 编写这个?