Chapter 39. Drawing on a Canvas
Last year, I mowed my lawn on a warm and wonderful late summer day. As I followed my usual mowing pattern. I wondered if there was a better way to do it. In this article, we’ll use Perl/Tk to visualize some different mowing patterns and see how well they represent reality—and along the way demonstrate drawing on a Perl/Tk canvas.
Our Mower Is Programmable!
For this exercise we have at our disposal a programmable robotic mower. Our job is to write a software simulation of lawnmowing. It’s exceedingly difficult to model the complexities of the physical world with a computer, so we’ll make some simplifying assumptions. First, we’ll define the lawn as a rectangular area without trees, gardens, rocks, ponds, or cats.[11] The lawn is also a perfect mowing surface without bumps or undulations, and the grass has uniform thickness; this way we know that the mower can be steered accurately. Finally, we’ll assume the mower has a turning radius of zero: that is, it can pivot. (Automatic mowers that are always going forward have a nonzero turning radius.)
A Canvas Widget Is the Lawn
To represent the mowing area, we’ll use a Perl/Tk canvas widget, colored chlorophyll green of course. Let’s assume that to program the mower all we need is to write Perl/Tk code that overlays various items that display the mower’s path (lines, arcs, ovals, and such) on the canvas, making sure that no green remains.
Our first program starts by mowing (drawing a line) 100 feet ...
Get Web, Graphics & Perl/Tk Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.