2.3.2.2 Creating a Formex using patterns

Another way of creating a Formex, is by using the coordinate generating functions pattern and mpattern. These functions create a series of coordinates from a simple string, by interpreting each of the characters of the string as a single unit step in one of the cordinate directions, or as some other simple action. These functions thus are very valuable in creating geometry where the points lie on a regular grid.

In this case, a line segment pattern is created from a string.

The function pattern(s) creates a list of line segments where all nodes lie on the gridpoints of a regular grid with unit step. The first point of the list is [0,0,0]. Each character from the given string s is interpreted as a code specifying how to move to the next node. Currently defined are the following codes:
0 = goto origin [0,0,0]
1..8 move in the x,y plane
9 remains at the same place
When looking at the plane with the x-axis to the right,
1 = East, 2 = North, 3 = West, 4 = South, 5 = NE, 6 = NW, 7 = SW, 8 = SE.
Adding 16 to the ordinal of the character causes an extra move of +1 in the z-direction. Adding 48 causes an extra move of -1. This means that 'ABCDEFGHI', resp. 'abcdefghi', correspond with '123456789' with an extra z +/-= 1. The special character '\' can be put before any character to make the move without making a connection. The effect of any other character is undefined.

This method has important restrictions, since it can only create lines on a regular grid. However, it can be a much easier and shorter way to define a simple Formex. This is illustrated by the difference in length between the previous creation of a square and the next one, although they define the same Formex (figure 2.3).

	F=Formex(pattern('1234'))

Some simple patterns are defined in simple.py and are ready for use. These patterns are stacked in a dictionary called 'Patterns'. Items of this dictionary can be accessed like Patterns['cube'].

	#!/usr/bin/env pyformex
	from simple import *
	c=Formex(pattern(Pattern['cube']))
	clear();draw(c)

Figure 2.6: A cube
 
cube.png