Skip to content
Advertisement

How to make Polygon in SymPy from a list of vertices

I want to use SymPy to create a Polygon with n faces and calculate all parameters.

The easy form is

from sympy import Polygon
p1, p2, p3, p4, p5 = [(0, 0), (1, 0), (5, 1), (0, 1), (3, 0)]
Polygon(p1, p2, p3, p4, p5)

Polygon(Point(0, 0), Point(1, 0), Point(5, 1), Point(0, 1))

but I want to use n points from a list, for example

p = [(0, 0), (1, 0), (5, 1), (0, 1), (3, 0)]
Polygon(p)

But this form and similar is not validated.

Any suggestions?

Advertisement

Answer

You could do this by putting an asterisk in front of the list of parameters to expand it out, like so:

p=[(0, 0), (1, 0), (5, 1), (0, 1), (3, 0)]
Polygon(*p)

This will be equivalent to calling Polygon((0, 0), (1, 0), (5, 1), (0, 1), (3, 0)).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement