Drawing Polygons
Lesson Overview
In this a lesson about how to use the poly()
function to do drawing on with MicroPython. The poly()
function is implemented as part of the Framebuf function in MicroPython. This means that it will run on any display that uses frame buffers. We think you will find poly()
a very powerful and flexibile way to draw features on a face such as a nose or eyebrows.
Drawing with the Polygon Function
Since v1.19.1-724 MicroPython includes a flexible way to draw any polygon of any number of points either in outline or filled mode. With this function, you can quickly draw complex shapes such as triangles, hexagons, stars, rockets or houses.
To use the poly()
function we must pass it an array of points. The syntax for short array initialization is
as follows:
1 |
|
The letter "B" signals that each element will be an unsigned byte which is enough for our purposes. Many systems use an "h" which is a signed two-byte value which can be any integer in a range from -32,768 to 32,767. Since our OLED screen is only a maximum of 128 the B is the most compact value, but remember that the max value is 255 and we can't store negative values in an unsigned byte.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
This will generate the following image:
Sample Polygons
Here is an example program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
Challenge
- Create a drawing with a rocket flying over a house
- Add your own shapes
- When would you use rect() and ellipse() instead of poly?
- Modify the triangle drawing to move the points with an animation. Use an array of directions that reverse when the points reach the edge of the screen. solution