Author Topic: The buddhabrot (challenging ?)  (Read 26074 times)

0 Members and 2 Guests are viewing this topic.

RobbeK

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #30 on: December 17, 2014, 07:37:52 AM »
Hi,

Nice images , Charles --  you have a 3D printer ???

John, well, I'll try SB  ;)  need something to write some "glue code" anyway  -- if I can set up a process pipe with Lisp, things would be a lot easier here.
SDL, well,   the blitting seems slow (both in system and hardware memory - but the package seems great for games etc...  ) , I've installed it and will use it if suitable.
As for the widgets, (I don't need much), I think I'll write something myself (for CL none of the GUI builders works under Win  -- CLIM, McCLIM , and a heap of others are based on Linux ) remaining Java , GTK , Tk  ...  (and Windows itself , I'll try this - but I don't know anything about it )

to compare (attached) I translated one of the Oxygen samples (using SDL) into CL / Japi --  Clozure Common Lisp.
(the only thing changed in the O² was setting the fps at maximum -  for the lisp code I added varying size simulating star twinkeling  8)

-- considering the fact that I think Clozure CL runs (at best) at half speed of oxygen, the graphics are not bad ....
best Rob


     

.

JRS

  • Guest
The SB Challenge
« Reply #31 on: December 17, 2014, 09:34:28 AM »
Quote
John, well, I'll try SB  ;)  need something to write some "glue code" anyway  -- if I can set up a process pipe with Lisp, things would be a lot easier here.

TinyScheme is an extension module for Script BASIC. All output from TS is returned to SB in a string. Very BASIC interface. SB has excellent dynamic string/array processing abilities to extract the info you need to feed SDL_gfx and IUP.


RobbeK

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #32 on: December 17, 2014, 12:33:17 PM »
Hi John,

Strings is what i need and even CL should be able to intercept the stdout from a parallel running process, let's see I'm able.
Anyway a scripting language should be the best ,   one has to control them all, i think it's better with a script.

As for the stars, it always can better -- tuned it a little in the way the more far away objects are displayed smaller.  Because of the variadic nature changing the code takes no time.
This time written in the (underestimated) Austin Kyoto CL.  They call it GNU CL nowadays, but  ... 

 (wikipedia) KCL is notable in that it was implemented from scratch, outside of the standard committee, solely on the basis of the specification. It was one of the first Common Lisp implementations ever, and exposed a number of holes and mistakes in the specification that had gone unnoticed.

I think the late Mr Schelter spend the rest of his life correcting it.  , from there Austin Kyoto.

Japi on board/incorporated as a static lib here, poor FFI (I think, or lack of documentation)    ...  Tk only works under Linux , Japi does not under this OS.
(mm, still on line : https://www.ma.utexas.edu/users/wfs/ )

example attached -- Japi incorporated , compacter code than calling FF's.

best, Rob
(typeless code , declarations could speed it up a little more)

oops, yes : to abort the running code - hit    "start/break" first - the windowlistener is outside the calculation loop.
 

.
« Last Edit: December 17, 2014, 12:55:43 PM by RobbeK »

JRS

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #33 on: December 17, 2014, 12:52:35 PM »
I will be happy to assist you in integrating a CL with SB. I have no interest in creating another LISP/SCHEME ext. module for SB.


RobbeK

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #34 on: December 18, 2014, 02:17:18 PM »
Thanks Johh,

I'll start studying it this weekend (but it comes with a lot of documentation  :) )-- does  SB have Timer Events (i will need several tasks running at the same time)

best Rob

RobbeK

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #35 on: December 30, 2014, 05:28:57 AM »
Started to study these images based on orbit traps , they include a lot more calculations than the classic fractal calculations.

I do think more interesting possibilities give orbit traps on Möbius Transformations  (next to do).

Wrote something in Scheme (Bigloo , but to gain speed - not typeless nor variadic numeric operator modes )

Attached -- used OrbitTrap z=(0,0) (just the origin)  -- you may tune the intensity of the colours by changing the value on the right.  The green/yellow point generated are the end points of the orbits , the red points a colour generated by the lowest value of their distance to the trap (in this case it is just abs(z) where z is complex  sqrt(x²+y²) for real numbers -- i skipped the sqrt for speed ....

best Rob

Bigloo (from their site )                           Bigloo is a Scheme implementation devoted to one goal: enabling Scheme based programming style where C(++) is usually required. Bigloo attempts to make Scheme practical by offering features usually presented by traditional programming languages but not offered by Scheme and functional programming. Bigloo compiles Scheme modules. It delivers small and fast stand alone binary executables. Bigloo enables full connections between Scheme and C programs, between Scheme and Java programs, and between Scheme and C# programs.

.
« Last Edit: December 30, 2014, 07:01:05 AM by RobbeK »

Mike Lobanovsky

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #36 on: January 01, 2015, 05:49:46 PM »
Thanks Rob,

Your fractal looks very nice.

You can also get a really nice looking Buddhabrot in less than 7 seconds using FBSL's BASIC+some DynC. If you're interested in the source code, you can find it here. A precompiled exe is attached below.

I'm perfectly sure the same can be written in pure Oxygen if only with a little more effort spent on creating the main window and maintaining it responsive in the tight calc loop. But I'm a little tired right now to accept the challenge. :)

.

Aurel

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #37 on: January 01, 2015, 06:22:22 PM »
In FBSL buhabrot (oups..budhabrot)  ;D  is created very fast... ;)
infact it is created in cca 12 seconds but what computer do in other cca 30 seconds i
don't know.. ::)

.

Mike Lobanovsky

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #38 on: January 01, 2015, 06:43:07 PM »
It augments the picture by adding more pixels and adjusting their relative brightness. If your computer is too slow by modern standards, which it undoubtedly is, you may prefer to reduce

Dim stop_after = 2500000, draw_every = 50000

to

Dim stop_after = 300000, draw_every = 10000

You'll get a much faster but also much cruder image of the same fractal:

.

JRS

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #39 on: January 01, 2015, 09:28:34 PM »
Thanks Johh,

I'll start studying it this weekend (but it comes with a lot of documentation  :) )-- does  SB have Timer Events (i will need several tasks running at the same time)

best Rob

I think our goals are too far apart for what you're looking for. All I was after was a TinyScheme set of functions that SB would call and take the output to SDL_gfx. If you need much more that that, I would look elsewhere.

Thanks for the response and interest.


Mike Lobanovsky

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #40 on: January 02, 2015, 03:22:46 AM »
Both ScriptBASIC and TinyScheme are capable of multi-threading, which means you can run several threads in/of each of them at the same time, either co-operatively or independently. This will however be reasonable only if your CPU can benefit from this technique, i.e. if it has a multi-core architecture. Trying to run a multi-threaded app on a single core CPU can actually be detrimental to the app speed as compared to a single-threaded implementation of the same.

JRS

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #41 on: January 02, 2015, 10:28:23 AM »
Hi Mike,

Quote
Both ScriptBASIC and TinyScheme are capable of multi-threading

You can easily run SB in multi-threaded mode with a common variables (MT) using Charles's DLLC extension module. I would have liked to see what I mentioned above accomplished first before cloning it to multiple threads.

RobbeK

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #42 on: January 03, 2015, 01:58:21 PM »
Thanks for all this info  , John & Mike :)

