I have sets of line segments AB1, AB2, ... ABn
. Each one has (Ax, Ay), (Bx, By)
coordinates. Then, I have circle with a center coordinate (Cx, Cy) and r (radius).
Problem: How can I detect which line segment lies on a circle (in figure) OR not? .
I tried to formulate my idea in 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()
Thank you for your help in advance.