
[The following article appeared in the March 1994 issue of Gimme 
Arrays!, the newsletter of the Toronto ACM APL Special Interest Group.
You can reach the SIG at P.O. Box 384, Adelaide Street Post Office,
Toronto, Ontario, Canada, M5C 2J5. This article may be reproduced 
provided that the Toronto ACM SIGAPL chapter and author are acknowledged.
No warranty is given or implied by these scripts which are provided 'as is'.]


From Pentagons and Plasma Clouds to Cliff Dwellers (Part I)
Cameron Linton <crl@ipsalab.tor.soliton.com>

With dazzling images Clifford Reiter wove the story of the evolution 
of his mathematics visualization class. Clifford started the evening 
with a simple pentagon but by the end of his presentation, the 
audience was staring at three dimensional (3D) images of cubes with 
cliffs (where the Cliff Dwellers live), plasma clouds, and fractal 
mountains. He had even analyzed a bit map image of a bear and 
determined some of the properties of a crystal.

Clifford has been an Associate Professor of Mathematics at 
Lafayette College1 in Pennsylvania since 1983. He has taught Graphics 
courses with Turbo Pascal and Linear Algebra with APL. "I'm a 
mathematician," said Clifford, "I know how to program and I teach 
whatever I want as long as the department has assigned it."

Clifford's most recent challenge was to teach a Mathematics 
Visualization course during the between-term "overload" period with a 
minimum budget (an overload course is an intense, full credit course 
taught for three hours, four nights a week, for three weeks).

To solve the budget problem, Clifford used J. Classes were small 
but enthusiasm was high, and the course was a success. Clifford had 
problems sending his students home each evening, or taking a five 
minute break!

In the course, Clifford wanted to teach visualization by 
integrating the concepts of array processing in J with the 
mathematics of computer graphics. This article includes several of 
the following items:

- understanding screen coordinates
- plotting curves, lines, polygons, and Raster arrays
- studying transformations in the plane
- working with colour models and image processing
- projecting 3D images, perspective, and surfaces
- ray-tracing

Graphics: Screen coordinates, polygons, and transformations
Clifford's first assignment gave his students the opportunity to 
learn how to generate graphics of objects represented by colours and 
a coordinate system. Clifford asked his students to draw a red 
pentagon. Clifford's method was to start with the unit circle x^2 + 
y^2 = 1, use (1,0) as one of the pentagon's coordinates, and derive 
the other four coordinates by moving this coordinate one fifth of the 
way around a circle.

Each new point is one fifth of the circumference (2pi/5) away from 
the previous point. In J this is simply 2r5p1. Points on this circle 
all have coordinates (cos(t),sin(t)) where t is the angle measured 
from the positive x-axis. The first point (1,0) is when t=0. The 
second point is when t=(2pi/5) * 1, and so on.

Therefore the coordinates can be generated as:

NB. transpose outer product of (cos,sin) of  2p/5 times iota 5

|:2 1 o./ 2r5p1 * i.5

 1      0
 0.309017       0.951057
_0.809017       0.587785
_0.809017       _0.587785
 0.309017       _0.951057

To display these coordinates as integer data between (0,0) and 
(1000,1000) Clifford adds one and multiplies the coordinates by 500. 
The resulting coordinate pairs are the right argument to the spoly 
function defined below. The left argument of spoly specifies the 
colours with three numbers indicating the red/green/blue colour 
combination. The left argument of sogwin is the boundary and window 
size of the window to be created. The right argument is the window 
title.

  Run 'j1.js' (0!:2 <j1.js)


Now that spoly has been defined, it is trivial to display other 
geometric shapes in a variety of colours.  For example, we can draw a 
blue 17-gon or a red five-pointed star ['j2.js'].



Raster Arrays
A Raster array is an array of pixels that are lit to show a computer 
image. Therefore, a black and white (B&W) Raster array corresponds to 
a boolean array. A coloured Raster array can be represented by an 
array of numbers with a corresponding palette for 8-bit graphics.
A simple way to create a B&W Raster array is to build it up 
iteratively as shown.  To display this array as a Windows bit map 
file, use the writebmp8 script defined below along with a two-colour 
palette pal.


Remember to use a path name appropriate to your system configuration 
in the right argument to writebmp8. [Modify and run 'j3.js'.]

The resulting file egbmp8_1.bmp (Figure 2) can be viewed in two ways. 
One, you can put it in your c:\windows directory and use it as 
wallpaper in the Control Panel Desktop. Alternatively, you can call 
it up in a graphics viewer shareware package such as WinGIF or 
Graphics Workshop (2). Try experimenting with pal and the array!



Sierpinski Triangle

A Sierpinski Triangle is essentially a triangular pattern made up of 
copies of identical smaller shapes. Clifford illustrated how a 
Sierpinski Triangle can be generated by taking the vertices of the 
unit square {(0,0), (0,1), (1,1), (1,0)} and applying three 
transformations to it to create three smaller squares. "Iterating 
this process gives the Sierpinski triangle as a limit of polygons." 
Clifford wrote an iterated function difs to produce this result
['j4.js'].



Fractal Dimensions

Fractal dimensions are used in industry and in science to study an 
object's properties and composition. They sometimes provide clues 
about the relationship between two objects or help to identify a 
remote object. For example, students of one of Koko's (3) colleagues 
once took slides of pulp at a paper mill to study whether or not the 
pulp had been sufficiently ground. He tried to base his conclusions 
on the fractal dimension of the samples.

The fractal dimension, D, of a set in the plane is Lim{ln(N(h)) / 
ln(1/h)} as h goes to 0 and where N(h) denotes the number of boxes of 
size h needed to cover a set. The fractal dimension for square Raster 
arrays of size 2^n by 2^n can be approximated by first breaking the 
array up into m blocks in each dimension. A block is said to be 
"touching the fractal" if it contains a one. If N(m) is the number of 
blocks touching a fractal then for large m the approximate fractal 
dimension, D, is {ln(N(m)) / ln(m)}.

N(m) can be calculated by a series of "plus reduces" on "or 
reduces". That is, after reshaping the original array into smaller 
blocks, check for a one in each of the blocks and add up the number 
of occurrences.  Or, as Clifford said, "fuff up the data and go zing 
zing zing zing."

One lesson culminated with an experiment. The students were given 
a bit image of a dendrite crystal and asked to estimate its fractal 
dimension. To succeed the students needed to understand how to read a 
bit map file into an array, sample it, transpose it, and run the 
fractal dimension algorithm on the data. This meant they needed to 
understand the programming interface, how to index into and 
manipulate arrays, and execute an algorithm.

As Clifford noted a few times during his presentation, by using 
APL or J there is less code to deal with so the students are closer 
to the mathematics.


Footnotes

1. Lafayette College has about 2,000 students, a quarter of whom are 
engineers. The Math faculty has 19 full time positions plus three 
computer science people. We are grateful for the support of Lafayette 
in sponsoring this presentation.

2. WinGIF is a shareware product by Superset Software Corp for 
viewing BMP, GIF, PCX or RLE graphic image files.  Graphics Workshop 
is a shareware product by Alchemy Mindworks which can view dozens of 
graphic image file formats including BMP, GIF, TGA (Targa), TIFF, 
JPG, PCD.

3. Koko is a professor at Bucknell College, also in Pennsylvania, who 
was the speaker at the November 29, 1993 meeting of the Toronto ACM 
SIGAPL.



