Skip to main content
gamedev.net
🔒 Locked

Julia Set problem

Started by AN_D_K 5 replies 1.6k views
Original Post
AN_D_K
AN_D_K
I've been working on some fractal code to make a Julia set image and my iterative code looks something like this while ((INC < INCMAX) && (T > 2)) { INC++; TA = (float)(A*A) - (B*B) + C; TB = (float)(2*A*B) + D; A = TA; B = TB; T = (float)sqrt((A*A)+(B*B)); } Where T is the Threshold, and TA and TB are temporary values. I am using the points of the screen as my A and B (converted to -1.5 to 1.5 nomalised space) and C and D are both 0.356. My problem is that the functions all expand and never collapse in (expanding to huge values that can't be stored too). I've narrowed down the problem to just this loop. Could anyone clue me into why this isn't working? Thanks
ZedFx
ZedFx
looking up a *very* old vb project i did i have this:

Public Function Iterate(ByVal xn As Double, ByVal yn As Double, ByVal MaxIterations As Long) As Long'Dim x0, y0 As Double 'z(0)'Dim xn, yn As Double 'z(n)Dim x1, y1 As Double 'z(n + 1)Dim cur As IntegerFor cur = 0 To MaxIterations  x1 = xc + xn * xn - yn * yn  y1 = yc + 2 * xn * yn  If x1 * x1 + y1 * y1 > 4 Then 'escapes to infinity    Iterate = cur    Exit Function  End If  If (Abs(x1 - xn) < 0.0000001) And (Abs(y1 - yn) < 0.0000001) Then 'unchanging values on iterations, avoid unnecessary calculations    Iterate = MaxIterations    Exit Function  End If  xn = x1  yn = y1Next curIterate = MaxIterationsEnd Function


If you can work your way through that, it could help you (it does work btw!)

EDIT: xc and yc are the real and imaginary parts of the number the julia set is based on
AN_D_K
AN_D_K
The function actually works. It first looked like a big blob but I just had to change the threshold down to from 2 to 0.25.

http://www.andykeeble.co.uk/julia365.tga

Thanks for the help.
Dmytry
Dmytry
Should be:
while ((INC < INCMAX) && (T <font size="+1" color=red><</font> 2)){INC++;TA = (float)(A*A) - (B*B) + C;TB = (float)(2*A*B) + D;A = TA; B = TB;T = (float)sqrt((A*A)+(B*B));}

Dmytry
Dmytry
if you want me to optimize it (thing OP haven't asked for, BTW), and make code be more readable,
float r=start_r,i=start_i;float r2=r*r;float i2=i*i;int n=0;while(n<max_iterations_count && r2+i2<4){i=2*r*i+param_i;r=r2-i2+param_r;r2=r*r;i2=i*i;n++;};


As about optimizations, many useful non-trivial algorithmic optimizations based on properties of Julia fractal have been developed. For instance, AFAIK if all points on boundary of some square after same number of iterations leave radius<2 circle and come to same quadrant, you can be very very certain that entire interior of square will leave circle after same amount of iterations, if examined border points was sampled dense enough. And therefore if you color it by number of iterations, you can paint entire interior with same color. It could accelerate things significantly, i have seen old renderer that split image in octree-like way and for each cell draw its border where it is not drawn, and accordinly to result, either recursively calls itself for interior, or fills entire cell with same color.
AN_D_K
AN_D_K
Thanks for all the help and optimisations. I'll probably optimise the code later down the line as it's university work and we had to follow a certain way of doing it, and if we wanted to follow up on the work we could work on optimisations and writing up the theory in a couple of months.

For the corrent method, changing the loop to just INC< INCMAX does work just as well, and now the T isn't square rooted but when I added it back in it didn't work anymore. Lucky I kept my old code too.

However, I think I found quite a nice pseudo-3D effect by getting the wrong end of the stick. It looks a lot nicer than a few dots.

Thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.