Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a Polygon with X sides (all equal length & angles)

Tags:

java

math

polygon

I am looking to calculate the X and Y points of each point on a polygon, given the number of sides, and the fact that all sides are equal. I would also have the width and height constraints of the entire shape. If it helps any, I would be doing this in Java (most likely using Line2D).

like image 948
Matt Dunbar Avatar asked Dec 30 '22 04:12

Matt Dunbar


1 Answers

You should first find out the center of the circle (cx, cy) and the radius R by the width and height constraints, which is trivial. Each of the polygon points is equally distributed on the circle and their position can be calculated by:

Xi = cx + R*cos(2.0*PI*i/n)
Yi = cy + R*sin(2.0*PI*i/n)
like image 100
Long Cheng Avatar answered Dec 31 '22 18:12

Long Cheng