9.2. Coordinate System

A canvas widget uses a coordinate system to locate items inside of it, but the coordinate system isn't a normal one. It's more like an upside-down coordinate system.

Figure 9.1 shows a diagram that demonstrates the coordinate system a canvas widget uses.

Figure 9.1. Canvas coordinate system

The x coordinates behave normally; the larger coordinates are to the right and the smaller ones are to the left. The y coordinates look like they have been drinking vodka; the larger y coordinates are on the bottom rather than on the top because the 0,0 point is in the upper-left corner. Although it is rare, you can use negative coordinates in a canvas.

The coordinate system isn't too hard to deal with once you realize what is happening, but if you try to draw a building with a standard coordinate system in mind (that is, with the larger y coordinates higher up), your building will come out upside down.

There are several ways to deal with this. First, adjust your way of thinking so you always think y coordinates are larger at the bottom (never mind all those years we all struggled through geometry classes). Or, you are just as stubborn as I am, you can think in normal coordinates, and have your program do a quick little calculation before sending y coordinates to the canvas functions. (Multiply all y coordinates by -1. Tricky, huh?)

Whichever way you decide to deal with ...

Get Learning Perl/Tk 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.