I just remembered my days at the university, for no particular reason. There was an assignment, something related to recursive function. The lecturer wanted us to create a simple graphic software that able to fill basic shape like square, circle and triangle. We have to create all the draw function ourself.
As you may know, while the shape function is simple, the fill function isn’t. The basic idea is that to call a function that call a putpixel(), and check the neighbouring pixel for color data, and call itself to plot that pixel location, until the shape is filled.
While the idea is nice, to call a function recursively for each pixel takes too much memory, and it causes stack overflow or related error (it’s been a long time for me to remember). You’re fine if you’re filling small shape, but medium or bigger shapes will fail.
I decided to do better, to make sure it work no matter what is the size of the shape. Realizing that all of the shape is concave, I decided to use loops. I know it a hack, since it will not work if the shape is convex, but at least I still follow the rule, to create a fill function that fill that particular 3 simple shape. This loop allow me to recursive for each line instead of pixel, and thus greatly minimize memory consumption. Now my fill function works perfectly, as far as the assignment goes.
On the presentation day, there was this girl that fill the drawing screen of her program with tiny grid. So if you draw a large shape, you have to fill the shape by filling this tiny grid, one by one. The lecturer looked at her code, and give her a full mark since her recursive function, is, indeed, recursive. Some other student only draw small shapes, and when trying to fill a larger shapes, it fails. When asked by the professor, the students mentioned :
“I have no idea why it fails, but my function is indeed recursive as you want it, which is why it worked on small shapes.†and the lecturer replied.
“It’s OK, a lot of my past students have this problems anyway.†and they too received a full mark.
I lost my confidence on university education on that particular day. Oh, by the way, stop asking why I recieved a Diploma in 3D Animation from Cosmopoint despite studying programming for 3 years at UTM Skudai. The answer is the brain is willing, but the memory is weak. And guess what our education system emphasize on?