How to make pictures using postscript programs (part 9) ----------------------------------------------------------------------------- A way to make statements be conditionally executed is the if or ifelse statement. This statement looks like condition {code block, in braces} if or condition {code block in braces, if-part} {code-block, else-part} ifelse The simplest condition is an equality test, command eq, which compares the previous two numbers. So ------------------------Example 19------------------------------------------- %!PS-Adobe-2.0 %%BoundingBox: 0 0 230 20 1 1 10 { 3 eq {newpath 10 10 5 0 360 arc stroke} {newpath 10 10 5 0 360 arc fill} ifelse 20 0 translate } for showpage ----------------------------------------------------------------------------- will draw a horizontal row of circles of radius 5, spaced 20 units, only number three will be an empty circle (stroke) whereas the other are full circles (fill). ----------------------------------------------------------------------------- We can combine that with recursion to make fractals. This is just one way to create fractals, these are self-similar fractals. The idea is that the entire picture is the union of several smaller copies of the picture, so we want to create the picture at level 7 (e.g.), we write it as several smaller copies of the picture at level 6, and so on, each time decreasing the level, and only at level 0, when all the accumulated scale factors are making the picture very small, we just draw a dot, or triangle, or outline of the picture: scaled that much, it looks like a dot anyway. The key for the resulting shape is the choice of the scale factor and translations and rotations you use to place the smaller copies. ---------------------------------------------------------------------------- Below is code for the Sierpinski triangle, invented by Waclaw Sierpinski, a polish mathematician (mostly classical number theory) in the first half of the 20th century. ------------------------Example 20------------------------------------------- %!PS-Adobe-2.0 %%BoundingBox: 0 0 300 300 /level 7 def /Triangle {level 0 eq {newpath 0 0 moveto 300 0 lineto 150 150 3 sqrt mul lineto closepath fill} {gsave 0.5 0.5 scale /level level 1 sub def %Decrease level Triangle %Left Triangle 300 0 translate Triangle %Right Triangle -150 150 3 sqrt mul translate Triangle %Top Triangle /level level 1 add def %Increase level again grestore} ifelse } def Triangle showpage