As for the Buddhabrot, made something that does NOT use the orbittraps , just the endvalues of the orbits.
Written in FreeBasic this time , somewhat care was needed to remove the NaNs, INF's etc ...  (the values of the orbits quickly go into over- and underflow values).
Animated, code shows 94 sequential frames.

best, Rob
(need some better colours, but it's already something ..)   -- this would be nicer in 3D though (imho)

.
« Last Edit: January 03, 2015, 02:11:04 PM by RobbeK »

JRS

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #43 on: January 03, 2015, 03:28:45 PM »
Thanks Rob for the example. My first attempt will be to convert your FreeBASIC code to SB using JAPI for the GUI. If all goes well I'll substitute JAPI for SDL_gfx.

Here is an old SB/JAPI example to show you that I already have a running start.

Code: Script BASIC
  1. rem Example mandel.bas
  2.  
  3. import japi.bas
  4.  
  5. xstart = -1.8
  6. xend   =  0.8
  7. ystart = -1.0
  8. yend   =  1.0
  9.  
  10. hoehe  = 240
  11. breite = 320
  12.  
  13. if(j_start() = J_FALSE) then
  14.     print("Fehler beim Starten")
  15.     end
  16. endif
  17.  
  18. jframe  = j_frame("")
  19. menubar = j_menubar(jframe)
  20. jfile    = j_menu(menubar,"File")
  21. calc    = j_menu(menubar,"Calc")
  22. quit    = j_menuitem(jfile,"Quit")
  23. start   = j_menuitem(calc,"Start")
  24. jstop    = j_menuitem(calc,"Stop")
  25.  
  26. canvas  = j_canvas(jframe,breite,hoehe)
  27. j_setpos(canvas,10,60)
  28. j_pack(jframe)
  29. j_show(jframe)
  30.  
  31. obj = 0
  32. do_work = 0
  33.  
  34. while((obj <> jframe) and (obj <> quit))
  35.  
  36.     if(do_work = 1) then
  37.         obj = j_getaction()
  38.     else
  39.         obj = j_nextaction()
  40.     endif      
  41.  
  42.     if(obj = start) then
  43.         x = -1
  44.         y = -1
  45.         do_work = 1
  46.     endif
  47.  
  48.     if(obj = jstop) then
  49.         do_work = 0
  50.     endif
  51.    
  52.     if(do_work = 1) then
  53.         x = (x+1) % breite
  54.        if(x = 0) then
  55.             y = (y+1) % hoehe
  56.        endif
  57.        if((x = breite-1) and (y = hoehe-1)) then
  58.             do_work = 0
  59.         else
  60.             zre = xstart + x*(xend-xstart)/breite
  61.             zim = ystart + y*(yend-ystart)/hoehe
  62.             it = mandel(zre,zim,512)
  63.             j_setcolor(canvas,it*11,it*13,it*17)
  64.             j_drawpixel(canvas,x,y)
  65.         endif
  66.     endif
  67.  
  68. wend
  69.  
  70. j_quit()
  71.  
  72.  
  73. function mandel(zre,zim,maxiter)
  74.     mx = 0.0
  75.     my = 0.0
  76.     iter=0
  77.     betrag=0.0
  78.  
  79.     while ((iter < maxiter) and (betrag < 4.0))
  80.         iter = iter+1
  81.         tmp = mx*mx-my*my+zre
  82.         my = 2*mx*my+zim
  83.         mx = tmp
  84.         betrag = (mx*mx + my*my)
  85.     wend
  86.     mandel=iter
  87. end function
  88.  

FYI I don't think the SB JAPI interface has the same release / expanding memory issue. (at least under Linux) In the process monitor the Mandelbrot is running in one core and there is no distinguishable memory creep or retention. Java JRE remains in memory as a process but that gets loaded at boot time I believe.


.
« Last Edit: January 03, 2015, 07:04:23 PM by John »

Mike Lobanovsky

  • Guest
Re: The buddhabrot (challenging ?)
« Reply #44 on: January 03, 2015, 07:33:50 PM »
Dear Rob,

Your fractal is beautiful and your coloration attempt is not really that bad. I enjoyed it to the extent of porting it to FBSL to see it at a much better speed and without memory access violations which FreeBASIC cannot sense while FBSL can.

First, a few observations as to your FB code:
  • You shouldn't use FB functions, e.g. such as Abs(), in a tight loop. You'll be much better off unrolling these to explicit If p < -1000.0 OrElse p > 1000.0 Then. FB's (and FBSL's) AndAlso/OrElse introduce C-style lazy evaluation and work much faster than the corresponding Abs() calls.
  • Your
    px = Int(266 * (p + 1.5))
    py = Int(266 * (q + 2.0))
    pxy = px + sz * py

    yields a hellofalot of pxy indices that fall far beyond the values permissible to index the rd(), gr(), and bl() arrays. These should be explicitly filtered out in an If pxy < 640000 Then block.
  • Your pxy above had better be Dimmed As Integer rather than Single since it is a sum of integer values used as nothing more than an array index.

