Incorrect value return by Python in Dynamo, but correct in another IDE, a bug?

Hey guys, I cross post this from stackoverflow, initially I thought there was a problem in my code, but in the end it’s something wrong with Python in Dynamo. You can see my pose here:

In brief, I’m trying to calculate the perpendicular distance between a point a and a line, and Dynamo gave me a value that’s very large (27111), while the correct value (returned by other python IDE) is 103. Anyone know why is that? Is this a bug?

I’m using Dynamo 1.3.2 in Revit 2016.

import math
#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN


def dot(v,w):
    x,y,z = v
    X,Y,Z = w
    return x*X + y*Y + z*Z

def length(v):
    x,y,z = v
    return math.sqrt(x*x + y*y + z*z)

def vector(b,e):
    x,y,z = b
    X,Y,Z = e
    return (X-x, Y-y, Z-z)

def unit(v):
    x,y,z = v
    mag = length(v)
    return (x/mag, y/mag, z/mag)

def distance(p0,p1):
    return length(vector(p0,p1))

def scale(v,sc):
    x,y,z = v
    return (x * sc, y * sc, z * sc)

def add(v,w):
    x,y,z = v
    X,Y,Z = w
    return (x+X, y+Y, z+Z)

def pnt2line(pnt, start, end):
    line_vec = vector(start, end)
    pnt_vec = vector(start, pnt)
    line_len = length(line_vec)
    line_unitvec = unit(line_vec)
    pnt_vec_scaled = scale(pnt_vec, 1.0/line_len)
    t = dot(line_unitvec, pnt_vec_scaled)    
    if t < 0.0:
        t = 0.0
    elif t > 1.0:
        t = 1.0
    nearest = scale(line_vec, t)
    dist = distance(nearest, pnt_vec)
    nearest = add(nearest, start)
    return (dist,nearest)

p0=[-93011.819, 510935.550, 84400.000]
p1=[-94493.434, 510660.112, 84400.000]
p2=[-80724.587, 514213.145, 84400.000]
result=pnt2line(p0,p1,p2)


#Assign your output to the OUT variable.
OUT = result

image

Worked fine for me…

2 Likes