Lesson 4: Basic Math in Tcl
Suppose you would like to do some simple math in your Tcl console. Let's
take a look at doing something simple, like adding 2 + 2. Basic first
grade arithmetic, right? How do we tell the computer to add 2 + 2 for
us? We will need to use the Tcl command,
expr, which is the command
for math expressions (expr). The Tcl command
expr has rules we must
follow or else the computer will get mad at us. Let's take a look at
our simple first grade arithmetic on the Tcl Wish console. Here is what
you will need to type:
expr 2 + 2
expr 2 + 2
The Tcl command is
expr, the arguments are our math expression, 2 + 2,
space separated. Using our
magic_number example, let's add 2 to our
magic number? That is, we want to do this:
2 + $magic_number = ?
And we would type the following into the Tcl console to get it done:
set new_magic_number [expr 2 + $magic_number]
Whoa! Where did those brackets [ ] come from? If you look hard we have
two Tcl commands on one line. The Tcl interpreter can only handle one
Tcl command at a time. The Tcl interpreter is going to perform the command
inside the brackets first. In our case it is the Tcl command
math expression will be calculated. Then guess what? The answer to the
simple arithmetic is going to be handed off to the Tcl command
a data argument. That's a mouth full isn't it? Let's break this down.
We type this command into the Tcl console interpreter:
set new_magic_number [expr 2 + $magic_number]
Tcl will grab what is inside the brackets first and do the following:
expr 2 + $magic_number
Obviously, the answer to the great mystery is 4. The answer is handed
back to the
set command by the interpreter.
set new_magic_number 4
The Tcl interpreter does this all invisibly so we don't see it like I showed you. But that's what the computer is doing. Let's see if the computer has the right answer (see Figure 11).
set magic_number 2 set new_magic_number [expr 2 + $magic_number]
Before we continue, let's quickly review. Look at how we set the
and how we set the
new_magic_number. Why did we need to use brackets
to set the value of the
new_magic_number? The values inside the brackets
 needed to be evaluated by the computer. In this case, the computer
had to first compute the value of the Tcl
expr command. Once it did
this, the value of 4 was set to the variable
new_magic_number. We will
use brackets in a Tcl program when we want the computer to compute a
value first and then set a variable.
Let's revisit our Tic-Tac-Toe grid. The upper left corner block is located at x=0 and y=0, as shown in Figure 4. Let's set these variables using the Tcl command set.
set x 0 set y 0
Now let's tell the computer to calculate a new block coordinate for the
bottom right block of the Tic-Tac-Toe grid. We know from our little
map that this block is located at x=2 and y=2. We can have the computer
calculate this new block location. To get from the upper lefthand corner
of the grid to the lower right corner of the grid we must walk across the
x direction 2 steps and down the y direction 2 steps. Using Tcl commands
expr, we would type in the following at the Tcl interpreter
set new_x [expr 2 + $x] set new_y [expr 2 + $y]
set x 0 set y 0 set new_x [expr 2 + $x] set new_y [expr 2 + $y]
new_y hold the grid position of the lower righthand corner block. If you look at Figure 4 you will see the lower righthand block in the Tic-Tac-Toe grid has the values, x=2 and y=2.
The value of
new_x is 2 and the value of
new_y is 2, the location of the lower righthand block in the Tic-Tac-Toe grid. If you are making
your move in Tic-Tac-Toe to place your X at the lower righthand corner
you would need to tell the computer to place an X at location x=2 and
y=2. I see the light bulb going on above your head! Time to move on.
Lesson 5: Exploring Computer Graphics Window Grids
Most computers today use a windowing system to display graphics on the computer. Our Tic-Tac-Toe game grid is a simple window that is 3 blocks wide by 3 blocks high. We learned earlier that the grid has a resolution of 3 x 3 (say 3 by 3).
A typical computer window system has a display resolution of at least 640 x 480, to resolutions up and around 1280 x 1024. Obviously, a 3 x 3 window would be too small to see if created on your computer window display. So let's pick a common window size. Using a game like Doom or Quake by id Software, we could select a window size of 640 x 480. The video game window is 640 wide by 480 high.
Since we are talking about computer windows and not a Tic-Tac-Toe grid, we would use the term pixels instead of blocks. A pixel is a picture element. Pixels are the magic little workers that make pictures appear on your computer display. We will create a display window 640 pixels wide by 480 pixels tall.
Tcl has a special software toolkit, called Tk, for graphics programming. When you run the Wish Shell app you are basically running Tk. We can set the little Wish window to the display size of a video game window. This is what you would type into the console window:
set video_game_width 640 set video_game_height 480
We instructed the computer of the values we want to store for creating our new game-size window. We want a window that is 640 pixels wide and is 480 pixels tall. Next we would need to create a graphics window, called a canvas, using the Tk command.
canvas .video_game_window -width $video_game_width -height $ video_game_height
The output from your console should look similar to the console output in Figure 13.
Nothing spectacular at the moment, right? Right! We set the new window
dimensions, but we just haven't instructed Tk to display the new window.
Tk has a special command called
pack that performs a lot of magic in
the background to place and display our new window. Type in this command
at the console:
Now let's see what happens to our Wish window, as shown in Figure 14.
set video_game_width 640 set video_game_height 480 canvas .video_game_window \ -width $video_game_width -height $video_game_height pack .video_game_window
Our little Wish window magically grew didn't it? This is the action of the Tk pack command. When we enter the pack command it sends our instructions to the Wish Shell window.
Please note, if you ever see the following error message that you will need to destroy your previously created
window name "video_game_window" already exists in parent
It may be lingering around from the previous script example. Use the following and then try the script again:
If that doesn't work you should try quitting your Wish Shell and restarting it. Now back to our discussion already in session.