Skip to content
Advertisement

The draw polygon function in pygame doesn’t draw the correct amount of vertices

I am trying to create a function that draws a random triangle on the screen. I need to make sure that the triangle is at least somewhat on the screen (although it doesn’t have to be fully on the screen).

First I choose a random point with the range of the random numbers being the bounds of the screen, and then I choose two random points with a range that’s 250 pixels larger in every direction.

This way I am making sure that at least one point is on the screen, which means that the triangle would be somewhat visible no matter what.

Here is my code:

def add_triangle():
    x1 = random.randint(0, wn_width)
    y1 = random.randint(0, wn_height)

    x2 = random.randint(-250, wn_width + 250)
    y2 = random.randint(-250, wn_height + 250)

    x3 = random.randint(-250, wn_width + 250)
    y3 = random.randint(-250, wn_height + 250)

    width = max(x1, x2, x3) - min(x1, x2, x3)
    height = max(y1, y2, y3) - min(y1, y2, y3)

    surface = pygame.Surface((width, height), pygame.SRCALPHA)
    surface.fill((0, 0, 0, 0))
    pygame.draw.polygon(surface, get_random_color(), [(x1, y1), (x2, y2), (x3, y3)])
    shapes.append((surface, (min(x1, x2, x3), min(y1, y2, y3))))


# temporary code
add_triangle()
wn.blit(shapes[0][0], shapes[0][1])

Here is the issue though. For some reason, most of the time it either doesn’t even draw a triangle, like this for example: not a triangle

or the polygon isn’t on the screen at all (not sure if it’s out of frame or just isn’t being drawn): empty

What am I doing wrong?

By the way if you want to see that everything else is written correctly here it the rest of my code:

pygame.init()
clock = pygame.time.Clock()
wn_width = 1920
wn_height = 1080
wn = pygame.display.set_mode((wn_width, wn_height))
shapes = list()

def get_random_color():
return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    pygame.display.update()
    clock.tick(20)

Advertisement

Answer

They don’t take into account the offset to the top left position of the bounding box of your random polygon. You cannot draw the polygon in window coordinates. You draw the polygon on an surface that is exactly the size of the polygon. The polygon needs to be drawn in the coordinates system of surface. Between the top left of the screen and the top left of the surface is a translation:

(0, 0) screen
   +----------------------+
   |                      |
   |   (0, 0) surface     |
   |       +---+- -+      |
   |       |  /   |      |
   |       | /    |      |
   |       +-------+      |
   |                      |
   +----------------------+  

So you need to move the polygon to the top left of the surface. You need to subtract the minimum coordinates from all the coordinates of the polygon:

def add_triangle():
    x1 = random.randint(0, wn_width)
    y1 = random.randint(0, wn_height)

    x2 = random.randint(-250, wn_width + 250)
    y2 = random.randint(-250, wn_height + 250)

    x3 = random.randint(-250, wn_width + 250)
    y3 = random.randint(-250, wn_height + 250)

    min_x, min_y = min(x1, x2, x3), min(y1, y2, y3)
    width = max(x1, x2, x3) - min_x
    height = max(y1, y2, y3) - min_y 

    surface = pygame.Surface((width, height), pygame.SRCALPHA)
    surface.fill((0, 0, 0, 0))
    points = [(x1-min_x, y1-min_y), (x2-min_x, y2-min_y), (x3-min_x, y3-min_y)]
    pygame.draw.polygon(surface, get_random_color(), points)
    shapes.append((surface, (min_x, min_y)))
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement