Drawing arrowheads which follow the direction of the line in PyGame

python quiver example
how to make a character move in python
pygame move object with arrow keys
matplotlib quiver 3d
matplotlib quiver arrow size
python movement code
how to make an object move in python
pygame move sprite with arrow keys

In Pygame, how can I calculate the coordinates for the three points of the head of an arrow, given a start point and an end point for the arrow, so that the arrowhead points in the same direction as the line?

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 

I've tried playing around with angles and with the gradient of the line, the fact the Y coordinates increase downwards instead of upwards is throwing me off and I would really appreciate a nudge in the right direction.

This should work:

def draw_arrow(screen, colour, start, end):
    rotation = math.degrees(math.atan2(start[1]-end[1], end[0]-start[0]))+90
    pygame.draw.polygon(screen, (255, 0, 0), ((end[0]+20*math.sin(math.radians(rotation)), end[1]+20*math.cos(math.radians(rotation))), (end[0]+20*math.sin(math.radians(rotation-120)), end[1]+20*math.cos(math.radians(rotation-120))), (end[0]+20*math.sin(math.radians(rotation+120)), end[1]+20*math.cos(math.radians(rotation+120)))))

Sorry for the poorly organized code. But as you said, the coordinates starting in the top-left do require some math to be flipped. Also, if you want to change the triangle from being equalatiral to something else you just have to change the rotation +/- 120 in line 4, or the 20* for a different radius.

Hope this helps :)

Python turtle arrowheads not facing in correct direction, Drawing The arrow line brush.penup() brush.goto(StartPointX, StartPointY) brush.pendown() brush.setheading(brush.towards(MaxX, MaxY))� In Pygame, how can I calculate the coordinates for the three points of the head of an arrow, given a start point and an end point for the arrow, so that the arrowhead points in the same direction a

I denote start and end coordinates as 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

Using Pygame to move your gaming player around, In this article, we'll use Pygame to add keyboard controls so you can direct window for output as you press the right, left, and up arrows, or the A, D, The first two lines are for context (you already have them in your code, if you've been following along), so add only the last three: player_list.draw(world) Drawing lines in pygame . The last shape to cover is a line (a line technically isn’t a shape… but we still need to know how to draw one!). There are four line drawing functions we can use however I’m just going to cover the most basic one in this tutorial.

Vladimir's answer is great! For anyone visiting in the future, here's the function with control over every aspect of the arrow:

def arrow(screen, lcolor, tricolor, start, end, trirad):
    rotation = math.degrees(math.atan2(start[1]-end[1], end[0]-start[0]))+90
    pg.draw.polygon(screen, tricolor, ((end[0]+trirad*math.sin(math.radians(rotation)), end[1]+trirad*math.cos(math.radians(rotation))), (end[0]+trirad*math.sin(math.radians(rotation-120)), end[1]+trirad*math.cos(math.radians(rotation-120))), (end[0]+trirad*math.sin(math.radians(rotation+120)), end[1]+trirad*math.cos(math.radians(rotation+120)))))'

Produce line from components, length and angle, The line does not need to have arrow heads. Edit: Solutions involving QGIS, GRASS GIS, GDAL, ArcGIS or Python/ ArcPy would be greatly� import pygame from math import pi pygame.init() screen = pygame.display.set_mode((100, 100)) WHITE = pygame.Color(255, 255, 255) RED = pygame.Color(255, 0, 0) The black color is the Surface default color and represents the part of the Surface that hasn't been drawn onto.

Quiver and Stream Plots, A quiver plot is a type of 2D plot that shows vector lines as arrows. The quiver arrow's direction is pointing up and to the right x_direct = 1 , y_direct = 1 . The following code section builds a quiver plot that contains one arrow. The keyword argument density=spacing specifies how close the streamlines are drawn together. Thanks for watching! feedback is appreciated FOLLOW ME ON TWITTER- https://twitter.com/dropdasoaps ----CODE---- import pygame, sys from pygame.locals import

Pygame Drawing Basics, You must do the following to get started with Pygame: import pygame color; e.g. screen.fill(white). pygame.draw.lines(screen, color, closed, pointlist, thickness). surf (pygame.Surface pygame object for representing images) -- Surface we are looking at. search_color (pygame.Color pygame object for color representations) -- Color we are searching for. threshold (pygame.Color pygame object for color representations) -- Within this distance from search_color (or search_surf). You can use a threshold of (r,g

Ggplot arrows, In this R ggplot dotplot example, we change the dot stack direction in a dot plot using the 5. plotnine is a data visualisation package for Python based on the grammar of A geom that draws line ranges, defined by an upper and lower value. The following program produces a plot with eight arrows with different styles. import pygame,sys from pygame.locals import * pygame.init() #set up the window screen = pygame.display.set_mode((500,400), 0, 32) pygame.display.set_caption('drawing') drawing the white background: In this function you define the color of the background. screen.fill(white) drawing the green polygon: