Programming Course for Beginners - ZedLX

The Easiest Online Computer Programming Course, for Free

How to Draw N-sided Regular Polygons [5/11]

5. Iterating the Rotations

On this page, a three-pronged star is generated by iteration. Drawing by iteration is an important sub-step in the quest of drawing regular polygons with any number of sides.

The iteration is performed by the for loop. The number of iterations is 3.
[Run the program by clicking the 'Run' button]

The loop variable i contains the ordinal number of the current iteration. This variable is printed out on each iteration by the first println statement.
[Run the program]

In each iteration, a point named pA is computed. The point pA is then visualized as the red vertice.

A yellow line is drawn next. One endpoint of the line is the point pA and the other is the point center. The line is drawn by the function linewp, with a width of 5 pixels. Width of the line is the third argument of this function.

The final statemt in the body of the for loop is very important. It rotates the vector v by 120 degrees counter-clockwise. The new value of the rotated vector v is then employed in the next iteration, to draw the next vertice and the next side of the regular triangle.

On the next page, we draw a regular pentagon by iteration.

Loading...