CompGeoJS Regular N-Gon

Regular n-gons with greater than three sides (we already have a Triangle class in CompGeoJS) can be created using a bounding rectangle and a parameter indicating the number of sides.  The smaller rectangle dimension is used to compute the radius of a bounding circle for the n-gon.  That and the rectangle centroid are used to compute vertices of the n-gon.

This makes is very easy to create diamonds, pentagons, hexagons, etc. and render them into a Canvas for educational or even architectural applications.  The RegularNGon demo illustrates how to use CompGeoJS and EaselJS to render regular n-gons from four to twelve sides.  It’s a pretty ugly demo as demos go, but it’s the math that is important in this project :)

The  source code is written for easy readability and is not minimized or otherwise obfuscated since this is an open-source project.


Comments are closed.