Jump to content

Mandelbrot fractal with AutoIt


lokster
 Share

Recommended Posts

Well... if someone is interested in fractals :)

Here is simple example of how to draw Mandelbrot fractal with AutoIt.

ScreenShot:

Posted Image

And here is the code:

; example of how to generate and display Mandelbrot fractal
; by lokster

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)
$Form1 = GUICreate("lokster's Mandelbrot Set example", 201, 201, -1, -1)
GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE,"Bye")

$user32 = DllOpen("user32.dll")
$gdi32 = DllOpen("gdi32.dll")

MsgBox(0,"Info","This program demosntrates how to draw Mandelbrot Fractal in AutoIt. It is _SLOW_, so please be patient")
$start = TimerInit()
Mandelbrot()
MsgBox(0,"Time taken:",TimerDiff($start)/1000 &" seconds")

While 1
    Sleep(100)
WEnd

Func Bye()
    Exit
EndFunc

Func pix($dc,$x,$y,$color)
    DllCall ($gdi32, "long", "SetPixel", "long", $dc, "long", $x, "long", $y, "long", $color)
EndFunc

Func GetDC($handle)
    $dc = DllCall ($user32, "int", "GetDC", "hwnd", $handle)
    Return $dc[0]
EndFunc

Func Mandelbrot()
    $dc = GetDC(WinGetHandle($Form1))
    
    $width = 200
    $height = 200
    $maxiteration = 10

    for $py = 0 to $height step 1
        for $px = 0 to $width Step 1
            $x0 = (4/$width)*$px-2
            $y0 = (4/$height)*$py-2
            $x = $x0
            $y = $y0
            $iteration = 0
            
            While (($x*$x + $y*$y) < 4  AND  $iteration < $maxiteration)
                $xtemp = $x*$x - $y*$y + $x0
                $y = 2*$x*$y + $y0
                $x = $xtemp
                $iteration+= 1              
            WEnd
            pix($dc,$px,$py,($iteration/$maxiteration)*0xFF) ; I did't had time to implement some fancy colouring
        Next
    Next
EndFunc
Link to comment
Share on other sites

  • 1 year later...

Hello Lokster, Sorry to revive this old thread, but I made something like this recently and I wanted to share it, although I didn't think it deserved it's own thread.

I'm not sure how it compares but it's a modified translation of the code shown here.

My screenshot is attached.

My code is below, and yes I know it's a bit slow on large images.

EDIT: Now using SetPixel as in Lokster's script. - Added some colors to middle just for fun.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$ImageHeight=250
$ImageWidth=$ImageHeight
$Form1 = GUICreate("Mandelbrot", $ImageWidth+2, $ImageHeight+2, 0, 0)
GUISetOnEvent(-3,'Quit')
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$user32 = DllOpen("user32.dll")
$gdi32 = DllOpen("gdi32.dll")
 $dc = GetDC(WinGetHandle($Form1))
#Region ### START Mandelbrot ###


$MinRe = -2.0;
$MaxRe = 1.0;
$MinIm = -1.2;
$MaxIm = $MinIm+($MaxRe-$MinRe)*$ImageHeight/$ImageWidth;
$Re_factor = ($MaxRe-$MinRe)/($ImageWidth-1);
$Im_factor = ($MaxIm-$MinIm)/($ImageHeight-1);
Global $MaxIterations = 30;
$Steps=1
Mandelb()


Func Mandelb()

    
    
    
for $y=0 To $ImageHeight-1 Step $Steps
    $c_im = $MaxIm - $y*$Im_factor;
    for $x=0 To $ImageWidth-1 Step $Steps
        $c_re = $MinRe + $x*$Re_factor;
        $Z_re = $c_re
        $Z_im = $c_im;
        $isInside = true;
        For $n=0 to $MaxIterations-1
            $Z_re2 = $Z_re*$Z_re
            $Z_im2 = $Z_im*$Z_im;
            if ($Z_re2 + $Z_im2) > 4 Then
                $isInside = false;
                ExitLoop
            EndIf
            $Z_im = 2*$Z_re*$Z_im + $c_im;
            $Z_re = $Z_re2 - $Z_im2 + $c_re;
        Next
        pix($dc,$x,$y,Mandelb_colorfix($x,$y,$n,$isInside))
    Next
Next
EndFunc
#EndRegion ### END Mandelbrot ###
While 1
    Sleep(100)
WEnd


Func Mandelb_colorfix($x,$y,$c,$inside)
    Global $MaxIterations
    $r=Int(_ConvertRange($c, 0, $MaxIterations-1, 0, 0xFF))
    $g=Int(_ConvertRange($x, 0, $ImageWidth, 0, 0xFF))
    $b=Int(_ConvertRange($y, 0, $ImageHeight, 0, 0xFF))
    ;ConsoleWrite('0| - '&$y&' - |'&$ImageHeight&' == '&$b&@LF)
    Local $color=$b+0x100*($g+0x100*$r)
    If $inside Then $color=Sqrt($x*$y)*0x010101
    Return $color
EndFunc

Func _ConvertRange($i, $n, $x, $nb, $xb)
    ;converts a number in a range of numbers
    ;proportionally to number in another range
    ;despite whether the numbers are negative or not
    ;based upon $P=100*(($i-$n)/($x-$n));
    $C = ((($i - $n) / ($x - $n)) * ($xb - $nb)) + $nb
    Return $C
EndFunc   ;==>_ConvertRange
Func Quit()
    Exit
EndFunc

Func pix($dc,$x,$y,$color)
    DllCall ($gdi32, "long", "SetPixel", "long", $dc, "long", $x, "long", $y, "long", $color)
EndFunc

Func GetDC($handle)
    $dc = DllCall ($user32, "int", "GetDC", "hwnd", $handle)
    Return $dc[0]
EndFunc

Au3.2.12.1

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Link to comment
Share on other sites

Yep, very very slow... try to increase it's speed.

Cw2k

Smaller pictures render faster, lower the ImageHeight.

As for the rest, sure - just give me a radical rewrite of the Mandelbrot function :mellow:

I mean, it takes as long as it takes - unless it's faster to build up an array of pixels and try to draw them after all the actual calculations... but that requires more initial wait for more output.

Edit: Sorry if your reply was directed at Lokster due to me reopening this subject, *shrugs* but mine is adequately slow by itself.

EDIT: Edited my post - See new script, replaced the graphics control with SetPixel as seen in Lokster's code, which is a little faster. (I think?)

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Link to comment
Share on other sites

  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...