简体   繁体   中英

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. 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). This problem is close to previously discussed one at here . I am looking for a working script in python for the 3D coordinates. Thanks

Thanks MedAli for the suggestion. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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