CS 3723/3721
Programming Languages

Rings of Boxes

 


The First Step: a Single Box
    The initial goal is to create a kind of elegant ring of boxes, perhaps looking like the diagrams on the left below, with 10, 16 and 20 boxes. (The blue lines are just for orientation.) So given any integer n, we want to create a ring of n boxes, looking as shown, without the blue lines. (Well, actually cyan.)

    The final goal is to be sort of a "flower" of boxes, as shown at the right below.

    The initial box is going to be the one on the positive x-axis in the figures below, immediately to the right of the center. It's just a little trigonometry to calculate exactly where the elements of that box are, starting with n and the value r of the distance from the origin to the first corner of that box. All these calculations are in the diagram in the center below:

     
     
                     

    Postscript Source for this Diagram: formula.ps
    Click on any picture above for a large .pdf version
          
     
     
     
     
     

    The Postscript code to produce the box is given at the top of the diagram in the middle above. The image produced, a single square oriented and placed correctly, is shown below. The image below also gives the various lines in the diagram in cyan to show that it is correct.


    Postscript Source for this Diagram: basebox.ps


The Second and Third Steps: a Ring of Boxes and then Nested Rings of Rings of Boxes
    Postscript code for a single ring of boxes is given on the left below. We only need an extra 7-line function, shown in red. Postscript code for a cascading ring of boxes is given on the right below. The additions, mostly one more function and one more loop are again given in red.

    Generate One Ring of Boxes
    %!PS-Adobe-2.0
    /n 6 def
    /a 360 n div def
    /b a 2 div def
    /r 100 def
    
    
    /tanb b sin b cos div def
    /x r 1 tanb sub div def
    /y x tanb mul def
    /s 2 sqrt y mul def
    /t y b sin div def
    
    
    /square {
    newpath
      r 0 moveto
      x y lineto
      x y add 0 lineto
      x y neg lineto
      closepath fill
    } def
    
    /ring {  % create ring of boxes
      1 1 n {  % loop from 1 to n
        pop % throw out loop variable
        square fill  % draw square
        a rotate  % rotate coords
      } for
    } def
    
    612 2 div 792 2 div translate
    ring
    
    showpage
    
                    
    Generate Rings of Rings of Boxes
    %!PS-Adobe-2.0
    /n 6 def
    /a 360 n div def
    /b a 2 div def
    /r 100 def
    /depth 10 def % number of rings
    
    /tanb b sin b cos div def
    /x r 1 tanb sub div def
    /y x tanb mul def
    /s 2 sqrt y mul def
    /t y b sin div def
    /ratio r t div def % ratio for scaling
    
    /square {
    newpath
      r 0 moveto
      x y lineto
      x y add 0 lineto
      x y neg lineto
      closepath fill
    } def
    
    /ring {
      1 1 n {
        pop
        square fill
        a rotate
      } for
    } def
    
    /rings { % create sequence of rings
      1 1 depth { % loop from 1 to depth
        pop % throw out loop variable
        ring % draw one ring of boxes
        ratio ratio scale % scale, next ring
        b rotate % small rotate correction
      } for
    } def
    
    612 2 div 792 2 div translate
    rings
    
    showpage

    Eight examples of images produced by these rings are at: Rings of Boxes.

    Finally: A flower with infinitely many rings: Infinite Flower (16.6K).

( Revision date: 2015-01-03. Please use ISO 8601, the International Standard Date and Time Notation.)