Search code examples
pythonpygamepositionlineangle

Get a point's position from an angle and the length of the line


I'm programming a game in Python, with pygame, and I'd like to make a function that draws a line in a specific direction from a point, with a specific length, for example, the definition of the funcion would be: def draw_line(position1: (int, int), angle: int, line_length: int, line_width: float, color: Color):

What can I do for calculating the second point to draw the line?

I have a little schematic of the problem, I want to get position2, to draw the line with pygame.

enter image description here


Solution

  • This is a math problem, but ok, the x and y coordinate of point 2 are:

    (x2,y2) = (x1 + line_length*cos(angle),y1 + line_length*sin(angle))