Drawing arrowheads which follow the direction of the line in PyGame
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 - end dY = -(start - end) print m.degrees(m.atan(dX/dY)) + 360 pygame.draw.line(screen,colour,start,end,2)
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): pygame.draw.line(screen,colour,start,end,2) rotation = math.degrees(math.atan2(start-end, end-start))+90 pygame.draw.polygon(screen, (255, 0, 0), ((end+20*math.sin(math.radians(rotation)), end+20*math.cos(math.radians(rotation))), (end+20*math.sin(math.radians(rotation-120)), end+20*math.cos(math.radians(rotation-120))), (end+20*math.sin(math.radians(rotation+120)), end+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): pg.draw.line(screen,lcolor,start,end,2) rotation = math.degrees(math.atan2(start-end, end-start))+90 pg.draw.polygon(screen, tricolor, ((end+trirad*math.sin(math.radians(rotation)), end+trirad*math.cos(math.radians(rotation))), (end+trirad*math.sin(math.radians(rotation-120)), end+trirad*math.cos(math.radians(rotation-120))), (end+trirad*math.sin(math.radians(rotation+120)), end+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: