name22

Brownian Tree

Nov 26th, 2012
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WindowsConstants.au3>
  2. #include <GUIConstants.au3>
  3. #include <GDIPlus.au3>
  4. #include <WinAPI.au3>
  5.  
  6. ; -Author: name22 (www.autoit.de)
  7.  
  8. Global $hDll_GDI32 = DllOpen("gdi32.dll")
  9. Global $iX_Pos, $iY_Pos, $iX_Dir, $iY_Dir
  10.  
  11. Global $sFile = FileSaveDialog("Save Picture", "", "Bitmap (*.bmp)")
  12. If @error Then Exit
  13.  
  14. Global $iCount = 10000
  15. Global $iSize = 300
  16.  
  17. Global $iColorBGR_Seed = 0x0000FF
  18. Global $iColorBGR_Not = 0x000000
  19. Global $iColorBGR_Set = 0xFF0000
  20. Global $iColorBGR_Mov = 0x202020
  21.  
  22. Opt("GUIOnEventMode", 1)
  23.  
  24. $hWnd = GUICreate("Brownian Tree", $iSize, $iSize)
  25. GUISetState()
  26.  
  27. $hDC_Window = _WinAPI_GetDC($hWnd)
  28. $hDC_Bitmap = _WinAPI_CreateCompatibleDC($hDC_Window)
  29. $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Window, $iSize, $iSize)
  30. $hObj_Old = _WinAPI_SelectObject($hDC_Bitmap, $hBitmap)
  31.  
  32. OnAutoItExitRegister("_Shutdown")
  33. GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
  34. GUISetOnEvent($GUI_EVENT_RESTORE, "_DrawBuffer")
  35. GUIRegisterMsg($WM_PAINT, "_DrawBuffer")
  36.  
  37. _WinAPI_SetBkColor($hDC_Bitmap, $iColorBGR_Not)
  38. _SetPixel($hDC_Bitmap, Random(0, $iSize - 1, 1), Random(0, $iSize - 1, 1), $iColorBGR_Seed)
  39. _DrawBuffer()
  40.  
  41. For $i = 1 To $iCount
  42.     Do
  43.         $iX_Pos = Random(0, $iSize - 1, 1)
  44.         $iY_Pos = Random(0, $iSize - 1, 1)
  45.     Until _GetPixel($hDC_Bitmap, $iX_Pos, $iY_Pos) = $iColorBGR_Not
  46.  
  47.     While True
  48.         _SetPixel($hDC_Window, $iX_Pos, $iY_Pos, $iColorBGR_Mov)
  49.  
  50.         $iX_Dir = Random(-1, 1, 1)
  51.         $iY_Dir = Random(-1, 1, 1)
  52.         If $iX_Dir = 0 And $iY_Dir = 0 Then ContinueLoop
  53.  
  54.         If $iX_Pos + $iX_Dir < 0 Or $iX_Pos + $iX_Dir >= $iSize Or $iY_Pos + $iY_Dir < 0 Or $iY_Pos + $iY_Dir >= $iSize Then
  55.             _DrawBuffer()
  56.             $i -= 1
  57.             ExitLoop
  58.         Else
  59.             If _GetPixel($hDC_Bitmap, $iX_Pos + $iX_Dir, $iY_Pos + $iY_Dir) <> $iColorBGR_Not Then
  60.                 _SetPixel($hDC_Bitmap, $iX_Pos, $iY_Pos, $iColorBGR_Set)
  61.                 _DrawBuffer()
  62.                 ExitLoop
  63.             Else
  64.                 $iX_Pos += $iX_Dir
  65.                 $iY_Pos += $iY_Dir
  66.             EndIf
  67.         EndIf
  68.     WEnd
  69. Next
  70.  
  71. _GDIPlus_Startup()
  72. $hBitmapTmp = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
  73. If Not StringRegExp($sFile, "\.bmp\z") Then $sFile &= ".bmp"
  74. _GDIPlus_ImageSaveToFile($hBitmapTmp, $sFile)
  75. _GDIPlus_ImageDispose($hBitmapTmp)
  76. _GDIPlus_Shutdown()
  77.  
  78. While Sleep(1000)
  79. WEnd
  80.  
  81. Func _SetPixel($hDC, $iX, $iY, $iColor)
  82.     DllCall($hDll_GDI32, 'INT', 'SetPixelV', 'HWND', $hDC, 'INT', $iX, 'INT', $iY, 'DWORD', $iColor)
  83. EndFunc
  84.  
  85. Func _GetPixel($hDC, $iX, $iY)
  86.     $aRet = DllCall($hDll_GDI32, 'DWORD', 'GetPixel', 'HWND', $hDC, 'INT', $iX, 'INT', $iY)
  87.     Return $aRet[0]
  88. EndFunc
  89.  
  90. Func _DrawBuffer()
  91.     DllCall($hDll_GDI32, "BOOL", "BitBlt", "HANDLE", $hDC_Window, "INT", 0, "INT", 0, "INT", $iSize, "INT", $iSize, "HANDLE", $hDC_Bitmap, "INT", 0, "INT", 0, "DWORD", $SRCCOPY)
  92. EndFunc
  93.  
  94. Func _Shutdown()
  95.     _WinAPI_SelectObject($hDC_Bitmap, $hObj_Old)
  96.     _WinAPI_ReleaseDC($hWnd, $hDC_Window)
  97.     _WinAPI_DeleteDC($hDC_Bitmap)
  98.     _WinAPI_DeleteObject($hBitmap)
  99.  
  100.     DllClose($hDll_GDI32)
  101. EndFunc
  102.  
  103. Func _Close()
  104.     Exit
  105. EndFunc
RAW Paste Data