Programming Course for Beginners - ZedLX

The Easiest Online Computer Programming Course, for Free

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

11. Moving the Regular Polygon by Keyboard

The polygon can now be moved by the arrow keys.
[Run the program]

The vector velocity represents the polygon's speed and direction. The velocity is modified by pressing the arrow keys.

On each display frame, the position of the polygon is updated according to the rule of motion vectors. The rule is implemented in the penultimate statement of the while loop:
posNew = posOld + v×t;
In the equation above, posNew is a new position, posOld is an old position, v is a vector of velocity, and t is the elapsed time.

With this page, we complete our tutorial on drawing the regular polygons. Click on the 'Articles' button at the top to select another article. We also suggest taking a peak into our main 'Gallery' of programs, and taking a look into 'Overview' of our main programming tutorial for beginners. Have fun!

Loading...