SZanlongo
12/1/2017 - 4:24 PM

Point along edge given distance from start

import math
from operator import add


def steer(start: tuple, goal: tuple, distance: float) -> tuple:
    """
    Return a point in the direction of the goal, that is distance away from start
    :param start: start location
    :param goal: goal location
    :param distance: distance away from start
    :return: point in the direction of the goal, distance away from start
    """
    ab = []  # difference between start and goal
    for start_i, goal_i in zip(start, goal):
        ab.append(goal_i - start_i)

    ab = tuple(ab)
    zero_vector = tuple([0] * len(ab))

    ba_length = distance_between_points(zero_vector, ab)  # get length of vector ab
    unit_vector = [i / ba_length for i in ab]  # normalize
    scaled_vector = [i * distance for i in unit_vector]  # scale vector to desired length

    steered_point = list(map(add, start, scaled_vector))  # add scaled vector to starting location for final point
    steered_point = tuple(steered_point)

    return steered_point


def distance_between_points(a: tuple, b: tuple) -> float:
    """
    Return the Euclidean distance between two points
    :param a: first point
    :param b: second point
    :return: Euclidean distance between a and b
    """
    distance = 0
    for a_i, b_i in zip(a, b):
        distance += (a_i - b_i) ** 2

    distance = math.sqrt(distance)

    return distance


print(steer((0, 0), (10, 10), 5))