0

如果我有 3D 在线点的 x,y 坐标,我需要使用 python 获取这个 z 坐标,我该怎么做?我有 3D 线的起点 (x1,y1,z1) 和终点 (x2,y2,z2),所以有什么建议吗???

4

2 回答 2

4

您可以轻松地为线设置参数方程:

x = x1 + (x2 - x1)*t
y = y1 + (y2 - y1)*t
z = z1 + (z2 - z1)*t

所以给定一个 (x,y),求解这个方程t

x = x1 + (x2 - x1)*t

所以

t = (x - x1) / (x2 - x1)

或者

t = (y - y1) / (y2 - y1)

现在您知道了t,您可以找到z

z = z1 + (z2 - z1)*t

所以在Python中它会是:

def computeZ(p1,p2,x,y):
    x1,y1,z1 = p1
    x2,y2,z2 = p2

    if x2 - x1 != 0:
        t = (x - x1) / (x2 - x1)
    elif:
        t = (y - y1) / (y2 - y1)
    else:
        print "no unique z value exists"
        return 0

    return z1 + (z2 - z1)*t
于 2013-07-22T17:43:02.650 回答
1

You only need to know either the X or the Y value, not both. The equation, if you have an X value, A, would be:

((A - x1)*(z2 - z1)/(x2 - x1)) + z1

Using this, you can plug in your two initial points and an x and get the new point like so:

def get_points(p1, p2, x):
    x1, y1, z1 = p1
    x2, y2, z2 = p2

    new_z = ((x - x1)*(z2 - z1)/(x2 - x1)) + z1
    new_y = ((x - x1)*(y2 - y1)/(x2 - x1)) + y1

    new_p = (x, new_y, new_z)
    return new_p
于 2013-07-22T17:45:29.523 回答