[comp.sys.apple] AE, Mandelbrot generator

eldorado@EN.ECN.PURDUE.EDU (David D Jansen) (02/08/90)

Keywords: mandelbrot

Could someone with fractal experience tell me why I get weird data.  It gets
the basic shape but not quite right.  A friend of mine (a fractal "guru")
can't make out what the problem.  Here is my type declaration:

type 
imaginary = record {an imaginary number  i.e. a + bi}
              a : real;
              b : real;
            end;
region = record {a rectangle on the imaginary plane}
           amin : real;
           bmin : real;
           amax : real;
           bmax : real;
	 end;
screen = record {the data that describes a screen}
           where : region;
           bitmap : packed array [1..hor,1..ver] of boolean;
 	end;

procedure plotset (var pages:screen;var fraccalc:boolean);
{a procedure to put the fractal from the sent region into the variable page}
var
dval,z,val:imaginary;
h,v,n:integer;
begin
  dval.a:=(pages.where.amax - pages.where.amin)/(hor - 1); 
  dval.b:=(pages.where.bmax - pages.where.bmin)/(ver - 1); 
  for v:=1 to ver do
  begin
    for h:=1 to hor do
    begin
      val.a:=pages.where.amin + (h - 1) * dval.a; {val is the value of the}
      val.b:=pages.where.bmin + (v - 1) * dval.b; {current point on the screen}
      n:=0;
      z.a:=0;
      z.b:=0;
      while ((z.a*z.a) + (z.b*z.b) <= 4) and (n < maxiter) do
      begin
        z.a:=(z.a * z.a) - (z.b * z.b) + val.a;
        z.b:=2 * z.a * z.b + val.b;
        n:=n + 1;
      end;
      if (((z.a * z.a) + (z.b * z.b)) <= 4) then
        pages.bitmap[h,v]:=true
    end;
    writeln (v/ver*100:5:2,'% done');
  end;
  fraccalc:=true;
end;

Someone last year uploaded a mandelbrot generator and I modified his code to
test in my program.  No go, it was worse than my attempt.  I don't think I
tested my code in his program.  That was a mistake on my part.  I have had 
this problem for more than a year.  There could be a reward for the correct
answer.
While I am at it, what is Applied Engineering's phone number?  Did anyone
download the Prodos Hyper C that was uploaded?  Did it work?


Dave Jansen (The Gilded One)
eldorado@en.ecn.purdue.edu

"We have to make all our programs idiot-proof.  Idiots are very intelligent!"

BRL102@psuvm.psu.edu (Ben Liblit) (02/08/90)

Newsgroups: comp.sys.apple
Subject: Re: AE, Mandelbrot generator
References:  <9002071850.AA29665@en.ecn.purdue.edu>
Organization: Penn State University
Date: Wednesday, 7 Feb 1990 18:28:42 EST
From: Ben Liblit <BRL102@psuvm.psu.edu>
Message-ID: <90038.182842BRL102@PSUVM.BITNET>

In article <9002071850.AA29665@en.ecn.purdue.edu>, eldorado@EN.ECN.PURDUE.EDU
(David D Jansen) says:
>
>Could someone with fractal experience tell me why I get weird data.  It gets
>the basic shape but not quite right.

>type
>region = record {a rectangle on the imaginary plane}
>           amin : real;
>           bmin : real;
>           amax : real;
>           bmax : real;
>         end;

This declaration isn't the error source, but makes me cringe.  It seems that a
nicer-looking declaration, that better reflects what you're representing, would
be:

     TYPE
        region = RECORD
                    min, max : imaginary
                 END;

Of course, my format is different, but that doesn't matter -- it's the idea
that counts.

As for the actual source of your errors, your real variables are probably being
rounded off.  After a few hundred iterations, that can add up to some rather
large inaccuracies.  There are a few things you can try:

If your compiler has other, higher precision floating point types, use them.
Look for something called double or extended.  The tradeoff is reduction in
speed.

You could also junk the floating point altogether, and switch to straight
integer math.  It's blindingly fast, and I *think* it's supposed to be more
accurate.  Someone posted the core routines for Mandelbrot set iterating in C a
short while back.  Anyone remember where this appeared?  Major tradoff of going
all-integer:  you have to rewrite the multiplication and division routines.

                      Ben Liblit
                      BRL102 @ psuvm.bitnet -- BRL102 @ psuvm.psu.edu
                      "Fais que tes reves soient plus longs que la nuit."

gwyn@smoke.BRL.MIL (Doug Gwyn) (02/08/90)

In article <9002071850.AA29665@en.ecn.purdue.edu> eldorado@EN.ECN.PURDUE.EDU (David D Jansen) writes:
>Could someone with fractal experience tell me why I get weird data.  It gets
>the basic shape but not quite right.

That's easy -- in computing z.b you're using the NEW value of z.a but
you should be using the OLD value.  Use a temporary variable to keep
from clobbering the old data until you're done with it.

reeder@reed.UUCP (Doug Reeder) (02/09/90)

In article <9002071850.AA29665@en.ecn.purdue.edu> eldorado@EN.ECN.PURDUE.EDU (David D Jansen) writes:

>Could someone with fractal experience tell me why I get weird data.  It gets
>the basic shape but not quite right.  A friend of mine (a fractal "guru")
>can't make out what the problem.

>      if (((z.a * z.a) + (z.b * z.b)) <= 4) then
>        pages.bitmap[h,v]:=true
Nowhere do you initialize the bitmap to false, and Pascal does not
initialize your variables for you, so the value of any given point is
undefined when you start.  The simplest way to fix this is to add:
       else
         pages.bitmap[h,v]:=false

>    end;
>    writeln (v/ver*100:5:2,'% done');

etc.

Some pascal compilers will initialize your variables for you, but don't
depend on it.
-- 
Doug Reeder                                   USENET: ...!tektronix!reed!reeder
from ARPA: tektronix!reed!reeder@berkeley.EDU BITNET: reeder@reed.BITNET
the Little Mermaid on materialism:
I just don't see how a world that makes such wonderful things ... could be bad!