简体   繁体   English

如何找到从附近点到 3D 线的截点坐标?

[英]How to find coordinates of intercept point from a nearby point to a line in 3D?

I have two points on a line, say X1(x1,y1,z1) and X2(x2,y2,z2) and I have an external point X(x0,y0,z0) nearby this line.我在一条线上有两个点,比如 X1(x1,y1,z1) 和 X2(x2,y2,z2),我在这条线附近有一个外部点 X(x0,y0,z0)。 I want to write a python code that will return me coordinate of point D(x,y,z) on the line X1X2 such that line XD is perpendicular to X1X2 (projection).我想编写一个 python 代码,它将返回我在 X1X2 线上的点 D(x,y,z) 的坐标,使得 XD 线垂直于 X1X2(投影)。 This problem is close to previously discussed one at here .这个问题与之前在这里讨论的问题很接近。 I am looking for a working script in python for the 3D coordinates.我正在为3D坐标寻找 Python 中的工作脚本。 Thanks谢谢

Thanks MedAli for the suggestion.感谢MedAli的建议。 I end-up with the following solution:我最终得到以下解决方案:

    """ Calculates coordinates of projection point of a P0
    point on the line is the line(p1,p2) """
    p = np.array(p0)
    a = np.array(p1)
    b = np.array(p2)
    ap = p - a
    ab = b - a

    proPoint = a + np.dot(ap, ab)/np.dot(ab,ab) * ab
    return proPoint

You can use the scikit-spatial library.您可以使用scikit-spatial库。

Having two points, you can define the line in the following way:有两个点,您可以通过以下方式定义线:

from skspatial.objects import Line, Point

point_x1 = Point([3, 6, 1])
point_x2 = Point([2, 1, 10])

line = Line.from_points(point_x1, point_x2)

Then the projected point on the line can be computed as:然后在线上的投影点可以计算为:

point_x = Point([0, -1, -7])
point_xd = line.project_point(point_x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM