Graphics

Shang has no built-in functionalities for handling graphics. However there is a package of programs written in the Shang language which can create 2D and 3D plots. The package saves pictures as encapuslated postscript files. In order to print or display the images created by plot.x, you need to download and install the Ghostscript and GSview programs at
http://pages.cs.wisc.edu/~ghost/

To use this package, you need to run the file plot.x, which is located in the programs directory. Or just run the following command before you plot

 include("plot.x");
The package plot.x defines a class named figure. To make a plot, first create a figure object using
 fig = figure.new();
If X and Y are the coordinates of a sequence of points, then
 fig.plot(X, Y);
will plot Y against X. The result is the points joined by straight line segments.

To plot (X,Y) as dots, use

 fig.plot(x=X, y=Y,  linetype = "dot");
To plot (X,Y) as circles, use
 fig.plot(x=X, y=Y,  linetype = "circle");
To plot a smooth curve through the (x,y) points, use
 fig.plot(x=X, y=Y,  linetype = "curve");
To plot a smooth curve through the (x,y) points, using red color, and making the line thicker
 fig.plot(x=X, y=Y,  linetype = "curve", linewidth = 2, color=[1,0,0]);
When the plotting is done, pick a file name (extension eps) and save the picture
 fig.save("file_name.eps");

For example, the following commands will plot some curves and dots

     with("plot.x");
     x = linspace(-5, 5, 10);
     fig = figure.new();
     /* draw small circles at (x,y) locations */
     /* color is vector of 3 numbers between 0 and 1, in rgb format */
     fig.plot{x=x, y=cos(x), linetype="circle", color=[1, 0, 0]};
     /* connect the dots with solid lines, linetype is the default value "solid" */
     fig.plot{x=x, y=cos(x), color=[0,1,0.5]};
     /* draw circular dots at location underneath the previous curve */
     fig.plot{x=x, y=cos(x) -1, linetype="dot", color=[1, 0, 1]};
     /* connect the dots with smooth curve */
     fig.plot{x=x, y=cos(x) -1, linetype="curve", color = [0, 0, 1]};
     /* set the xrange and yrange of the picutre */
     fig.setXrange([-5.5, 5.5]);
     fig.setYrange([-2.5, 1.5]);
     fig.save("curves.eps");
\includegraphics[height=0.31\textheight,width=0.9\textwidth, clip]{curves.eps}
The following commands will draw a butterfly
with("plot.x");
fig = figure.new();
for k = 0 : 99
   theta = linspace(k * 2 * pi, (k + 1) * 2 * pi, 500);
   rho = exp(cos(theta)) - 2 * cos(4*theta)+sin(theta/12).^5;
   x = rho .* cos(theta);
   y = rho .* sin(theta);
   fig.plot{x=x, y=y, linetype="curve", color=rand(3)};
end
fig.style = "empty";
fig.save("butterfly.eps");
\includegraphics[height=0.31\textheight,width=0.9\textwidth, clip]{butterfly.eps}

For more examples, check out Shang website.

oz 2009-12-22