[Edu-sig] Just for Fun: Another Fractal (ASCII output)

Kirby Urner kurner at oreillyschool.com
Sun Mar 2 21:34:34 CET 2014


Below is an add-on to the above simple fractal maker
allowing for POV-Ray output.

Here are screen shots of the output:

http://flic.kr/p/kCDaRB  (ASCII art)

http://flic.kr/p/kCLp2F   (POV-Ray)

Since the "ASCII canvas" is denser in the X than Y direction,
my little jumps in the complex plane were 0.02 and 0.05
respectively.

But the POV-Ray canvas looks better if each cell is a square or
sphere -- I'll use spheres but of course feel free to tweek / alter
this code however -- and jumps of 0.02 in both directions.

So the sphere radius is 0.01 (half a jump).

The Fractal class will need to be updated accordingly i.e.
in __init__:

                x += 0.02
            y -= 0.02  #  <--- changed from 0.05

and in __str__ in you like (it won't work unless the address
keys are the same -- but __str__ isn't needed for outputting
in .pov format).

Then, and the end of the file you might do as follows, to
preserve raw ASCII output as an option:

def _test1():
    # run me and open the file in a text editor to see 'fractal'
    f = Fractal(-2.2, 1.4, 0.8, -1.4)
    with open("mandelbrot.txt","w") as mandelbrot:
        print(f, file = mandelbrot)

def _test2():
    # run me and then render me in POV-Ray to see 'fractal'
    f = Fractal(-2.2, 1.4, 0.8, -1.4)
    pov_out.make_file(f)  # mandelbrot.pov is the output

if __name__ == "__main__":
    _test2()

Below is the pov_out module, to be imported.

Kirby

"""
Just for Fun:

Simple Fractal -> POV-Ray file

-- for import into simple_fractal.py
or import simple_fractal into this module

(cc) Kirby Urner, MIT license
4dsolutions.net

Example bash command lines to generate the output file and
then render it into a PNG file:

mackurner:Documents kurner$ python3 simple_fractal.py
mackurner:Documents kurner$ povray +Imandelbrot.pov +A[0.1] +W1024 +H768

Example output:
http://flic.kr/p/kCLp2F  (the PNG file, unedited)

"""


template = """\
#include "colors.inc"
#include "textures.inc"
#include "shapes.inc"

camera { location<-1,0,-3> // standpoint of the viewer
         look_at <-1,0,0> // where to look at
         right x*image_width/image_height  // aspect ratio
         angle 0 // camera angle
       }

light_source{ <1000,1000,-1500> color White}
"""

sphere_tmpl = """\
    sphere{{ <{x},{y},{z}>, {radius}
         texture{{
             pigment{{ color {color} }}
            }} // end texture
         }} // end sphere
"""

def make_pov(F):
    """
    eat Fractal object, output POV-ray spheres
    for inclusion in a POV-Ray file
    """
    rows = ""
    y = F.top_y
    radius = 0.01
    while y >= F.bottom_y:
        row= ""
        x = F.left_x
        while x <= F.right_x:
            v = abs(F[(x,y)])
            # calibrate by magnitude of stored value
            if v >= 2:
                color = "rgb<1,1,1>"
            elif 2 > v >= 1.5:
                color = "rgb<1,0.65,0>"
            elif 1.5 > v >= 1.3:
                color = "rgb<0.45,0.45,0>"
            elif 1.3 > y >= 1.0:
                color = "rgb<0.25,0.25,0>"
            else:
                color = "rgb<0.25,0.25,0>"
            row += sphere_tmpl.format(x=x, y=y, z=0, radius=radius,
color=color)
            x += 0.02
        rows += row
        y -= 0.02
    return rows

def make_file(the_fractal):
    """
    eat a Fractal type object, a subclass of dict with complex numbers
    already stored in each cell, a result of processing M-seed type
    objects
    """
    # write the file
    with open("mandelbrot.pov", "w") as povout:
        povout.write(template)    # header
        body = make_pov(the_fractal)
        povout.write(body)  # main content
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20140302/ea76288f/attachment.html>


More information about the Edu-sig mailing list