Search code examples
pythonalgorithmpygamepython-3.4

Drawing pentagon,hexagon in Pygame


I'm trying to make functions that can draw pentagon hexagon etc. Troubling on algorithm, should I compare the lines with pygame.draw.line ? It looks like too much work around the determine coords of lines. Is there an easy way to draw them? Also I don't know another library that can draw these things. I know only Pygame, Is there another one ?


Solution

  • You can draw it using lines. You only need to generate list of vertices with simple trigonometry.

    Something like this (if I didn't make a mistake):

    def draw_ngon(Surface, color, n, radius, position):
        pi2 = 2 * 3.14
    
        for i in range(0, n):
            pygame.draw.line(Surface, color, position, (cos(i / n * pi2) * radius + position[0], sin(i / n * pi2) * radius + position[1]))
    
        return pygame.draw.lines(Surface,
              color,
              True,
              [(cos(i / n * pi2) * radius + position[0], sin(i / n * pi2) * radius + position[1]) for i in range(0, n)])