Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Polygon in SymPy from a list of vertices

Tags:

python

sympy

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?

like image 542
user2274850 Avatar asked Dec 07 '25 06:12

user2274850


1 Answers

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)).

like image 62
Animatinator Avatar answered Dec 08 '25 19:12

Animatinator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!