Math 280 Project Report

Line-fields in Tcl/Tk

(May 7, 1999)
Jonathan A. Booth, Student, University of Illinois at Urbana/Champaign

Abstract

Graphing linefields is a useful learning tool for mathematics students, but it is an arduous process to do by hand. To solve this problem, a linefield graphing applet was constructed in Tcl/Tk, capable of being run in a web browser (Netscape 4/IE4) with appropriate plugin or stand alone via the 'wish' interpreter. It graphs linefields, displaying the results for the user's inspection. Approximations of solutions using Euler's method are implemented, allowing students to explore the solutions of the linefield as well.

Method

The tclet (a Tcl/Tk applet) was conceived and designed using a top-down design methodology. It's general structure follows:

SlopedLine calculations

The core of the SlopedLine calculation is a double-nested for loop, running from the minimum value of x to the maximum, and for each of those values, running from the minimum value of y to the maximum. Inside both those loops is the computation of the slope at the current point, which is then converted to an angle to allow for constant length slope lines. Using that angle, an appropriately sloped line is drawn on the graph, and then moved from it's position at (0,0) when drawn to the current (x,y) position given by the double-nested for loops.

The pseudo-code form of this loop (written in a C like pseudo-code) is:

for (x = x_min;x <= x_max;x = x+dx) {
  for (y = y_min;y <= y_max;y = y+dy) {
    slope = evaluate_equation_at(equation,x,y);
    theta = inverse_tan(slope);

    draw_line_from_to(-len/2*cos(theta),-len/2*sin(theta),
                      len/2*cos(theta),len/2*sin(theta))
    move_line_from_to(0,0,x,y);
  }
}

Click calculations

To approximate a solution, once a point is clicked that the approximation should go through, the tclet uses Euler's method to compute a left and right half (with the point as the center). The choice of this method was somewhat unfortunate, rendering the tclet unable to accurately handle linefields who's slope becomes infinity/undefined (because of the division by dX, the slope can become infinity/undefined if dX should be equal to 0).

Using Euler's, the tclet takes the point that is currently being looked at, and finds the slope at that point. Then, it computes a new point to be at, moving x by a little bit, and moving y by the slope times the distance x moved. It draws a line between the old and new positions, then writes the new position as the old ones, and repeats, until one of the positions moves out of bounds of the graph.

The pseudo-code form of this procedure (written in a C like pseudo-code) is:

  x_right = x_left = point_clicked_x;
  y_right = y_left = point_clicked_y;
  // dx = <distance for each step in line graphing, 1 in this case>

  // Graph the left side of the line
  while ( in_graph_bounds(x_left,y_left) ) {
    slope = evaluate_equation_at(equation,x_left,y_left);

    new_x = x_left - dx;
    new_y = y_left - slope*dx;

    draw_line_from_to(x_left,y_left,new_x,new_y);
    x_left = new_x;
    y_left = new_y;
  }

  // Graph the right side of the line
  while ( in_graph_bounds(x_right,y_right) ) {
    slope = evaluate_equation_at(equation,x_right,y_right);

    new_x = x_right + dx;
    new_y = y_right + slope*dx;

    draw_line_from_to(x_right,y_right,new_x,new_y);
    x_right = new_x;
    y_right = new_y;
  }

Results

The result is a useful linefield graphing tclet, which can either be embedded in a web page as it is below, or as a stand-alone application which can be run from any system which has the 'wish' interpreter available (Windows, MacOS, and UNIX with X) for it.

The field can be scrolled around using the scroll bars to the sides of it. The dark grey equation box is editable, to allow you to enter a new equation in slope form. The X,Y display is the current location of the mouse pointer inside the graph.

Note that sometimes when graphing a field, the field will be too steep to be able to see anything interesting in it (such as: (dx/dy)=$y). In this case, multiply the field by a scalar, such as 0.01 and it will then be visible.

The source code is available here (approx 10kb), however downloading it will most likely just start the tclet fullscreen in your browser; instead use your browser's "Save as ..." option to save the code to disk. You may also download all the project files (the code and html files) in a large zip (approx 10kb) or tar-gzip file (approx 8kb).

Future Improvements

Some improvements could be made to the tclet, aside from general code cleanup. The following list contains improvements that would be useful:

References

  1. Welch, Brent. Practical Programming in Tcl/Tk.
    New Jersey. Prentice Hall. 1997.
  2. Tcl plugin and documentation [online]. Scriptics Corp.
    Available from WWW: <http://www.scriptics.com/>.
  3. Skeel, R. Keiper, J. Elementary Numerical Computing
    with Mathematica
    . New York. McGraw-Hill, Inc. 1993.
  4. Koopman, Phil. How To Write An Abstract [online].
    Available from WWW: <http://www.cs.cmu.edu/afs/cs.cmu.edu/local/mosaic/common/omega/Web/People/Koopman/essays/abstract.html>.

 Home Math280 Sitemap