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