MandelBrot Fractal in HMG :)

HMG Samples and Enhancements

Moderator: Rathinagiri

User avatar
Rathinagiri
Posts: 5246
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 165 times
Been thanked: 164 times
Contact:

MandelBrot Fractal in HMG :)

Post by Rathinagiri » Wed Oct 31, 2018 1:14 pm

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   
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
andyglezl
Posts: 1030
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Has thanked: 21 times
Been thanked: 42 times
Contact:

Post by andyglezl » Wed Oct 31, 2018 2:38 pm

Fantastico Rathi !!!
Gracias.
Andrés González López
Desde Guadalajara, Jalisco. México.

User avatar
serge_girard
Posts: 2140
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 449 times
Been thanked: 102 times
Contact:

Post by serge_girard » Wed Oct 31, 2018 4:03 pm

Great work!

Serge

chrisjx2002
Posts: 165
Joined: Wed Jan 06, 2010 5:39 pm
Been thanked: 2 times

Post by chrisjx2002 » Wed Oct 31, 2018 6:08 pm

Beautiful!

User avatar
gfilatov
Posts: 573
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Has thanked: 14 times
Been thanked: 138 times
Contact:

Post by gfilatov » Wed Oct 31, 2018 7:59 pm

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
Attachments
screen.png
screen.png (97.99 KiB) Viewed 258 times
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein

EduardoLuis
Posts: 594
Joined: Tue Jun 04, 2013 6:33 pm
Location: Argentina
Has thanked: 1 time
Been thanked: 37 times

Post by EduardoLuis » Wed Oct 31, 2018 8:18 pm

SUPERB DEMO. Thanks Rathi for share.-
Eduardo

User avatar
andyglezl
Posts: 1030
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Has thanked: 21 times
Been thanked: 42 times
Contact:

Post by andyglezl » Thu Nov 01, 2018 12:44 am

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
Andrés González López
Desde Guadalajara, Jalisco. México.

User avatar
luisvasquezcl
Posts: 1115
Joined: Thu Jul 31, 2008 3:23 am
Location: Chile
Has thanked: 13 times
Been thanked: 14 times
Contact:

Post by luisvasquezcl » Thu Nov 01, 2018 1:22 am

Great rathi. Beauti result
Best regards

User avatar
Steed
Posts: 366
Joined: Sat Dec 12, 2009 3:40 pm
Has thanked: 1 time
Been thanked: 2 times

Post by Steed » Thu Nov 01, 2018 8:00 pm

Congratulations,

User avatar
mustafa
Posts: 704
Joined: Fri Mar 20, 2009 11:38 am
Location: Alicante - Spain
Been thanked: 80 times

Post by mustafa » Fri Nov 02, 2018 10:34 am

+1
Best regards
Mustafa

Post Reply