#include <Array.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Timers.au3>
Global $n = 50
Global $components = $n * $n
Global $id[$components]
Global $cells[$n][$n]
Global $Graphics[$n][$n]
Global $tsample = 10000000
Global $thr = 0.592746
Global $time = 0
Global $akt_thr = 0
$fac = 5
$Form = GUICreate("Simulation {" & $n * $n & "}", $n * $fac, $n * $fac + 50, 100, 100)
$Label_Comp = GUICtrlCreateLabel("Components: " & $components, 10, $n * $fac + 10, 200, 17)
$Label_CT = GUICtrlCreateLabel("Cur. Threshold: 0", 10, $n * $fac + 30, 200, 17)
$Label_T = GUICtrlCreateLabel("Time converge: ", 120, $n * $fac + 10, 200, 17)
GUICtrlSetColor(-1, 0x000080)
GUISetState(@SW_SHOW)
_GDIPlus_Startup()
$hBrushBlue = _GDIPlus_BrushCreateSolid(0xFFFF0000)
$hBrushB = _GDIPlus_BrushCreateSolid(0xFF000000)
For $i = 0 To $n - 1
For $j = 0 To $n - 1
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($Form)
_GDIPlus_GraphicsFillRect($hGraphic, $i * $fac, $j * $fac, $fac, $fac, $hBrushBlue)
Next
Next
For $i = 0 To $components - 1
$id[$i] = $i
Next
For $k = 0 To $n - 2
unite(cell($k, 0), cell($k + 1, 0))
unite(cell($k, $n - 1), cell($k + 1, $n - 1))
Next
For $k = 0 To $n - 1
$cells[$k][0] = True
$cells[$k][$n - 1] = True
Next
$t = _simulate()
ConsoleWrite("treshold: " & (1 * $t / ($n * $n)) & " - t: " & $t & " - time: " & $time & " - akt_thr: " & $akt_thr & @CRLF)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()
Exit
EndSwitch
WEnd
Func _simulate()
$t = 0
While True
Local $stt = _Timer_Init()
If _root(cell(0, 0), cell($n - 1, $n - 1)) Then Return $t
_step()
_show()
$t += 1
Local $tsample = _Timer_Diff($stt)
$time += $tsample
$akt_thr = ($thr - ($t / ($n * $n))) * $tsample
GUICtrlSetData($Label_T, "Time converge: " & Round($akt_thr, 2))
GUICtrlSetData($Label_CT, "Cur. Threshold: " & ($t / ($n * $n)))
WEnd
EndFunc ;==>_simulate
Func _show()
For $r = 0 To $n - 1
For $p = 0 To $n - 1
If $cells[$r][$p] Then
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($Form)
_GDIPlus_GraphicsFillRect($hGraphic, $r * $fac, $p * $fac, $fac, $fac, $hBrushB)
EndIf
Next
Next
EndFunc ;==>_show
Func _step()
Local $m, $h
While $cells[$m][$h]
$m = Random(0, $n - 1, 1)
$h = Random(0, $n - 1, 1)
WEnd
$cells[$m][$h] = True
If $m < $n - 1 And $cells[$m + 1][$h] Then unite(cell($m, $h), cell($m + 1, $h))
If $m > 0 And $cells[$m - 1][$h] Then unite(cell($m, $h), cell($m - 1, $h))
If $h < $n - 1 And $cells[$m][$h + 1] Then unite(cell($m, $h), cell($m, $h + 1))
If $h > 0 And $cells[$m][$h - 1] Then unite(cell($m, $h), cell($m, $h - 1))
EndFunc ;==>_step
Func cell($i, $j)
Return $i + $j * $n
EndFunc ;==>cell
Func _find($x)
While $x <> $id[$x]
$id[$x] = $id[$id[$x]]
$x = $id[$x]
WEnd
Return $x
EndFunc ;==>_find
Func _root($p, $q)
Return _find($p) == _find($q)
EndFunc ;==>_root
Func unite($p, $q)
$i = _find($p)
$j = _find($q)
If $i == $j Then Return
$id[$i] = $j
$components -= 1
GUICtrlSetData($Label_Comp, "Components: " & $components)
EndFunc ;==>unite