如何找到一条直线和一个曲线点的二维点集的交点? [英] How to find the intersection points of a straight line and a curve-like set of two dimensional points?
本文介绍了如何找到一条直线和一个曲线点的二维点集的交点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们有:
- 由二维数据集描述的曲线,该曲线大致描述了高阶多项式曲线.
- 由两点定义的线.
这是示例图片:
假设直线和曲线相互交叉,如何找到直线和数据集之间的交点?
Supposing the line and the curve intercept each other, how could I find the intersection point between the line and the dataset?
推荐答案
根据我上面的评论
import numpy as np
A = np.random.random((20, 2))
A[:,0] = np.arange(20)
A[:,1] = A[:,1] * (7.5 + A[:,0]) # some kind of wiggly line
p0 = [-1.0,-6.5] # point 0
p1 = [22.0, 20.0] # point 1
b = (p1[1] - p0[1]) / (p1[0] - p0[0]) # gradient
a = p0[1] - b * p0[0] # intercept
B = (a + A[:,0] * b) - A[:,1] # distance of y value from line
ix = np.where(B[1:] * B[:-1] < 0)[0] # index of points where the next point is on the other side of the line
d_ratio = B[ix] / (B[ix] - B[ix + 1]) # similar triangles work out crossing points
cross_points = np.zeros((len(ix), 2)) # empty array for crossing points
cross_points[:,0] = A[ix,0] + d_ratio * (A[ix+1,0] - A[ix,0]) # x crossings
cross_points[:,1] = A[ix,1] + d_ratio * (A[ix+1,1] - A[ix,1]) # y crossings
print(ix, B, A, cross_points)
这篇关于如何找到一条直线和一个曲线点的二维点集的交点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文