Mandelbrot set generation help

Started by
16 comments, last by MDI 20 years ago
Sorry for posting this here. This is a homework question, but I'm not looking for someone to give me an answer. We've been tasked with having to generate some fractals in Java, I've already completed Menger's Sponge and Sierpinski's triangle, but the generation of the Mandelbrot and Julias-Fatou sets are giving me a bit of trouble (I have actually generated a Mandelbrot set before, I posted a picture in the thread with the animated GIFs a while back, but I've since forgotten how exactly they're done). I've tried to write the code myself, based on the limited information given to us in the notes, and some other online articles. Here's the relevant code (I've only tried the Mandelbort set so far): *Code deleted to stop plagiarism.* However, when this is plotted, all I get is a big black line running from the top left corner to the bottom right. What I'm asking is for a hint as to where I'm going wrong. I'm not looking for someone to give me an answer (as that would count as plagiarism). Is my code close to what it should be? grhodes: I'd apprecite it if you closed this thread when a satisfactory hint is given. Thanks in advance. [edited by - MDI on April 14, 2004 6:25:18 AM]
Advertisement
Unless you''re using some really screwy Complex class, I think the problem is a domain error. The Mandlebrot set is only really well defined for |z| < 2 . (Or rather only generates pretty pictures when |z| < 2 or so). You need to scale your x and y before constructing a complex number from them.
The maths looks ok, but not at all optimised. The loop doesn't seem to be exiting though when mag > 2.

Heh just spotted a slightly larger problem. A nice render of the M-Set will be given when plotted from -2 to 2 on the Real axis and -1.5 to 1.5 on the Imaginary axis. Oh deary me!

[edit] seems SiCrane got there first [/edit]

[edited by - higherspeed on April 3, 2004 6:12:25 AM]
Sorry, I''m working on a machine not connected to the ''net, so I had to type out the code manually. There is of course a "break" in the "if" statement.

SiCrane, are you saying that I should change this line:

Complex cons = new Complex(x, y);

?

Thanks for the replies so far.
That''s pretty much what he''s saying. You need to scale your x/y coordinates so that your complex numbers are generated as -2<=x<=2 and -1.5<=y<=1.5 to get the interesting part of the fractal. All the rest of it outside these ranges is pretty boring. If you can reassign these mappings, you can "zoom in" on the set, to show the infinitely increasing detail (at least, to the limits of double precision). The full set at [-2,2] and [-1.5, 1.5] (as higherspeed recommended) isn''t really all that interesting, all things considered. The good stuff is only found "deeper" in.

The way I like to do it is to assign 4 variables-- x1,y1,x2,y2 --the coordinates of a zoom rectangle within the set, and use x/screenwidth and y/screenheight to linearly interpolate these ranges. This way, I can use the mouse to define zoom rectangles, and reassign the ranges to zoom in.

Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller
OK, I''m now calculating the scaling using this method:

I have two complex numbers, min and max, which define the minimum and maximum extents of the complex plane. These are set to -2 - 2i and 2 + 2i respectively as their default values.

I then calculate the "width" (incx) and "height" (incy) using max.real - min.real / screensize.width and max.imaginary - min.imaginary / screensize.height.

My "constant" is then new Complex(x + incx * width, y + incy * height);

However the fractal is not drawing correctly (this is a big improvement over it not drawing anythign at all, though!). The best I can describe how it looks like would be kind of similar to a leopard skin stretched out. It is both symmetrical in the horizontal and vertical planes.

Anyone any ideas?

Thanks.
You are interpolating wrong. You calculate, for instance, incx as (max-min)/width, then when building the complex number, you multiply by width as (x+incx*width), which is algebraically equivalent to (x+(max-min)), and not the desired result. What you need to do instead is to multiply your interval by x, as in (x+incx*x), which equates to (x + (max-min)(x/width)). In this fashion, x/width acts as your interpolant, incrementing in the range of [0,1], 0 when x=0 and 1 when x=width.

edit: Change my screwup...
new Complex(min.real+incx*x, min.imaginary+incy*y)

Hope that helps.

edit: Ack. You're right, SiCrane. That's what I meant. Sorry.
Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller

[edited by - VertexNormal on April 3, 2004 4:14:47 PM]
min.real + incx * x, min.imaginary + incy * y
I am intrigued, how did you construct your menger sponge in Java?
I created some art based on reverse menger sponges and similar fractals a while back, but with spheres in openGL, however it ran extemely slow even at the 2nd and 3rd level of recursion. I didn''t bother fixing it
I am intrigued, how did you construct your menger sponge in Java?
I created some art based on reverse menger sponges and similar fractals a while back, but with spheres in openGL, however it ran extemely slow even at the 2nd and 3rd level of recursion. I didn''t bother fixing it

This topic is closed to new replies.

Advertisement