Skip to content

shapes.Polygon crashes uninformingly when given too many vertices #1282

@ionymikler

Description

@ionymikler

Describe the bug
Tried to make a polygon with too many vertices, in a similar fashion to this:

import math

import pyglet
from pyglet import shapes

# Create a window
window = pyglet.window.Window(800, 600, caption="Pyglet Polygon Example")
batch = pyglet.graphics.Batch()

# Create a simple polygon (a hexagon)
x_center, y_center = 400, 300  # Center of the window
radius = 100
sides = 80

# Method 1: Create a flat list of coordinates for the polygon
vertices_flat = []
vertices_tupled = []
for i in range(sides):
    angle = i * (2 * math.pi / sides)
    x = x_center + int(radius * math.cos(angle))
    y = y_center + int(radius * math.sin(angle))
    vertices_flat.extend([x, y])  # Add as a flat list of coordinates
    vertices_tupled.append((x,y))

# Create the polygon - pass vertices as a flat list (no unpacking)
# breakpoint()
polygon = shapes.Polygon(
    *vertices_tupled,
    color=(0, 255, 0),  # Green color
    batch=batch,
)

# Create a label to display information
info_label = pyglet.text.Label(
    "Simple Polygon Example",
    x=10,
    y=window.height - 10,
    anchor_y="top",
    batch=batch,
)


@window.event
def on_draw() -> None:
    window.clear()
    batch.draw()


# Run the application
if __name__ == "__main__":
    print("Running Pyglet Polygon Example...")
    print(f"Created a hexagon with {sides} sides")
    print(f"Vertices (flat list): {vertices_flat}")
    pyglet.app.run()

got the following error:

Traceback (most recent call last):
  File "/home/ros/ws/tools/safety_zones/polygon.py", line 27, in <module>
    polygon = shapes.Polygon(
              ^^^^^^^^^^^^^^^
  File "/home/ros/ws/eco_ws/.venv/lib/python3.12/site-packages/pyglet/shapes.py", line 2622, in __init__
    super().__init__(
  File "/home/ros/ws/eco_ws/.venv/lib/python3.12/site-packages/pyglet/shapes.py", line 353, in __init__
    self._create_vertex_list()
  File "/home/ros/ws/eco_ws/.venv/lib/python3.12/site-packages/pyglet/shapes.py", line 2636, in _create_vertex_list
    earcut.earcut(vertices),
    ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ros/ws/eco_ws/.venv/lib/python3.12/site-packages/pyglet/extlibs/earcut.py", line 69, in earcut
    earcutLinked(outerNode, triangles, dim, minX, minY, size)
  File "/home/ros/ws/eco_ws/.venv/lib/python3.12/site-packages/pyglet/extlibs/earcut.py", line 127, in earcutLinked
    indexCurve(ear, minX, minY, size)
  File "/home/ros/ws/eco_ws/.venv/lib/python3.12/site-packages/pyglet/extlibs/earcut.py", line 390, in indexCurve
    p.z = zOrder(p.x, p.y, minX, minY, size)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ros/ws/eco_ws/.venv/lib/python3.12/site-packages/pyglet/extlibs/earcut.py", line 479, in zOrder
    x = (x | (x << 8)) & 0x00FF00FF
              ~~^^~~
TypeError: unsupported operand type(s) for <<: 'float' and 'int'

System Information:
Uploaded in pyglet.info

How To Reproduce
With code provided up

pyglet_info.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions