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/gnuplotwhere userid is your strauss userid. Hence, the directory name is:
~/public_html/cisc181/files/gnuplot
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.
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 |
set xrange [0:8] set yrange [0:8] set output "A.png" set terminal png large color plot "A.dat" with lines |
Here is an explanation of the gnuplot commands:
set xrange [0:8] signifies that the x-axis will run from 0 to 8. The set yrange command is similar.
set output "A.png" means that the output graphics will go to a file called "A.png". You could supply any other file here that you wish, though it should end with .png if you want it to work properly with your browser.
set terminal png large color means that we want .png output. (Note: There are other kinds of output that gnuplot can produce, but we won't need to know about those for this exercise.)
plot "A.dat" with lines means to get the data points from the file called "A.dat", and connect the dots with lines. There are other kinds of plots that gnuplot can do, but we won't need to know about those in this exercise.
gnuplot A.gnuplotThen, 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.
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.gnuplotYou 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.
CC foo.cpp -o foo ./foo gnuplot foo.gnuplot cp -f foo.png ~/public_html/cisc181/gnuplot chmod a+rx ~/public_html/cisc181/gnuplot
How do you make a shell script? See your Anderson textbook (the Unix book) for details!