在其身体上移动箭头

时间:2018-09-13 12:23:42

标签: python vector trigonometry vertices

我创建了以下函数,该函数获取两个线点并输出箭头三角形的三个点。我需要添加一个长度,箭头将根据该长度从线AB的B点移开。为了更好地解释它,我希望这样:A ----->-B,其中两个破折号等于长度

def create_arrowhead(A, B):
    """
    Calculate the arrowheads vertex positions according to the edge direction.

    Parameters
    ----------
    A : array
    x,y Starting point of edge
    B : array
    x,y Ending point of edge

    Returns
    -------
    B, v1, v2 : tuple
    The point of head, the v1 xy and v2 xy points of the two base vertices of the arrowhead.
    """
    w = 0.005 # half width of the triangle base
    h = w * 0.8660254037844386467637  # sqrt(3)/2

    mag = math.sqrt((B[0] - A[0])**2.0 + (B[1] - A[1])**2.0)

    u0 = (B[0] - A[0]) / (mag)
    u1 = (B[1] - A[1]) / (mag)
    U = [u0, u1]
    V = [-U[1], U[0]]
    v1 = [B[0] - h * U[0] + w * V[0], B[1] - h * U[1] + w * V[1]]
    v2 = [B[0] - h * U[0] - w * V[0], B[1] - h * U[1] - w * V[1]]

    return (B, v1, v2)

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。通过将距离变量与单位矢量的分量相乘,然后从B的坐标中减去此结果。这将在AB线上创建一个点,该点与B的距离为d。

def create_arrowhead(A, B, d):
"""
Use trigonometry to calculate the arrowheads vertex positions according to the line direction.

Parameters
----------
A : array
    x,y Starting point of line segment
B : array
    x,y Ending point of line segment

Returns
-------
C, v1, v2 : tuple
    The point of head with distance d from point B, the v1 xy and v2 xy points of the two base vertices of the arrowhead.
"""
w = 0.003 # Half of the triangle base width
h = w / 0.26794919243 # tan(15)

AB = [B[0] - A[0], B[1] - A[1]]
mag = math.sqrt(AB[0]**2.0 + AB[1]**2.0)

u0 = AB[0] / mag
u1 = AB[1] / mag
U = [u0, u1] # Unit vector of AB

V = [-U[1], U[0]] # Unit vector perpendicular to AB

C = [ B[0] - d * u0, B[1] - d * u1 ]

v1 = [C[0] - h * U[0] + w * V[0], C[1] - h * U[1] + w * V[1]]
v2 = [C[0] - h * U[0] - w * V[0], C[1] - h * U[1] - w * V[1]]

return (C, v1, v2)