gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:39:44 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com