【发布时间】:2014-12-30 19:03:22
【问题描述】:
我有一组线段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()
提前感谢您的帮助。
【问题讨论】:
-
你只是想找到圆中的线段部分吗?或者试图找出哪些线段与圆相交?
-
@Maria 。我正在尝试找出哪些线段不仅相交而且位于圆内。
标签: python numpy intersection clipping line-intersection