Processing Month, Day 13 - Regular Polygons
Today we’re going to write a flexible function to draw regular polygons. As Processing only offers basic shapes like ellipses, rectangles and triangles, it might be handy to have a collection of functions to draw other shapes. The function for a regular polygon looks a lot like the algorithm we wrote on day 4, Stars.
The code below is the full function to draw a regular polygon. It takes a float for the radius, and an integer for the number of points.
void regularPolygon( float radius, int numPoints )
{
float angle = TWO_PI / numPoints;
beginShape();
for (int i = 0; i < numPoints; i++) {
float x = cos( i * angle ) * radius;
float y = sin( i * angle ) * radius;
vertex( x, y );
}
endShape(CLOSE);
}
If you want to draw the polygon to the screen, you’ll need to use the translate() function first. The function draws the regular polygon from its center. I’ve used a for-loop to draw a lot of polygons, so you can see how powerful a small function like this one can be.
translate( width/2, height/2 );
for (int i = 0; i < 18; i++) {
regularPolygon( 10 + i * 10, 3 + i );
}
Examples
The sketch is also available on OpenProcessing.

Download
Download the example for Procesing Day 13, Regular Polygons.
Related Articles
- Processing Month (Blog)
- Processing Month, Day 4 - Stars (Blog)
Browse Articles
Comments (0)
Popular Articles
- Introduction to openFrameworks
- Creating 3D Shapes with Hemesh
- Mirroring Video with openFrameworks
- An Introduction to colorLib
- How to create a FullScreen iPhone Application
Popular Tags
- processing (95)
- software (50)
- art (48)
- web design (40)
- photography (39)