python - 在 PyGame 中绘制沿直线方向的箭头

标签 python python-2.7 pygame geometry algebra

在 Pygame 中,给定箭头的起点和终点,如何计算箭头头部三个点的坐标,使箭头指向与直线相同的方向?

def __draw_arrow(self, screen, colour, start, end):     
    start = self.__coordinate_lookup[start]
    end = self.__coordinate_lookup[end]
    dX = start[0] - end[0]
    dY = -(start[1] - end[1])
    print m.degrees(m.atan(dX/dY)) + 360 
    pygame.draw.line(screen,colour,start,end,2)

我尝试过调整角度和线的渐变,Y 坐标向下而不是向上增加这一事实让我失望,我真的很感激向正确的方向轻推。

最佳答案

我将开始和结束坐标表示为 startX, startY, endX, endY enter image description here

dX = endX - startX
dY = endY - startY

//vector length 
Len = Sqrt(dX* dX + dY * dY)  //use Hypot if available

//normalized direction vector components
udX = dX / Len
udY = dY / Len

//perpendicular vector
perpX = -udY
perpY = udX

//points forming arrowhead
//with length L and half-width H
arrowend  = (end) 

leftX = endX - L * udX + H * perpX
leftY = endY - L * udY + H * perpY

rightX = endX - L * udX - H * perpX
rightY = endY - L * udY - H * perpY

关于python - 在 PyGame 中绘制沿直线方向的箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43527894/

相关文章:

python - Pygame Sprite 运动故障

python - 我怎样才能通过 Selenium 点击 "javascript:void(0)"

python - 在 Python 中编写从 SPARQL 查询返回的 JSON 数据的正确方法

python - 将 matplotlib 安装到 pypy

python - Django 1.5 : Display foreignkey values in both admin models

python - Pygame在子目录中找不到图像文件夹

Python:使用 cerberus 进行验证

python-2.7 - 理解 python 中的 *[] 传递给 pyspark 中的 .agg()

python 2.7 : remove a key from a dictionary by part of key

python - 空白 Pygame 屏幕