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
예제 #2
0
__author__ = 'Jonas I Liechti'
from vecpy import Vector as Vec

# define a vector:
v = Vec(0, 2)  # or Vec((0, 2)) or Vec([0, 2])
w = Vec(1, 3)

# get a vector with twice the lengt (same direction)
v_twice = v ^ 2

# get the unit vector
v_unit = v ^ 0
v_unit = v.unit

# adding scalars and vectors
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