Programming Course for Beginners - ZedLX

The Easiest Online Computer Programming Course, for Free

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

6. Drawing the Regular Pentagon

On this page, the regular pentagon is generated by iteration. The first statement of the source code sets the number of sides n to equal 5. In the first statement, the type int indicates that the number of sides must be a whole number.
[Run the program by clicking the 'Run' button]

The fourth statemet of the program computes the rotation angle, which must equal the full circle of 360 degrees divided by the number of sides.

A very important change is that the for loop now has exactly n iterations. For the regular pentagon, the number of iterations is 5.
[Run the program]

In each iteration, two points named pA and pB are computed. The point pA is immediately visualized as the red vertice.

On the previous page, the angle of 120 degrees was employed to draw the regular triangle. Now, the computed value named angle is the argument of both rotations that previously used the angle of 120 degrees.

The point pB is not immediately visualized. Instead, this point is employed as the endpoint of a yellow line that visualizes one side of the regular triangle.

The final statemt in the body of the for loop is rotates the vector v counter-clockwise, by the computed angle in degrees.

On the next page, the drawings of n-sided regular polygons are further improved.

Loading...