Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的线交点与numpy_Python_Numpy_Vector - Fatal编程技术网

Python中的线交点与numpy

Python中的线交点与numpy,python,numpy,vector,Python,Numpy,Vector,我有一个相对简单的问题,我知道答案,但我似乎找不到使用Python和Numpy的正确实现。我的想法是,我有两条线,我需要找到虚拟交点(我使用的例子来自) 两条直线的形式都是r=r0+t*V,其中r0作为位置向量(直线通过的点),t是变量,V是方向向量。方向向量V可以简单地通过直线的两个点找到向量,例如V=A-B 有了它,就可以把这条线表述为: L1=r0(已知点)+t(未知变量)*V(方向向量) 现在,我可以很容易地手动找到t,但我不知道如何告诉Python。。我尝试了numpy.linalg.

我有一个相对简单的问题,我知道答案,但我似乎找不到使用Python和Numpy的正确实现。我的想法是,我有两条线,我需要找到虚拟交点(我使用的例子来自)

两条直线的形式都是r=r0+t*V,其中r0作为位置向量(直线通过的点),t是变量,V是方向向量。方向向量V可以简单地通过直线的两个点找到向量,例如V=A-B

有了它,就可以把这条线表述为:
L1=r0(已知点)+t(未知变量)*V(方向向量)

现在,我可以很容易地手动找到t,但我不知道如何告诉Python。。我尝试了numpy.linalg.solve,但这给了我一个矩阵,而我需要一个值

例如:

# Line A and Line B that intersect somewhere
A = LineString([(4., 0.), (4., -3.)])
B = LineString([(6., 2.), (10., 2.)])

# direction vectors for line A and B
v1 = (A[0].x - A[1].x, A[0].y, A[1].y) # --> [0,3]
v2 = (B[0].x - B[1].x, B[0].y, B[1].y) # --> [-4,0]

L1 = A[1] + x * v1
L2 = B[1] + y * v2
现在,我将通过解L1 for x手动解决这个问题:

L1 = [4, 0] + x * [0, 3] = [4, 3x]
L2 = [6, 2] + y * [-4, 0] = [6-4y, 2]

# finding intersection point by solving L1 = L2
4 = 6-4y  &  3x = 2
y = 1/2   &   x = 2/3
但是我不知道如何告诉numpy/python如何求解x和y


如能提供正确方向的任何帮助或指导,将不胜感激。

穿过A0和A1的直线具有参数方程
(1-t)*A0+t*A1
,其中t是参数。 穿过B0和B1的线具有参数方程
(1-s)*A0+s*A1
,其中s是参数。设置这些相等,我们得到系统
(A1-A0)t+(B0-B1)s==B0-A0
。因此,右边是B0-A0,矩阵有列A1-A0和B0-B1。该系统可用
np.linalg.solve
进行求解。完整示例:

A = np.array([[4, 0], [4, -3]])
B = np.array([[6, 2], [10, 2]])
t, s = np.linalg.solve(np.array([A[1]-A[0], B[0]-B[1]]).T, B[0]-A[0])
print((1-t)*A[0] + t*A[1])
print((1-s)*B[0] + s*B[1])

两个打印命令都输出确认正确性的
[4,2.]
。(第二次打印时,它确实是多余的。)

我编写了一个函数,用于查找两条3d线之间的最近点

import scipy.optimize
#takes in two lines, the line formed by pt1 and pt2, and the line formed by pt3 and pt4, and finds their intersection or closest point
def fourptsMeetat(pt1,pt2,pt3,pt4):
    #least squares method
    def errFunc(estimates):
        s, t = estimates
        x = pt1 + s * (pt2 - pt1) - (pt3 + t * (pt4 - pt3))
        return x

    estimates = [1, 1]

    sols = scipy.optimize.least_squares(errFunc, estimates)
    s,t = sols.x

    x1 =  pt1[0] + s * (pt2[0] - pt1[0])
    x2 =  pt3[0] + t * (pt4[0] - pt3[0])
    y1 =  pt1[1] + s * (pt2[1] - pt1[1])
    y2 =  pt3[1] + t * (pt4[1] - pt3[1])
    z1 =  pt1[2] + s * (pt2[2] - pt1[2])
    z2 = pt3[2] + t * (pt4[2] - pt3[2])

    x = (x1 + x2) / 2  #halfway point if they don't match
    y = (y1 + y2) / 2  # halfway point if they don't match
    z = (z1 + z2) / 2  # halfway point if they don't match

    return (x,y,z)

相关报道:你说的是“和努比在一起”,但我在这里没有看到任何努比。你想要一个简单的解决方案吗?或者这是没有必要的?如果可能的话,没有,很好,但我已经尝试了Numpy.linalg.solve,所以我认为Numpy会有功能来做这样的事情。在一般情况下,你只需要手工求解,也就是用你假设已知的变量替换所有数值(比如[4,0])(比如[r0_x,r0_y])这样做,您将得到y作为您知道的几个变量的表达式。