ASCII Mandelbrot

C. David Shaffer cdshaffer at acm.org
Wed Oct 27 12:45:42 UTC 2004


Christopher Barham wrote:

>Thanks to both Avi and Richard for taking the time to help me out.  I was
>confused with ifTrue: and IfFalse, I, (half), remembered that it was
>considered better style to bracket your boolean conditions to make things
>slightly more readable;  but a whileTrue: is not the same as ifTrue:
>though... ;-)
>
>Once running I found that it was extremely slow with Transcript show:
>(probably because the chars had to be converted to strings and then
>flushed), changing things to nextPut: really sped things up.  The current
>method code is below. Can anyone suggest anything else I could improve to
>make it more 'Squeak-Like'  - I was thinking that sunit tests and a little
>refactoring would be useful next step.
>  
>

Chris,

As you start to think about SUnit tests, one of the problems you'll 
encounter is that the images generated are pushed right to the 
transcript.  Might I suggest that you write to a Stream?  Basically 
you'd make the changes I suggest below.  Then, if you want to write it 
to the transcript you can (see the end).  Anyway, nice example, fun to 
play with...thanks for putting it out there!

>    "Generate a MandelBrot image using only ASCII characters."
>    | d r n h |
>  
>
add s to your list of temps

>    Transcript show: Character cr.
>  
>
Change the previous line to:
    s := WriteStream on: String new.

>    1.1
>        to: -1.1001
>        by: -0.1
>        do: [:e | 
>            -2.0
>                to: 0.9601
>                by: 0.04
>                do: [:b |
>                    r := 0.0.
>                    n := 0.0.
>                    h := 127.
>                    [r * r + (n * n) < 4.0
>                        and: [h > 32]]
>                        whileTrue: [h := h - 1.
>                            d := r.
>                            r := r * r - (n * n) + b.
>                            n := 2 * d * n + e].
>  
>
Change Transcript on the next line to s

>                    Transcript
>                        nextPut: (Character
>                                value: (b > 1
>                                        ifTrue: [13]
>                                        ifFalse: [h]))].
>            Transcript show: Character cr]
>  
>
Change the previous line to
       s nextPut: Character cr]

Now, if you want to display your output on the Transcript you could:

Transcript show: s contents.

But you could also use s in a test case since it contains the pattern 
generated by your program.

self assert: s = 
'~~~~~~~~~}}}}}}}}|||||||||||||||||||||{{{{{zylszzz{{{{|||||}}}}}}}}}}}}}}}}
~~~~~~~}}}}}}||||||||||||||||||||{{{{{{{{zzzyxuqvtVz{{{{{||||||}}}}}}}}}}}}'

or you could push s out to a web page...it is really trivial to turn 
your code into a Seaside application so you can produce ASCII Mandlebrot 
for the masses :-)  Let us know if you want some guidance.

David

-- 
C. David Shaffer
http://www.cs.westminster.edu/~shaffer
http://www.shaffer-consulting.com




More information about the Squeak-dev mailing list