; Hüpfer Fraktal Generator
; minx 2012
; GDI

; Du kannst die Fraktalwerte a,b,c dargestellt durch den Array "$Frac"
; beliebig auswählen. Der Divider gibt die Größendivision an, je kleiner
; desto näher ist das Bild.

#include <Misc.au3>
#include <Array.au3>
#include <WinAPI.au3>

;-------- Begin changes ---------------------------------------------------------------------------------
Dim $Frac[4] = [Random(-100, 100, 1), Random(-10.100, 0.100, 1), Random(-100, 100, 1), Random(0, 500000, 1)]
Local $Divider = 2
;-------- Stop chaning here -----------------------------------------------------------------------------

Global Const $BLACKNESS = 0x00000042

$Form1 = GUICreate("Fractal: Hopalong (Hüpfer) | a=" & $Frac[0] & ", b=" & $Frac[1] & ", c=" & $Frac[2] & ", num=" & $Frac[3], 1000, 600, -1, -1)
GUISetState(@SW_SHOW)
GUISetBkColor(0)

$user32 = DllOpen("user32.dll")
$gdi32 = DllOpen("gdi32.dll")

$dc = GetDC($Form1)
Draw($Frac[0], $Frac[1], $Frac[2], $Frac[3], $Divider)

Do

    If _IsPressed("52") Then

        While _IsPressed("52") And Sleep(10)
        WEnd

        DllCall($gdi32, "bool", "BitBlt", "handle", $dc, "int", 0, "int", 0, "int", 1000, "int", 600, "handle", 0, "int", 0, "int", 0, "dword", $BLACKNESS)
        WinSetTitle($Form1, '', "Fractal: Hopalong (Hüpfer) | a=" & $Frac[0] & ", b=" & $Frac[1] & ", c=" & $Frac[2] & ", num=" & $Frac[3])

        Dim $Frac[4] = [Random(-100, 100, 1), Random(-10.100, 0.100, 1), Random(-100, 100, 1), Random(0, 500000, 1)]
        Draw($Frac[0], $Frac[1], $Frac[2], $Frac[3], $Divider)

    ElseIf _IsPressed("53") Then

        While _IsPressed("53") And Sleep(10)
        WEnd

        ClipPut(_ArrayToString($Frac, ", "))
        MsgBox(0, "", "Put to Clipboard")

    EndIf

Until GUIGetMsg() = -3

_WinAPI_ReleaseDC($Form1, $dc)
DllClose($gdi32)
DllClose($user32)

Func pix($dc, $x, $y, $color)
    DllCall($gdi32, "long", "SetPixel", "long", $dc, "long", $x, "long", $y, "long", $color)
EndFunc   ;==>pix

Func GetDC($handle)
    $aRet = DllCall($user32, "int", "GetDC", "hwnd", $handle)
    Return $aRet[0]
EndFunc   ;==>GetDC

Func Draw($a = 10, $b = 10, $c = 10, $num = 100000, $Divider = 2)
    Local $iTimer = TimerInit()
    $j = $a
    $k = $b
    $x = 0
    $y = 0
    $xx = 0
    $xp = 0
    $yp = 0
    $r = 0
    $i = 0
    $xoffs = 1000
    $yoffs = 600
    $color = 0xAA
    For $i = 1 To $num
        $color = 0xAA * $i
        $xx = $y - (($x > 0) - ($x < 0)) * Sqrt(Abs($k * $x - $c))
        $y = $j - $x
        $x = $xx
        $xp = $x * $Divider + $xoffs
        $yp = $y * $Divider + $yoffs
        pix($dc, $xp / $Divider, $yp / $Divider, $color)
    Next
    ConsoleWrite('Zeit pro 10k Px: ' & Round(TimerDiff($iTimer) / $Frac[3] * 10000, 2) & @CRLF)
EndFunc   ;==>Draw