我有几组线段AB1, AB2, ... ABn
。每个都有(Ax, Ay), (Bx, By)
坐标。然后,我有一个中心坐标(Cx,Cy)和r(半径)的圆。
问题: 如何检测哪条线段位于圆上(如图)?.
我试图用 Python 来表达我的想法:
import numpy as np
import pylab as plt
def detect(A,B, C, r):
'''
Returns 'True' if line is inside or intersected the circle, otherwise 'False'.
Inputs:
- A - segment line coordinate (Ax, Ay)
- B - segment line coordinate (Bx, By)
- C - circle coordinate (Cx, Cy)
- r - circle radius
'''
# Do process for detection
return (boolean)
def plot_detected(An, Bn, C, r):
'''
Plots newly detected line segments with red color
while the rest remains with blue color
'''
plt.figure(1)
plt.subplot(111)
for A, B in zip(An, Bn):
if detect(A, B, C, r):
line1, = plt.plot([ A[0], B[0] ], [ A[1], B[1] ], 'ro-')
else:
line2, = plt.plot([ A[0], B[0] ], [ A[1], B[1] ], 'bo-')
pl.legend([line1, line2], ('detected','un-detected'))
plt.show()
def main():
C = [18.5, 18.5]
r = 2.4
Ax = np.array([16.2, 17.2, 22.2, 18.2, 23.8, 18.8])
Ay = np.array([22.8, 20.6, 23.8, 18.4, 20.8, 22.8])
Bx = np.array([21.8, 19.8, 18.2, 19.8, 17.2, 22.8])
By = np.array([17.8, 17.2, 19.2, 19.2, 16.8, 20.8])
An = np.vstack([Ax, Ay]).T
Bn = np.vstack([Bx, By]).T
plot_detected(An, Bn, C, r)
if __name__ == '__main__':
main()
提前谢谢你的帮助。