def point2segment_dist(point, segment):
    """
    Return the distance between a segment of a line and a point

    :param point:
    :param segment:
    :return:
    """
    sp_vec = Vec(segment[0], point)  # create a vector from start of the segment to the point
    seg_vec = Vec(segment)  # create a vector along the segment
    proj_scale = seg_vec.proj(sp_vec, True)  # project the sp_vector to the segment vector and get the ration of the
    # length of those two parallel vectors
    if proj_scale <= 0:  # if the projection has not the same direction
        dist = sp_vec.length
    elif proj_scale >= 1:  # if the projection is longer than the segment vector
        dist = Vec(segment[1], point).length
    else:  # get the length from the part of sg_vec orthogonal to the segment vector
        ortho_v = seg_vec.proj(sp_vec) - sp_vec
        dist = ortho_v.length
    return dist
Exemple #2
0
v + 2  # adds 2 to every coord
v + w  # adds coordinate by coordinate

# multiply by scalar
v * 3  # or 3 * v

# dot product
v.dot(w)

# get a norm
v.norm('inf')  # the default is the Euclidean norm (p=2)

# get the length of a vector
v.length  # this is just v.norm(2)

# get the dimension
v.dim

# project one vector onto another
w_proj_v = v.proj(w)

# get length ration of a vector and the projectoin of another vector onto it
ratio = v.proj(w, get_scale=True)

# iterate through coordinates
print [xi for xi in v]

# string representation
print str(v)
print '{:[x, y, z]}'.format(v)