線分のセットがあり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()
よろしくお願いいたします。