Be patient, a programming question is coming.
I introduce a discussion of boxes with edges parallel to the coordinate axes.
These boxes do not have to be cubes. They are identified by "located vectors"
which are diagonals through the center of the box. Such a located vector
uniquely determines the box, which in special cases can be a face or an edge.
However, in three dimensions a box can be identified by eight different located
vectors: these may begin at any of the eight corners of the box and pass
through
the center of the box to the diagonally opposite corner. Thus our first task
is
to identify a canonical representation of a box. I will do that below. Your
task will be to write a verb that finds the faces of a box.
A "located vector" is represented by a table a ,: h in which vector a gives the
location of the vector's tail, and vector h contains the components, thus
providing the direction and length of the located vector. The located vector
goes from location a (the tail) to location a+h (the head). All of this makes
sense in any number of dimensions; we can draw pictures in three dimensions
(where #a and #h are both 3).
Items of array "cubes" below are the eight vectors located at 0 0 0 which
represent unit cubes:
]cubes =: 8 2 3 $ cubedata
0 0 0
_1 _1 _1
0 0 0
_1 _1 1
0 0 0
_1 1 _1
0 0 0
_1 1 1
0 0 0
1 _1 _1
0 0 0
1 _1 1
0 0 0
1 1 _1
0 0 0
1 1 1
Items of array "faces" below represent the faces of the particular unit cube
represented by 0 0 0 ,: 1 1 1
]faces =: 6 2 3 $ facedata
0 0 0
0 1 1
0 0 0
1 0 1
0 0 0
1 1 0
1 1 1
_1 _1 0
1 1 1
_1 0 _1
1 1 1
0 _1 _1
Verb cr (canonical representation) gives the "canonical" located vector
representing a given box. In three dimensions the canonical located vector
starts at the lower left rear corner of the box and proceeds to the upper right
front corner. As a result its components are all positive. (Here is how I am
drawing pictures in three dimensions with the axes labeled 0, 1, and 2:
2
|
|
|_________1
/
/
/
0
)
cr =: ( {. + 0.5 * [: (- |) {: ) ,: [: | {:
cr"_1 cubes
_1 _1 _1
1 1 1
_1 _1 0
1 1 1
_1 0 _1
1 1 1
_1 0 0
1 1 1
0 _1 _1
1 1 1
0 _1 0
1 1 1
0 0 _1
1 1 1
0 0 0
1 1 1
cr"_1 faces
0 0 0
0 1 1
0 0 0
1 0 1
0 0 0
1 1 0
0 0 1
1 1 0
0 1 0
1 0 1
1 0 0
0 1 1
DO YOU REMEMBER YOUR ASSIGNMENT? Write a verb that finds the faces of a box.
That is, given a located vector representing the box, find located vectors
representing the faces.
--Kip Murray
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm