Drawing letters with Gnuplot (for CISC181, Spring 2004)

Background

In this exercise, you will use a program called gnuplot to draw letters of the alphabet. This exercise illustrates some of the basic ideas behind how "fonts" work in systems such as Microsoft Windows, Macintosh OS, and the X Windows system used on strauss.

This exercise will teach you what you need to know about gnuplot. You should already be familiar with how to read input from a file in C++ and write output to a file in C++. If you need to review these concepts, Section 14.1 through 14.6 in Deitel/Deitel covers Sequential-access file processing, that is, ifstream and ofstream.

In this exercise, you will first use a text editor (vi or emacs) to create a file that contains points that draw a letter of the alphabet. You will then use gnuplot to plot those points, and store the resulting graphic in an output file. The output file has the extension .png, which stands for "Portable Network Graphics". Files ending in .png are similar to files ending in .gif, .jpg, or .jpeg, except they use a different format for representing the pixels in the image, and for compressing those pixels.

This exercise also assumes you know how to put files on the web under your public_html directory, use the chmod command to make those files readable, and point your web browser at those files.

The suggested link for putting the files you create with this exercise is:

   http://copland.udel.edu/~userid/cisc181/files/gnuplot
where userid is your strauss userid. Hence, the directory name is:
   ~/public_html/cisc181/files/gnuplot

Part 0: Copying files

Throughout this lab, you'l be using files from the gnuplot subdirectory of the labs directory. You might want to just copy all the files from that subdirectory into your current directory now. Here's a command to do it:

cp -r ~pconrad/public_html/cisc181h/04S/labs/gnuplot .

This will copy the entire gnuplot subdirectory, along with all its contents, into the current directory. You'll then have to cd into gnuplot to see all the files. The -r stands for recursive.

 

Part 1: Creating a gnuplot data file: "A.dat"

A typical gnuplot data file consists of lines of text, where on each line there are two numbers, representing an (x,y) coordinate. Here is a gnuplot data file called "A.dat", followed by an explanation of its contents:

# A

0 0
2 8
4 0

1 4
3 4
Explanation:

Part 2: A file containing gnuplot commands: "A.gnuplot"

To plot this data file, you use a file that contains gnuplot commands. Here is an example file "A.gnuplot" that takes "A.dat" as input, and produces an output file "A.png". The actual graphic produced appears after the listing of A.gnuplot:

set xrange [0:8]
set yrange [0:8]
set output "A.png"
set terminal png large color
plot "A.dat" with lines

A.png graphic Here is an explanation of the gnuplot commands:

Note: if you want to learn more about gnuplot commands, you can read about these by finding the gnuplot documentation on the web, or looking at the online help inside of the gnuplot program itself. The commands listed above are probably sufficient for this exercise, however.

Part 3: Actually plotting the graphic: creating the "A.png" file

To actually create the "A.png" file using "A.dat" and "A.gnuplot" (see above), type the Unix command:
gnuplot A.gnuplot
Then, to see the file, copy it into a directory on your webpage (i.e. somewhere under your ~/public_html directory tree) and do the "chmod" command that makes it readable. You should then be able to point your web browser to the file and see the output.

Part 4: Doing this from C++

Writing a .dat file and a .gnuplot file by hand can be tedious. If we are going to plot a large graphic, we might want to write a program to do it instead.

In the directory for this exercise you will find a C++ program called plotCIS.cpp. Copy this program into your directory, and open it with a text editor. Compile it and run it. You will see that it produces two output files: CIS.dat and CIS.gnuplot. Once the program runs, you can then run these files through gnuplot with the command:

gnuplot CIS.gnuplot
You should get an output file CIS.png. Copy this file to the web, and look at it. Notice that the output of the graphic "CIS" has a small problem. Can you fix this problem? If you are not understanding the code, here's wow to make sense of it.

Part 5: Another challenge

Now, see if you can extend the program so that it produces a graphic of CIS181. Add a function to write the number 1, and a function to write the number 8. You can call the function to write the number 1 twice. You'll have to change the xrange and yrange when you do this. Note that we could use variables to print out the xrange and yrange if we wanted to, rather than hard coding these values.

A few tips


Phillip T Conrad
Last modified: Thu Apr 22 17:26:24 EDT 2004