[comp.lang.forth] Fractals and Forth...

ugweir@cs.Buffalo.EDU (Karl Weir) (06/08/89)

Has anyone had experence with fractal images in FORTH?  More
Prefered on the Mac???

If anyone can give the name of some *good* books, and/or some
reference articles it would be appriciated. 

			Thanx in advance..

Karl Weir			ugweir@sybil.cs.buffalo.edu
				mickarl@ubvms.cc.buffalo.edu
				mickarl@ubvm.cc.buffalo.edu

wmb@SUN.COM (Mitch Bradley) (06/09/89)

Here's a Mandelbrot program in Forth.  A guy who was taking a Forth
class from me wrote it.  The dialect is basically Forth 83, although
it's written for a 32-bit machine.  I haven't scrutinized it for
portability problems.  It calculates into a 1-byte-per-pixel memory
region, which can be a frame buffer or just memory.

No doubt everybody will now do the "me too" act and we'll see a zillion
different fractal programs.

Oh, by the way:

   >>   ( n1 shift-count -- n2 ) \ Logical right shift

   >>a  ( n1 shift-count -- n2 ) \ Arithmetic right shift

   <<   ( n1 shift-count -- n2 ) \ Logical left shift


-------------------------------------------------------
\ Calculates the Mandelbrot set in a 1280x1280 pixel space.
\ Requires that the following words be already defined:
\    memory  ( -- adr )  The starting address of the memory to use.
\    Must be at least 1280*1280 bytes
\    border  ( -- n )  The number of left-over pixels on each scan
\    line.

decimal

variable cimag  variable creal

\ scale factor is 2**28
\ root scale is 2**14

26 constant log-scale
log-scale 2/ constant log-root-scale
1 log-scale << constant scale

4 log-scale << constant size-limit

\ Debugging tool
: **10  ( n -- )  1 swap  0 ?do  10 *  loop  ;
: **5  ( n -- )  1 swap  0 ?do  5 *  loop  ;
: >s  ( n -- )  ( and dpl )
   dpl @ **10  /mod  ( rem quot )
   log-scale <<  swap log-scale 4 - dpl @ -  <<  dpl @ **5  /  4 <<  +
;
: s.  ( scaled-int -- )
   dup 0<  if  ." -"  then
   abs
   scale  /mod  ( rem quot ) (u.) type ." ."   ( rem )
   10 >>  10000 *  log-scale 10 -  >> .
;

: s*  ( a b -- a*b )  \ Multiply fractions with an implicit denominator
    log-root-scale >>a  swap log-root-scale >>a  *
;
: s-square  ( a -- a*a )  log-root-scale >>a  dup *  ;

: next-number  ( imag real -- imag' real' )
   2dup           ( i r i r )
   s-square       ( i r i r*r )
   swap s-square  ( i r  r*r i*i )
   -              ( i r  new-r )
   -rot  s* 2*    ( new-r new-i )

   cimag @ +  swap creal @ +     ( imag' real' )
;

: big-enough?  ( imag real -- flag )  s-square swap s-square +  size-limit >  ;

: fr3  ( -- pixel-value )
   255   cimag @  creal @   ( pixel  imag real )
   256 0  do   ( pixel  imag' real' )
      2dup  big-enough?  if  rot drop i -rot  leave  then
      next-number
   loop
   2drop       ( pixel )
;

\  12500 constant max-imag  \ 1.25 * scale
\ -20000 constant min-real  \ -2.00 * scale
\     16 constant real-delta
\    -20 constant imag-delta

 5 log-scale 2 - <<        constant max-imag  \ 1.25 * scale
 5 log-scale 2 - << negate constant min-imag  \ -1.25 * scale

 2 log-scale    << negate constant min-real  \ -2.00 * scale
 1 log-scale 1- <<        constant max-real  \  0.50 * scale

\ This is for a square picture in a 1280 x 1280 pixel space.
\ The coordinate space width is 5 * 2^27 == 2.5 * scale
\ The pixel      space width is 5 * 2^8  == 1280
\
\ Consequently, the space between pixels in the coordinate space
\ is  2^27 / 2^8  ==  2^19

1 log-scale 1- 8 - <<        constant real-delta
1 log-scale 1- 8 - << negate constant imag-delta

variable offset
: first-scan-line  ( -- )  memory offset !  ;
: put-pixel  ( pixel -- )  offset @ c!  1 offset +!  ;
: next-scan-line  ( -- )  border  offset +!  ;

: mandelbrot  ( -- )
   first-scan-line
   min-imag  max-imag  do
." Scan line" cr \ Debugging hack; report progress
      i cimag !
      max-real min-real do
         i creal !
         fr3  put-pixel
      real-delta +loop
      next-scan-line
   imag-delta +loop
;

koopman@a.gp.cs.cmu.edu (Philip Koopman) (06/09/89)

I published a fractal landscape program in Forth Dimensions
a while back.  The place it is most accessible is in
Dr. Dobb's Toolbook of Forth (Vol. II), chapters 24 & 25
  (M&T Books, 1987).

  Phil Koopman                koopman@greyhound.ece.cmu.edu   Arpanet
  5551 Beacon St.
  Pittsburgh, PA  15217    
Senior scientist, Harris Semiconductor
Harris has no idea what I'm saying.


--