In my humble opinion, you should abandon these Java exercises and concentrate on a decent language that would provide you with sufficient speed, easy GUI functionality, and direct memory access on an all-in-one basis, such as FBSL or OxygenBasic.

Here is your code re-written in a tiny FBSL BASIC GUI+DynC calc script that runs 12 times faster than your FB+Java original. On my PC, your 94-frame sequence runs 24 seconds while my FBSL equivalent requires only about 2. The corresponding precompiled executable is attached below for your enjoyment.

Code: OxygenBasic
  1. #Option Strict
  2. #Include <Include\Windows.inc>
  3.  
  4. Type BMPBLOB Align 2 // fake BITMAPINFOHEADER
  5.   %biSize
  6.   %biWidth
  7.   %biHeight
  8.   %biPlanes * 16
  9.   %biBitCount * 16
  10.   $biUnused * 24
  11. End Type
  12.  
  13. Type FRACTAL
  14.   !!x
  15.   !!y
  16.   !!rc
  17.   !!ic
  18. End Type
  19.  
  20. Dim sz = 800
  21. Dim matrix[sz * sz] As FRACTAL
  22. Dim %rd[sz * sz], %gr[sz * sz], %bl[sz * sz]
  23.  
  24. Dim bb As BMPBLOB
  25. With bb
  26.   .biSize = LenB(BMPBLOB)
  27.   .biWidth = sz
  28.   .biHeight = sz
  29.   .biPlanes = 1
  30.   .biBitCount = 32
  31. End With
  32.  
  33. Dim button = FbslControl("button", ME, "Run", 1001, 400, 833, 100, 24, WS_CHILD BOr WS_VISIBLE, 0)
  34. Dim canvas = FbslControl("SysAnimate32", ME, "", 1002, 50, 20, sz, sz, WS_CHILD BOr WS_VISIBLE BOr WS_BORDER)
  35.  
  36. StyleRemove(ME, WS_MAXIMIZEBOX BOr WS_MINIMIZEBOX BOr WS_THICKFRAME)
  37. FbslSetText(ME, "Click 'Run' to start...")
  38. Resize(ME, 0, 0, 900, 900): Center(ME)
  39.  
  40. Dim %pixels, hBmp = CreateDIBSection(NULL, @bb, 0, @pixels, NULL, 0)
  41. Dim hDC = GetDC(canvas), memDC = CreateCompatibleDC(NULL)
  42. Dim hOldBmp = SelectObject(memDC, hBmp)
  43.  
  44. Show(ME)
  45. DoEvents
  46.  
  47. init_matrix()
  48.  
  49. Begin Events
  50.   If CBMSG = WM_COMMAND Then
  51.     If CBLPARAM = button Then
  52.       DrawBrot()
  53.       Return 0
  54.     End If
  55.   ElseIf CBMSG = WM_PAINT Then
  56.     BitBlt(hDC, 0, 0, sz, sz, memDC, 0, 0, SRCCOPY)
  57.   ElseIf CBMSG = WM_CLOSE Then
  58.     DeleteObject(SelectObject(memDC, hOldBmp))
  59.     DeleteDC(memDC)
  60.     ReleaseDC(canvas, hDC)
  61.   ElseIf CBMSG = WM_DESTROY Then
  62.     ExitProcess(0)
  63.   End If
  64. End Events
  65.  
  66. Sub DrawBrot()
  67.   RtlZeroMemory(@gr[0], LenB(gr)) // clear green array
  68.  
  69.   Dim start_time = GetTickCount()
  70.   For Dim k = 1 To 94
  71.     update_matrix(@matrix[0], sz)
  72.     SelectObject(memDC, hOldBmp)
  73.     plot_matrix(@matrix[0], @rd[0], @gr[0], @bl[0], pixels, sz)
  74.     SelectObject(memDC, hBmp)
  75.     BitBlt(hDC, 0, 0, sz, sz, memDC, 0, 0, SRCCOPY)
  76.     FbslSetText(ME, (GetTickCount() - start_time) / 1000 & " sec, frame " & k & "/94")
  77.     DoEvents
  78.   Next
  79. End Sub
  80.  
  81. Sub init_matrix()
  82.   Dim ix, y, x = -2.0, dz = 3 / 800
  83.  
  84.   For Dim i = 0 To sz - 1
  85.     x = x + dz: y = -1.5
  86.     For Dim j = 0 To sz - 1
  87.       y = y + dz: ix = i + j * sz
  88.       With matrix[ix]
  89.         Let(.x, .rc) = x
  90.         Let(.y, .ic) = y
  91.       End With
  92.     Next
  93.   Next
  94. End Sub
  95.  
  96. DynC update_matrix(%mtx_ptr, %sz)
  97.   typedef struct {
  98.     double x, y, rc, ic;
  99.   } FRACTAL, *LPFRACTAL;
  100.  
  101.   void main(FRACTAL matrix[], int sz)
  102.   {
  103.     int i;
  104.     double p;
  105.     LPFRACTAL f;
  106.    
  107.     for (i = 0; i < sz * sz; i++) {
  108.       f = &matrix[i];
  109.       p = f->x;
  110.       f->x = p * p - f->y * f->y + f->rc;
  111.       f->y = 2 * p * f->y + f->ic;
  112.     }
  113.   }
  114. End DynC
  115.  
  116. DynC plot_matrix(%mtx_ptr, %r_ptr, %g_ptr, %b_ptr, %pxs_ptr, %sz)
  117.   typedef struct {
  118.     double x, y, rc, ic;
  119.   } FRACTAL, *LPFRACTAL;
  120.  
  121.   void main(FRACTAL matrix[], int rd[], int gr[], int bl[], int* pixels, int sz)
  122.   {
  123.     int i, px, py, pxy;
  124.     double p, q;
  125.     LPFRACTAL f;
  126.    
  127.     for (i = 0; i < sz * sz; i++) {
  128.       f = &matrix[i];
  129.       p = f->y;
  130.       q = f->x;
  131.       if (p > 1000.0 || p < -1000.0 || q > 1000.0 || q < -1000.0) {
  132.         memset(f, 0, sizeof(FRACTAL));
  133.       }
  134.      
  135.       if (p > -2.0) {
  136.         if (p < 1.0) {
  137.           if (q < 1.5 && q > -1.5) {
  138.             px = 266 * (p + 1.5);
  139.             py = 266 * (q + 2.0);
  140.             pxy = px + sz * py;
  141.             if (pxy < 640000) {
  142.               rd[pxy] += 3;
  143.               gr[pxy] += 3;
  144.               bl[pxy] += 1;
  145.             }
  146.           }
  147.         }
  148.       }
  149.     }
  150.    
  151.     for (px = sz - 1; px >= 0; px--) {
  152.       for (py = 0; py < sz; py++) {
  153.         pxy = px * sz + py;
  154.         *pixels++ = bl[pxy] | (gr[pxy] << 8) | (rd[pxy] << 16);
  155.       }
  156.     }
  157.   }
  158. End DynC

The most beautiful coloration is achieved if the initial sequence is interrupted with another click on the Run button. Enjoy this smiling Buddha I was lucky to capture; note also the small skull right in the middle of his fat divine belly:  :)


.
« Last Edit: January 03, 2015, 08:46:41 PM by Mike Lobanovsky »