At My Fingertips
Rapid Playground
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, and the (somewhat complex) 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, and we want to light them from the top or the bottom:
The height of the eraser graphic corresponds to the pencil diameter.
The height of the lit part of the eraser is 5/6 of the overall height.
The reflex is horizontally centered within the eraser,
and is offset from the vertically by the erasor's thickness.
The remaining measures are computed in the eraser
function.
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.
To create the body, we need quite some information:
We could define the colors inside the body function, like we did for the eraser and the ferrule, but we don't do that. We will need the same colors when creating the scalloping (the three semicircles) of the point. Thus, we define the colors somewhere else, and we pass them to our body function as an argument.
The point consists of the wooden collar, the graphite tip, and the scalloping (the three semicircles).
The wooden collar is bright but has a darker part at the bottom.
This is the most challenging part of the pencil. Can you figure out a way to compose it?
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 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. Light them from the top or the bottom (rotate them, and make sure the rotated one is shaded from the top). 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.
Shaded Pencil
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version c08406b (Wed, 20 Nov 2024 12:30:00 GMT)