home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / applications / polysys / polysys.docs < prev    next >
Text File  |  1990-10-23  |  24KB  |  462 lines

  1. /* PolySys 1.0 ©1990 by Terry W. Gintz */
  2.  
  3. PolySys is an extended version of the 0L-system (string rewriting) described
  4. in The Science of Fractal Images (edited by Pietgen and Saupe.)  The basic
  5. algorithm has been expanded and modified extensively, and looping commands
  6. similiar to those found in other Turtle graphics systems (Logo, etc) have
  7. been added.  Support for three-dimensional drawing, with perspective, is also
  8. included in PolySys.  To enable the program to run on an Amiga 500 without
  9. extra memory, an alterate method of string interpretation has been
  10. implemented, using recursion.  String size is kept to a minimum, while the
  11. axiom is interpreted character by character.
  12.  
  13. For those who are not familiar with L-systems, a discussion of their basic
  14. features and how they differ from other turtle graphics is a good starting
  15. point.  Lsystems have as their base an axiom, which is a string of character
  16. commands, or a single character.  The character can be a command in itself,
  17. such as 'F' for forward, or it may be a production rule, which is a string of
  18. commands also.  The axiom and production rules are created and edited with
  19. separate editors.  When the axiom is 'run', the axiom is scanned for
  20. commands and/or rules.  Recursion is used to substitute where necessary a
  21. production rule string for a single character, to the depth specified at
  22. runtime.  In 0L-systems, quite long character strings can be built this way
  23. and then executed.  Once the string is built, it is then stripped of
  24. non-commands and executed without displaying the commands on the screen.
  25. The size of the drawing object is thus established and it is then scaled and
  26. drawn on the screen.  The first significant difference from other turtle
  27. graphics systems is now apparent: self-scaling.  The object will never
  28. overrun the screen boundaries.  Objects can be displayed to the limits of
  29. the window, regardless of window size.  PolySys uses a 640X400 hi-res
  30. screen, but any resolution is possible.
  31.  
  32. L-systems have a stack available to keep track of turtle position and
  33. variables whenever it is necessary to return to a particular point in the
  34. drawing.  This is useful for producing tree structures and other branching
  35. patterns.  PolySys puts on its stack the turtle postion, headings,
  36. linestyle, and color information.
  37.  
  38. The third significant difference between L-systems and other turtle graphics
  39. interpreters is in its handling of recursion.  In L-systems, recursion is a
  40. built-in feature.  You only need to specify the level of recursion, and the
  41. system does its string interpretation to that level.  PolySys uses recursion
  42. to call its own interpretation routines, and thus eliminates the need for
  43. lengthy strings to interpret.
  44.  
  45. The price for the above built-in features is speed.  Lsystems can never be
  46. as fast drawing a dragon curve, for instance, as a dedicated dragon-drawing
  47. program.  On the other hand, fractal generators have trouble matching the
  48. speed of L-systems where branching patterns are involved.  There is no need
  49. to recurse backwards to reach a branching node in L-systems:  just pop the
  50. stack, and you're there.  PolySys is capable of rendering polygons in three
  51. dimensions, but cannot match the speed of a dedicated 3-D modeler such as
  52. PageRender3D.  There is no hidden-line removal or shading included in
  53. PolySys, except for solid area fills or flood fill.  The 3-dimensional
  54. features can be used to produce more realistic looking plants than
  55. a 2-dimensional L-system could produce.  L-systems such as PolySys offer the
  56. user a chance to experiment with a very different approach to graphics
  57. production.  The author has included many examples of possible
  58. approaches to using PolySys, but these are only initial exercises, like
  59. tuning a guitar.  Productions of imaginative insight remain to be created
  60. and explored.
  61.  
  62. The following is a decription of the features of PolySys, starting with
  63. menus and windows, and concluding with available commands.
  64.  
  65. The Menus:
  66.  
  67. The Main or Project menu:
  68.  
  69. 'New' -- Erases the current axiom and production rules from memory.  Will
  70. ask for confirmation before doing so.
  71.  
  72. 'Clear' -- Clears the drawing window to the color specified by the
  73. background menu, one of 16 colors.
  74.  
  75. 'Draw' -- Opens the runtime window, which includes gadgets to set rotation
  76. angles, perspective, scale of drawing, and depth of drawing, etc.  This
  77. window has options to draw or redraw the current axiom, or draw a
  78. perspective version of the axiom (if in 3-D mode).  You can abort the drawing
  79. at any time by clicking in the drawing window with the left mouse button.
  80.  
  81. 'Save' -- Saves the current axiom and production rules, background, rotation
  82. angles and depth.  Scale and perspective are not saved.  A custom icon is
  83. created for each data file, that can load PolySys and draw itself from the
  84. Workbench.  You can save an axiom without production rules, or rules without
  85. an axiom, but you can't load and draw the latter.
  86.  
  87. 'Load' -- Loads an axiom and its associated data, and runs the axiom, if not
  88. NULL.  Does not automatically clear the screen each time an axiom is loaded,
  89. so you can display multiple axioms on the drawing window.
  90.  
  91. 'Save' and 'Load' use a Charlie Heath file requester to save and retrieve
  92. data files.  Enter the directory name into the upper box of the requester,
  93. or leave blank for the current directory.  Click on the file you wish to
  94. load, or enter a name in the file name box.  Click on Okay to load the file
  95. or click Cancel to abort the operation.
  96.  
  97. 'Quit' exits the program.  
  98.  
  99. The Edit menu:
  100.  
  101. 'Axiom' -- opens the axiom editor window.
  102. 'Rules' -- opens the rules editor window.
  103.  
  104. The Background menu:
  105.  
  106. You set the background color to any of 16 colors specified by the elements
  107. of this menu.  Does not automatically clear the screen.  You have to use the
  108. choose 'Clear' on the main menu to clear the screen to the color selected.
  109. Alternately, see use of the axiom/rule command 'c'.
  110.  
  111. The Windows:
  112.  
  113. The main drawing window is a hi-res interlaced window 640X400.  Smart
  114. refresh is used to save the current image while other windows are in use.
  115.  
  116. The runtime draw window has gadgets to set projection angles in 3-D mode.
  117. These are not used in 2-D mode.  You enter the level of recursion into the
  118. level gadget.  The maximum level of recursion is limited to 50.
  119. Use the scale slider to set the size of the drawing, .01 to full-size.
  120. Use the perspective slider to set the amount of perspective
  121. desired, if in 3-D mode.  A range of 1-30 is allowed, with 1 being the
  122. maximum perspective.  The perspective option is available only after the
  123. figure has been drawn without perspective.  (The parser needs to know limits
  124. on the figure size to calculate perspective.)  You don't have to wait for the
  125. first drawing to finish to use perspective.  Click on Draw to recompute
  126. and draw the current axiom.  Click on Redraw to redraw the figure after it
  127. has been drawn once.  Use this option after changing background, or clearing
  128. the screen.  Click on Perspect, if enabled, to draw a perspective version of
  129. the current axiom.
  130.  
  131. The axiom editor:
  132.  
  133. Enter the maximum number of angles in the number box, 1-360.  Enter an
  134. axiom, up to 55 characters in the axiom box.  Click on Okay to set the new
  135. axiom into memory.  Click on Cancel to revert to the previous axiom.  You
  136. must have a number in the number box >0, for the Okay option to work.
  137. Entering an axiom enables the Save and Draw options on the Main menu.  See a
  138. description of the available commands, if unsure what is acceptable as an
  139. axiom.
  140.  
  141. The rules editor:
  142.  
  143. The top row of gadgets are the command line gadgets, and the string gadgets
  144. are used to enter production rules and labels.
  145.  
  146. The string gadgets:
  147.  
  148. 'Rule' -- enter up to 45 characters for each production rule.
  149.  
  150. 'ARule' -- enter up to 45 characters for an alternate production rule, if
  151. a number greater than 0 is entered into the Prior gadget.  Otherwise, the
  152. ARule is added to the Rule for a double-long production rule.
  153.  
  154. 'Prior' -- sets a priority from 0-9 for the ARule.  A random generator is
  155. then used to decide which rule to use at runtime.  If the random number is
  156. less than the Priority then the ARule is used (even if NULL.)  If Prior is
  157. set to zero, the main Rule is always used, then the ARule, if any, is
  158. executed also. A buffer of 10000 bytes is set aside for random elements at
  159. runtime.  This is used to match the drawing created while sizing it, so that
  160. the drawing will not go off-screen during actual plotting.
  161.  
  162. 'Label' -- the label for the production rule.  Must be ASCII, non-blank, and
  163. contain no CTRL key sequence.  For rules that will be called recursively,
  164. it's best to use Upper Case letters for the label, as all lower case letters
  165. are used in commands.  Exceptions to this are the labels that you wish to be
  166. commands that change themselves recursively, such as 'F'.  E.g.  the rule may
  167. be F='FFF'.  At each level of recursion, the forward command triples in
  168. length, and a line is drawn three times as long as the previous level.
  169.  
  170. The command line gadgets:
  171.  
  172. 'Add' -- adds whatever production rule has been entered into the string
  173. gadgets, if the label does not duplicate a label already in use.  Up to 10
  174. rules can be entered.  The window below the string gadgets displays the
  175. current production rules in use.
  176.  
  177. '#' && 'Recall' are used in conjunction to edit a previously added rule.
  178. Enter the number of the rule, as displayed below, into the # gadget and
  179. click on Recall.  The rule is entered into the string gadgets.  This enables
  180. 'Change' and 'Delete' to change or delete the rule.  An extra step here
  181. prevents changing or deleting the wrong rule by mistake.  Hint:  you can also
  182. change the rule and then Add it in the same operation, if the label is
  183. changed.  Use 'Cancel' to change a another rule, if you don't really want
  184. to change or delete the rule you Recalled. (# gadget is disabled during a
  185. Recall operation.)
  186.  
  187. 'Cancel'  -- cancels the recall operation.
  188.  
  189. 'Insert' -- inserts a rule before the numbered rule as entered in the #
  190. gadget.  You can insert a rule after recalling and changing it, too.  You
  191. can't insert a rule with a label already in use.  You must add at least one
  192. rule before you can insert a rule.
  193.  
  194. 'Done' -- terminates the edit-rule session and closes the edit rule window.
  195.  
  196.  
  197. Axiom and production rules can be composed of the following commands:
  198.  
  199. Most of the original command set is included: '+','-','F','[',']'.
  200.  
  201.    '+' -- turn right by a set angle.  E.g.: '+45' would turn the turtle
  202. heading right 45 degrees (assuming 2-D mode and the number of possible
  203. angles is 360.)  A '+' followed by a non-numeric command defaults to 1, or
  204. 360/[number of possible angles.]  Turtle headings are based on a maximum
  205. number of turnings, set with the axiom editor.  The number of angles can be
  206. set from 1 to 360.  Thus, irregular angles such as PI/7 can be implemented
  207. as 14.
  208.  
  209.    '-' -- turn left by a set angle.  E.g.: '-' followed by a non-numeric
  210. command turns the turtle heading left 360/[number of possible angles]
  211. degrees (assuming 2-D mode.)
  212.  
  213.    'F' -- go forward a set length, and draw a line there from the original
  214. position.
  215.  
  216.    'D' -- same as 'F'.  Used when 'F' is used as a production rule.  E.g.
  217. F='FFF'.  Here, F becomes FFF at every stage of recursion, while D remains D
  218. (if not defined in another production rule.)
  219.  
  220.    '[' -- save turtle position and variables on stack.
  221.  
  222.    ']' -- retreive turtle position and variable from stack and decrement
  223. stack.
  224.  
  225.    'm' -- originally 'f', move a set length forward without drawing.
  226.    (Another difference from other turtle graphics -- no penup or
  227. pendown intermediate commands.)
  228.  
  229.    'n' -- same a 'm'.  Used when you need a single move command and 'm' is
  230. defined as a production rule.
  231.  
  232.    '|' (turn back, or turn by 180 degrees) is not implemented, because that
  233. would make no sense in 3-dimenional mode.  Use '+180' or the like, depending
  234. on the number of possible angles set.
  235.  
  236. In addition, the following commands are available:
  237.  
  238.    'c' -- clear the screen to the color specified on the background menu.
  239.  
  240.    'o' -- turn on 3-dimenional mode; must be the first character in the
  241. axiom to work.
  242.  
  243.    'p' -- set line color to the color specified by the following character or
  244. characters.  (Palette is limited to 16 solid colors.)  E.g.: 'p10' would set
  245. A and B pens to the color specified by the tenth element of the colortable.
  246.  
  247.    'b' -- set B Pen to the color specified by the following character or
  248. characters.  (136 colors are possible with dithering.)  E.g.:  'b1' would
  249. set the B pen to the color specified by the second element of the colortable.
  250. Dithering is based on an alternating dot pattern.  Linewise the pattern is
  251. ABABAB (A pixel B pixel etc), while a dithered fill alternates ABABABA with
  252. BABABABA.  
  253.  
  254.    '(' -- begin an area fill at present position -- each subsequent drawing
  255. position is added to the fill buffer until the limit of the buffer (1000
  256. points) or until a ')' is encountered.  If the limit of the fill buffer is
  257. reached before a ')' is encountered, the area is closed and filled, and a
  258. new buffer is started from that position.  If a ']' or 'm' is encountered
  259. before a ')' the area is also closed and filled and a new area fill started.
  260.  
  261.    ')' -- closes the area fill and fills the area (if preceded by '('.)
  262.  
  263.    'w' -- set line width to the width specified by the following character
  264. or characters.  Width is limited to one half the drawing height -10 (or 190
  265. for a full-sized drawing.)  The width command is used mainly in
  266. 2-dimensional mode, as it is a line-implemented rather than
  267. pixel-implemented width.  It will produce uneven results in 3-dimentional
  268. mode.  E.g.: 'w10' would make the current line width 10.
  269.  
  270.    'l' -- set line length to the length specified by the following
  271. character or characters. E.g.: 'l10' would make the current line length 10.
  272.  
  273.    's' -- scale length by the factor specified by the following character or
  274. characters.  E.g.: 's1.06' would multiply the current drawing length by 1.06.
  275.  
  276.    'd' -- delay execution of the next command by the delay length specified
  277. by the following character or characters.  Delay time is specified in
  278. fiftieths of a second, limited to 999 fiftieths or 16.65 seconds maximum.
  279. For example: 'd60' would produce a delay of 1 second.
  280.  
  281.    'r' -- repeat the execution of the following commands by the number
  282. specified by the following character or characters.  The default is 1, or
  283. one repetition.  For example: 'r5' would repeat a loop 5 times.
  284.  
  285.    'rv' -- repeat the execution of the following commands until the current
  286. heading is reached again.  Used to create turtle graphics subroutines that
  287. stop when the the total turning is a multiple of 360 degrees.
  288.  
  289.    '@' -- end of repeat loop.  Repeat loops can be nested to a level of 10.
  290. You should begin and end a repeat loop inside of the same production rule
  291. or axiom, to avoid potential problems with the interpretation of that loop.
  292. Since recursion is used to interpret a production rule character by
  293. character, the '@' can only send the interpreter back to the beginning of
  294. that loop.  Trying to get back to an axiom's repeat loop using a '@' from a
  295. production rule will trigger a syntax error.  Repeat loops that follow each
  296. other should be separated by a delimiter.  E.g.: 'r4!rF@@'.  The '!' between
  297. 'r's  is necessary to allow the parser to set the repeat counter on the
  298. previous repeat loop and the return point of that loop before beginning the
  299. next repeat loop.  (Defaults or buffer values are set when non-numeric
  300. values are encountered.)
  301.  
  302.  
  303.    'h' -- set the variable specified by the next character (must be a lower
  304. case letter a-z) to the value specified by the following character or
  305. characters.  E.g. 'ha2.5' would place the value 2.5 in 'ha'.  All variables
  306. are initiallized to 0 each time the axiom is run.
  307.  
  308. The 26 possible 'h' variables may be used to set line length, line
  309. width, scale, palette and B Pen, and repeat loop counters.  For example:
  310. "pha" would set the drawing color (pens A and B) to the value contained in
  311. ha (assuming a valid number is contained in ha.)  The syntax is 'chx', where
  312. 'c' is the basic command and 'hx' is any one of 26 floating-point variables.
  313. 'h' variables may also be used with '+' and '-' to make turnings dependent on
  314. variables.  E.g. '+hb' would direct the turtle to turn its heading right by
  315. the value contained in 'hb'.  It is necessary to add a delimiter in
  316. front of the 'h' variable, if it follows another command and it is not
  317. intended to extend that command.  E.g. '+hb1' should be entered as '+!hb1'
  318. to prevent misinterpretation by the parser.  Also, watch out for dangling
  319. commands that could be interpreted as extended commands from axiom to rule
  320. or rule to rule.  At runtime, the parser looks at axioms and rules as if one
  321. long string, even though they look separate.
  322.  
  323.    'f' -- flood fill starting at the current position, with the current
  324. pen colors.  Should be preceded by a few 'm's, to move the drawing
  325. position off a previously drawn line to the area you want to fill.  E.g.:
  326. 'mmmf' would move the drawing position three lengths and fill outward any
  327. pixels the same color as the drawing position and not the same color as the
  328. drawing pen.
  329.  
  330. Extended command set:
  331.  
  332.    The following extensions of the previous commands are available:
  333.  
  334.    '*' -- multiply the previous command by the following variable.  Can be
  335. used with 'p', 'b', 'w', 'l', 'h', and 's'.  E.g.: 'l*a' multiplies the
  336. line length by the value contained in ha.  Note that the 'h' is not used in
  337. any of the extended commands to designate the variable (except for the
  338. variable 'hh'.)  A value added to an integer unit, such as line width, is
  339. added as the integer value of the variable.
  340.  
  341.    '+' -- add to the previous command the following variable.  Can be used
  342. as '*'.  E.g.: 'w+a' adds the contents of 'ha' to the current width.
  343.  
  344.    '-' -- subtract from the previous command the following variable.  Can be
  345. used as '*'.  E.g.: 'ha-b' subtracts the value of variable 'hb' from 'ha'
  346. and stores the result in 'ha'.
  347.  
  348.    '>' -- if the value contained in the previous command is greater than the
  349. value of the following variable, set the previous command's value to either
  350. 1.0 or 0 (0 if the previous command was 'p','b' or 'h'; 1.0 if the previous
  351. command was 's','l' or 'w'.)  E.g.:  'p>a' -- if the current drawing pen A
  352. is greater than the value of 'ha' set the A and B pens to 0.
  353.  
  354.    '<' -- if the value contained in the previous command is less than the
  355. value of the following variable, set the previous command's value to either
  356. 1.0 or 0 (0 if the previous command was 'p','b' or 'h'; 1.0 if the previous
  357. command was 's','l' or 'w'.)  E.g.:  'p>a' -- if the current drawing pen A
  358. is less than the value of 'ha' set the A and B pens to 0.
  359.  
  360.    '=' -- if the value contained in the previous command is equal to 0 or
  361. 1.0 (0 if the previous command was 'p','b' or 'h'; 1.0 if the previous
  362. command was 's', 'l' or 'w') set the value of the previous command equal to
  363. the following variable.  E.g: 'l=s' -- if the line length is equal to 1.0
  364. then set the length equal to the value of 'hs'.
  365.  
  366. Thus, using the previous extended commands, any incrementing or decrementing
  367. loop involving color or line style can be set.  E.g.: 'r4w+aw>bw=c@' -- add
  368. the value of a to the linewidth; if the line width is greater than 'hb' set
  369. it equal to 'hc'; repeat this loop 4 times.
  370.  
  371. Additional extended commands apply to 3-D mode:
  372.  
  373.    'r' -- roll the specified angle.  E.g.:  '+45r' rolls the current heading
  374. 45 degrees right (assuming the maximum number of turnings is set to 360.)
  375. Rolling is the same as rotating the x axis and y axis around the z axis, or
  376. tilting sideways.
  377.    'p' -- pitch the specified angle.  E.g.: '-hap' pitches the current
  378. heading left by the number of degrees equal to the value of 'ha'.
  379. Pitching is the same as rotating the x axis and z axis around the y axis, or
  380. tilting forward or backward.
  381.    'y' -- yaw the specified angle. E.g.: : '+y' yaws the current heading
  382. right one degree (assuming the maximum number of angles is set to 360.)
  383. Yawing is the same as rotating the y axis and z axis around the x axis, or
  384. spinning the figure on the line of projection.
  385. Yawing must be followed by pitching or rolling to notice any change in
  386. heading.  Only 'p' and 'r' update the 3-D turtle heading directly.
  387. Yawing is useful for spinning an object inside of a repeat loop, to create
  388. multiple images of the object on screen.  'r' and 'p' are sufficient to
  389. draw the object's initial view.
  390.    Either 'r','p' or 'y' must be used with the heading commands '+' and '-'
  391. when in 3-D mode, or a syntax error will occur.
  392.  
  393. Syntax errors that occur during runtime are displayed by changing the
  394. background color to an error color and flashing the screen.
  395. The possible errors are as follows:
  396.  
  397.    color            error
  398.      0 -- palette incorrectly set via 'h' variable (<0 or >15 or h
  399. designation is missing -- 'h' followed by a label that is not a lower case
  400. letter.)
  401.      1 -- back color incorrectly set via 'h' variable (<0 or >15.)
  402.      2 -- random elements exceed 10000, the current limit.
  403.      3 -- 'h' variable incorrectly set (designation missing or buffer length
  404. exceeded -- >9 digits)
  405.      4 -- '+' variable incorrectly set (h designation missing; 3-D extended
  406. command missing ('r','p',or 'y'); buffer length exceeds 9 digits, or an 'h'
  407. variable was used whose value is greater than the maximum number of angles
  408. set with the axiom editor.)
  409.      5 -- '-' variable incorrectly set (h designation missing; 3-D extended
  410. command missing ('r','p',or 'y'); buffer length exceeds 9 digits, or an 'h'
  411. variable was used whose value is greater than the maximum number of angles
  412. set with the axiom editor.)
  413.      6 -- 'l' variable incorrectly set (h designation missing/ buffer length
  414. exceeds 9 digits.)
  415.      7 -- 's' variable incorrectly set (h designation missing/ buffer length
  416. exceeds 9 digits.)
  417.      8 -- 'w' variable incorrectly set (h designation missing/ buffer length
  418. exceeds 9 digits.)
  419.      9 -- 'r' variable incorrectly set (h designation missing/ buffer length
  420. exceeds 9 digits.)
  421.      10 -- repeat loop is broken (user tried to use '@' to end an axiom
  422. repeat loop from a rule, etc).)
  423.      11 -- stack overflow (stack exceeds 60.)
  424.      12 -- stack underflow (attempted to pop stack that was already zero.)
  425.  
  426. Errors that do not cause a syntax error are:
  427.  
  428.    Nesting repeat loops to a greater depth than 10 ('r' ignored.)
  429.    '@' without 'r' (ignored.)
  430.    'r' without '@' (no repeat.)
  431.    '[' without ']' (stack incremented, unless stack exceeded.)
  432.    '(' without ')' (fills object at end of drawing.)
  433.    ')' without '(' (ignored.)
  434.    Delays greater than 999 (delay buffer truncated to 3 digits.)
  435.    Fill buffer would exceed 997 (area fill ended and restarted at 0.)
  436.    Forever loop ('rv' where the angle of turning never gets back to where it
  437. started.  Note: you can stop any drawing at any time by clicking in the
  438. drawing window with the left mouse button.)
  439.  
  440.  
  441. Bibliography:  suggested reading for studies in LSystems and turtle graphics:
  442.  
  443.    The Science of Fractal Images, edited by Peitgen and Saupe, 1988
  444. Springer-Verlag New York (Appendix C)
  445.  
  446.    Turtle Geometry -- The Computer as a Medium for Exploring Mathematics, by
  447. Harold Abelson and Andrea diSessa, 1981 The Massachusetts Institute of
  448. Technology
  449.  
  450. Any questions, comments or donations for PolySys should be address to:
  451.  
  452. Terry W. Gintz
  453. 4237 Marcum Lane
  454. Eugene, Or 97402
  455.  
  456. A donation of $20 for PolySys gets you the documentation to all my current
  457. programs, plus additional help with PolySys if you need it, plus any of the
  458. other programs for $5 off their suggested price, plus upgrade privileges for
  459. $10.
  460.  
  461.   
  462.