At My Fingertips
Let's write a function that produces this pencil graphic:
You might have thought, from left to right, of the eraser, the metal ring (called the "ferrule"), the yellow body, the zig-zag fringe, and the point.
Let's implement the different components of a pencil, creating a function for each. We want to be able to create pencils with arbitrary body lengths and diameters.
The height of the eraser graphic corresponds to the pencil diameter. The width of the eraser graphic corresponds to one half of the pencil diameter. The corner radius is a quarter of the pencil diameter.
The metal ring connecting the eraser to the body of the pencil is called the ferrule. The width of the ferrule is 1/3 of the pencil diameter.
While the name of this function may be unfamiliar, its implementation should feel almost too easy.
The body is simple, but to create it, we need quite some information:
We could define the color inside the body function, like we did for the eraser and the ferrule, but we don't do that. We will need the same color when creating the small yellow fringe triangles. Thus, we define the color somewhere else, and we pass it to our body function as an argument.
The probably most difficult part to construct is the fringe between the body and the point:
The height of the graphic corresponds to the pencil's diameter. Important: The width of the graphic corresponds to 1/4 the pencil's diameter.
One way to construct this graphic, without needing trigonometric functions, is to think of this as six yellow right triangles placed above each other, overlaid on a wood-colored rectangle.
Each triangle has a known height (1/6 of the pencil diameter) and a known width (1/4 of the pencil diameter). Use triangle and rotate to create the triangles (we need two kinds of triangles: one where the right angle is at the top, the other where the right angle is at the bottom).
The point consists of the wooden collar and the graphite tip.
We can use a wood-colored triangle and place a graphite-colored triangle on top, using pin and compose.
To compose a pencil from the above components, we simply need to place the components beside each other.
If you have a beside_list
function in your toolbox, you can use that.
Otherwise, you can simply call beside a number of times.
Do you like your simple_pencil
function?
You may want to store it in your toolbox,
so you can reuse it in other activities and in the playground!
Now that we have a nice function to create pencils, play with it! Create thick and thin, and long and short pencils. What are realistic proportions? What proportions look the best?
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Simple Pencil
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version c3a8286 (Mon, 14 Oct 2024 13:30:28 GMT)