Programming Course for Beginners - ZedLX

The Easiest Online Computer Programming Course, for Free

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

3. Calculating The Left Vertice

The next step of generating the regular triangle is to draw the left vertice. The left vertice has to be placed in a precise position, so the position of the vertice needs to be computed first.
[Run the program by clicking the 'Run' button]

The simplest way to corectly compute the position of the left vertice is to perform a vector rotation. Compared to the previous page, the green vector has been rotated by 120 degrees counter-clockwise.
[Run the program]

Rotations of Vectors

The vector named v2 is introduced in the source code as equal to the vector v rotated by 120 degrees counter-clockwise. The rotation in degrees is performed by the member function rotdg. In 2D computer graphics, since the y coordinate is always reversed upside-down, so is the direction of rotations.

In the next statement, the vector v2 is visualized by a green arrow. Then a slight delay is made so that the green arrow can be easily observed.
[Run the program]

The Azure Vertice

The next statement creates the left vertice named p2 , and puts it at the position computed by adding the green vector v2 to the center point. The point p2 is then visualized in the azure color.

On the next page, the source code gets greatly simplified.

Loading...