Page 1 of 2

MandelBrot Fractal in HMG :)

Posted: Wed Oct 31, 2018 1:14 pm
by Rathinagiri
Just these 88 lines of code creates a wonderful MandelBrot fractal. Enjoy!

Code: Select all

#include <hmg.ch>

Function Main
   public lStop := .f.
   define window main at 0, 0 width 1000 height 800 title 'MandelBrot' main
      define button b1
         row 10
         col 10
         width 80
         caption "Draw"
         action startcalc()
      end button
      define button stop
         row 10
         col 110
         caption 'Stop'
         action lStop := .t.
      end button 
   end window
   Main.Center
   Main.Activate

Return

function startcalc
   local nWidth := 800
   local nHeight := 600
   local hDC, BTstruct
   local aColors := { ;
   {66, 30, 15},;
    {25, 7, 26},;
    {9, 1, 47},;
    {4, 4, 73},;
    {0, 7, 100},;
    {12, 44, 138},;
    {24, 82, 177},;
    {57, 125, 209},;
    {134, 181, 229},;
    {211, 236, 248},;
    {241, 233, 191},;
    {248, 201, 95},;
    {255, 170, 0},;
    {204, 128, 0},;
    {153, 87, 0},;
    {106, 52, 3};
   }
   BT_ClientAreaInvalidateAll ("main")
   hDC := BT_CreateDC ("main", BT_HDC_INVALIDCLIENTAREA, @BTstruct)
   BT_DrawFillRectangle (hDC, 40, 40, nWidth, nHeight, { 255, 255, 255 }, { 0, 0, 0 }, 1 )
   cx := 0.05
   cy := 0.05
   scale := 0.005
   limit := 40
   for x := -400 to ( nWidth / 2 )
      for y:= -300 to ( nHeight / 2 )
         ax := cx + ( x * scale )
         ay := cy + ( y * scale )
         a1 := ax
         b1 := ay
         lp := 0 
         for lp = 1 to 255
            a2 := ( a1 * a1 ) -  ( b1 * b1 ) + ax 
            b2 := 2*a1*b1+ay
            a1 := a2
            b1 := b2
            if ( a1 * a1 ) + ( b1 * b1 ) > limit
               exit
            endif
         next lp   
         if lp < 255 
            aColor := aColors[ if( int( mod( lp, 16 ) ) > 0, int( mod( lp, 16 ) ), 1 ) ]
         else
            aColor := { 0, 0, 0 }
         endif
         nRow := 40 + 300 + y
         nCol := 40 + 400 + x
         BT_DrawSetPixel ( hDC, nRow, nCol, aColor )
         do events
         if lStop
            exit
         endif
      next y
      if lStop
         exit
      endif
   next x   
   BT_DeleteDC( BTstruct )
return nil   

Re: MandelBrot Fractal in HMG :)

Posted: Wed Oct 31, 2018 2:38 pm
by andyglezl
Fantastico Rathi !!!
Gracias.

Re: MandelBrot Fractal in HMG :)

Posted: Wed Oct 31, 2018 4:03 pm
by serge_girard
Great work!

Serge

Re: MandelBrot Fractal in HMG :)

Posted: Wed Oct 31, 2018 6:08 pm
by chrisjx2002
Beautiful!

Re: MandelBrot Fractal in HMG :)

Posted: Wed Oct 31, 2018 7:59 pm
by gfilatov
Rathinagiri wrote: ↑Wed Oct 31, 2018 1:14 pm Just these 88 lines of code creates a wonderful MandelBrot fractal. Enjoy!
Thanks a lot, Rathi :!:
My result is below

Re: MandelBrot Fractal in HMG :)

Posted: Wed Oct 31, 2018 8:18 pm
by EduardoLuis
SUPERB DEMO. Thanks Rathi for share.-
Eduardo

Re: MandelBrot Fractal in HMG :)

Posted: Thu Nov 01, 2018 12:44 am
by andyglezl
Rathi, tu eres un genio, yo no entendí nada... :oops:
*-----------------------------------------------------------
Rathi, you are a genius, I did not understand anything ... :oops:

https://en.wikipedia.org/wiki/Mandelbrot_set

Re: MandelBrot Fractal in HMG :)

Posted: Thu Nov 01, 2018 1:22 am
by luisvasquezcl
Great rathi. Beauti result
Best regards

Re: MandelBrot Fractal in HMG :)

Posted: Thu Nov 01, 2018 8:00 pm
by Steed
Congratulations,

Re: MandelBrot Fractal in HMG :)

Posted: Fri Nov 02, 2018 10:34 am
by mustafa
+1
Best regards
Mustafa