O'Reilly logo

Web, Graphics & Perl/Tk Programming by Jon Orwant

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 39. Drawing on a Canvas

Steve Lidie

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required