If you use Tcl/Tk for GUI programming, you should know about Tkpath, a replacement for the built in canvas widget that adds antialiasing, full alpha transparencies and more. The project is still in its infancy, but it’s already quite usable. If you’re trying to make good-looking charts or pictures with Tk, you owe it to yourself to check it out. Seriously, take a quick look at the demos. It’s OK, I’ll wait.
One area where Tkpath shows its immaturity is in the API, which is still pretty rough around the edges. For some picture elements, like arcs, you actually have to use SVG syntax to describe the path you want. That’s obviously possible, but I don’t think that anybody would call it a particularly easy to use interface. In fact, it took me the better part of a weekend to figure out how to make arcs with Tkpath (starting with absolutely no knowledge of SVG syntax). In hopes that it will save somebody else some pain, here’s what I learned.
The path item
Unlike the built-in Tk canvas, Tkpath does not have a dedicated arc item, at least not yet. Instead you use the generic path item, which is just an interface for specifying raw SVG paths. The Tcl syntax for creating a path is deceptively simple: pathName create path pathSpec ?options?. The magic is all in pathSpec, which is where you stuff an SVG path description.
SVG path syntax
SVG path syntax is itself a rudimentary graphics programming language, somewhat reminiscent of Logo’s “turtle graphics”. Here’s what you need to know:
- A path description is a space-separated list of drawing commands, such as moveto or line
- Each drawing command consists of a single letter to identify the type of command, and some number of arguments, determined by the type of command.
- Commands are processed left-to-right.
- Each command updates the current location of the drawing pen; this location is an implicit argument for the next command. For example, the line command draws a line from the current location to the location explicitly given as an argument.
- Commands are case-sensitive. For example, “M 100 100” is not the same as “m 100 100”. When the command name is upper-case, then coordinate arguments are treated as absolute; when the command name is lowercase, then the coordinates are interpreted relative to the current pen location.
- The moveto command must be first in any sequence of commands.
SVG drawing commands for arcs
We only need a couple of SVG drawing commands to place arcs anywhere we like: moveto, and of course arc:
||M x y
||The moveto moves the pen from the current location to the specified location without drawing any line or curve between the points.
||A rx ry rot large direction x y
||The arc command draws an elliptical arc from the current location to the specified location x,y. rx and ry give the radius of the ellipse along the x- and y-axes. rot gives the x-axis rotation. large indicates whether the arc is at least 180 degrees. If so, it has value one; otherwise it has value zero. direction indicates the direction in which to draw the arc: one for clockwise, zero for counter-clockwise.
A simple example
For example, here’s how to create a simple image. It consists of three paths, with two arcs each:
The mouth starts at 40,60, so we need to move the pen to that point with the moveto command:
M 40 60
The lower arc is the bottom half of a circle of radius 60, ending at 160,60. Note that we don’t start a new path yet, just tack another command onto the one we’ve started:
M 40 60 A 60 60 0 1 0 160 60
The upper arc is half of an ellipse with x-radius 60 and y-radius 40, ending where the first arc began, at 40,60. Again we can just append another command to our existing path:
M 40 60 A 60 60 0 1 0 160 60 A 60 40 0 1 1 40 60
The left eye
The left eye starts at 40,40, so again we start with a moveto command:
M 40 40
Now, we want to create the upper arc — the top half of a circle with radius 20, ending at 80,40:
M 40 40 A 20 20 0 1 1 80 40
The lower arc is the top half of an ellipse with x-radius 20 and y-radius 10, ending where the first arc began, at 40,40:
M 40 40 A 20 20 0 1 1 80 40 A 20 10 0 1 0 40 40
The right eye
The right eye has the same shape as the left, but starts at 120,40 and ends at 160,40:
M 120 40 A 20 20 0 1 1 160 40 A 20 10 0 1 0 120 40
Here’s how it looks in Tcl:
package require tkpath
wm title . "arcs demo"
pack [tkp::canvas .c -background white -width 200 -height 140]
.c create path "M 40 60 A 60 60 0 1 0 160 60 A 60 40 0 1 1 40 60"
.c create path "M 40 40 A 20 20 0 1 1 80 40 A 20 10 0 1 0 40 40"
.c create path "M 120 40 A 20 20 0 1 1 160 40 A 20 10 0 1 0 120 40"
set done 0
Once you know how to do it, making arcs with Tkpath (or SVG, for that matter) is not too hard, although I think there’s room for a dedicated arc item, even if that’s just a simple wrapper around SVG paths.
If you want more information about Tkpath, you can try the home page, or the user manual.