Auto Cellular automaton Was: Drawing Sierpinski Triangle fractal

"valery_vi" <[email protected]>
Newsgroups gmane.comp.windows.autoit.user
Message-ID <[email protected]>
Hi,

As soon as AutoIt can draw fractals, I've written other example 
ACA.au3:

; ACA, © Valery Ivanov, 20 June, 2007
;  ACA is Auto Cellular automaton example
; About CA see here:
; http://en.wikipedia.org/wiki/Cellular_automata
#include <GUIConstants.au3>
Global $hDC, $GDI, $hWnd


; Default Rule 30
;  Rule 30 cellular automaton
Global $RuleNum = 30
Global $CA_Rule[8] = [0,0,0,1,1,1,1,0]
; Rule 30 cellular automaton
; You can set Rule 110
; $RuleNum = 110
Set_CA_Rule($RuleNum)

$hWnd = GUICreate("Auto Cellular automaton - © Valery Ivanov, 20 
June, 2007", 400, 200)
GUISetState(@SW_SHOW)

$GDI = DllOpen("gdi32.dll")
$hDC = DllCall ("user32.dll", "int", "GetDC", "hwnd", $hWnd)
$hDC = $hDC[0]

$String = GetFirstLine(400)
$s = StringSplit($String,'')
$i = 0
for $i = 1 To 200
 for $j = 1 To $s[0]
  if $s[$j] = '1' Then 
   PutPoint($hDC,$j,$i,0xAA0000)
  else
   PutPoint($hDC,$j,$i,0xFFFFFF)
  endif
 next
 $String = GetNextLine($String)
 $s = StringSplit($String,'')
Next

; Loop until user exits
do
until GUIGetMsg() = $GUI_EVENT_CLOSE

;===============
func PutPoint($hDC, $x, $y, $color)
 DllCall ($GDI, "long", "SetPixel", "long", $hDC, "long", $x, "long", 
$y, "long", $color)
endfunc

;===============
;set Rule 
; There are 256 rules [0 - 255]
; The known CA rule are 30 and 110!
func Set_CA_Rule($RuleNum)
 for $i = 1 to 8
  $CA_Rule[8-$i] = 0
  if mod($RuleNum,2) then $CA_Rule[8-$i] = 1
  $RuleNum = BitShift($RuleNum,1)
 next  
endfunc

;===============
;view Rule 
func View_CA_Rule()
 $l = ''
 for $i = 0 to 7
  $l &= $CA_Rule[$i]
  if $i <> 7 then $l &= ', '
 next
 Msgbox(0,'',$l)
endfunc

;===============
;view CA
func View_CA()
 $s = GetFirstLine(20)
 $l = $s
 for $i = 0 to 3
  $s = GetNextLine($s)
  $l &= @CrLf & $s
 next
 Msgbox(0,'',$l)
endfunc

;===============
;get first CA line
func GetFirstLine($Dim)
local $String = ''
for $i = 1 to  $Dim
 if $i = $Dim/2 then 
  $String &= '1'
 else
  $String &= '0'
 endif
next
return $String
endfunc

;===============
;get next CA line
func GetNextLine($String)
local $S, $N, $l, $c, $r
 $S = StringSplit($String,'')
 $String = ''
 $N = $S[0]
 for $i = 1 to $N
  $l = $S[$N]
  if $i <> 1 then $l = $S[$i-1]
  $c = $S[$i]
  $r = $S[1]
  if $i <> $n then $r = $S[$i+1]
  $String &= $CA_Rule[7-($r+2*$c+4*$l)]
 next
 return $String
endfunc

This script shows how to draw the simplest nontrivial CA. 
Current code build Rule 30 but you can update it to draw any Rule 
examples with help of special variable $RuleNum and calling of 
function Set_CA_Rule($RuleNum). 
for example

  $RuleNum = 110
  Set_CA_Rule($RuleNum)

BTW About CA read the following:

http://en.wikipedia.org/wiki/Cellular_automata

Enjoy,
Valery

--- In [email protected], "valery_vi" <shehrer1@...> wrote:
>
> Maybe it will be more fine (with alpha colored hBrushes):
> 
> #include <A3LGDIPlus.au3>
> Global $hGUI, $hWnd, $hGraphic
> 
> ;basis (width of the triangle)
> Global $d  = 512
> ;distance from the left
> Global $x0 = 50
> ;distance from the top
> Global $y0 = 50
> ;height
> Global $h  = $d*sqrt(3)/2
> ;limit to recursion in pixels
> Global $dMin=2
> Global $hBrushRed, $hBrushGreen, $hBrushBlue
> 
> dim $x[3] = [ $x0, $x0+$d, $x0+$d/2 ]
> dim $y[3] = [ $y0+$h, $y0+$h, $y0 ]
> 
> $hGUI = GUICreate("Sierpinski", 600, 600)
> $hWnd = WinGetHandle("Sierpinski")
> 
> _GDIP_Startup()
> $hBrushRed = _GDIP_BrushCreateSolid(255,80,80,200)
> $hBrushGreen = _GDIP_BrushCreateSolid(80,255,80,200)
> $hBrushBlue = _GDIP_BrushCreateSolid(80,80,255,200)
> GUISetState()
> $hGraphic = _GDIP_GraphicsCreateFromHWND($hWnd)
> drawSierpinskiTriangle($x,$y,$d/2, $hBrushRed)
> 
> ; Loop until user exits
> do
> until GUIGetMsg() = $GUI_EVENT_CLOSE
> 
> ; Clean up resources
> _GDIP_BrushDispose($hBrushRed)
> _GDIP_BrushDispose($hBrushGreen)
> _GDIP_BrushDispose($hBrushBlue)
> _GDIP_GraphicsDispose($hGraphic)
> _GDIP_Shutdown()
> 
> ;==================================
> ; recursive draw
> func drawSierpinskiTriangle ($x, $y, $d, $hBrush)
> if $d <= $dMin then 
>   fillTriangle($x,$y, $hBrush)
> else
> ;centers of the sides:
> local $xMc = ($x[0]+$x[1])/2
> local $yMc = ($y[0]+$y[1])/2
> local $xMb = ($x[0]+$x[2])/2
> local $yMb = ($y[0]+$y[2])/2
> local $xMa = ($x[1]+$x[2])/2
> local $yMa = ($y[1]+$y[2])/2
> 
> dim $x1[3] = [ $x[0], $xMc, $xMb ]
> dim $y1[3] = [ $y[0], $yMc, $yMb ]
> drawSierpinskiTriangle ($x1,$y1,$d/2, $hBrushRed)
> 
> dim $x2[3] = [ $x[1], $xMc, $xMa ]
> dim $y2[3] = [ $y[1], $yMc, $yMa ]
> drawSierpinskiTriangle ($x2,$y2,$d/2, $hBrushGreen)
> 
> dim $x3[3] = [ $x[2], $xMb, $xMa ]
> dim $y3[3] = [ $y[2], $yMb, $yMa ]
> drawSierpinskiTriangle ($x3,$y3,$d/2, $hBrushBlue)
> endif
> endfunc
> 
> ;=================================
> func fillTriangle ($x,$y, $hBrush)
> dim $aP[4][2]
> 
> $aP[0][0] = 3
> $aP[1][0] = $x[0]
> $aP[1][1] = $y[0]
> $aP[2][0] = $x[1]
> $aP[2][1] = $y[1]
> $aP[3][0] = $x[2]
> $aP[3][1] = $y[2]
> 
> _GDIP_GraphicsFillClosedCurve($hGraphic, $aP, $hBrush)
> endfunc
> 
> Valery
> 
> --- In [email protected], "valery_vi" <shehrer1@> wrote:
> >
> > lokster from AutoItScript wrote:
> > http://www.autoitscript.com/forum/index.php?showtopic=47882
> > "
> > This example shows how to draw The Sierpinski triangle with autoit
> > ..
> > "
> > 
> > Following script is recursive version of it with help of Auto3Lib:
> > 
> > #include <A3LGDIPlus.au3>
> > Global $hGUI, $hWnd, $hGraphic
> > 
> > ;basis (width of the triangle)
> > Global $d  = 512
> > ;distance from the left
> > Global $x0 = 50
> > ;distance from the top
> > Global $y0 = 50
> > ;height
> > Global $h  = $d*sqrt(3)/2
> > ;limit to recursion in pixels
> > Global $dMin=4
> > 
> > dim $x[3] = [ $x0, $x0+$d, $x0+$d/2 ]
> > dim $y[3] = [ $y0+$h, $y0+$h, $y0 ]
> > 
> > $hGUI = GUICreate("Sierpinski", 600, 600)
> > $hWnd = WinGetHandle("Sierpinski")
> > 
> > _GDIP_Startup()
> > GUISetState()
> > $hGraphic = _GDIP_GraphicsCreateFromHWND($hWnd)
> > 
> > drawSierpinskiTriangle($x,$y,$d/2)
> > 
> > ; Loop until user exits
> > do
> > until GUIGetMsg() = $GUI_EVENT_CLOSE
> > 
> > ; Clean up resources
> > _GDIP_GraphicsDispose($hGraphic)
> > _GDIP_Shutdown()
> > 
> > ;==================================
> > ; recursive draw
> > func drawSierpinskiTriangle ($x, $y, $d )
> > if $d <= $dMin then 
> >   fillTriangle($x,$y)
> > else
> > ;centers of the sides:
> > local $xMc = ($x[0]+$x[1])/2
> > local $yMc = ($y[0]+$y[1])/2
> > local $xMb = ($x[0]+$x[2])/2
> > local $yMb = ($y[0]+$y[2])/2
> > local $xMa = ($x[1]+$x[2])/2
> > local $yMa = ($y[1]+$y[2])/2
> > 
> > dim $x1[3] = [ $x[0], $xMc, $xMb ]
> > dim $y1[3] = [ $y[0], $yMc, $yMb ]
> > drawSierpinskiTriangle ($x1,$y1,$d/2)
> > 
> > dim $x2[3] = [ $x[1], $xMc, $xMa ]
> > dim $y2[3] = [ $y[1], $yMc, $yMa ]
> > drawSierpinskiTriangle ($x2,$y2,$d/2)
> > 
> > dim $x3[3] = [ $x[2], $xMb, $xMa ]
> > dim $y3[3] = [ $y[2], $yMb, $yMa ]
> > drawSierpinskiTriangle ($x3,$y3,$d/2)
> > endif
> > endfunc
> > 
> > ;=================================
> > func fillTriangle ($x,$y)
> > dim $aP[4][2]
> > 
> > $aP[0][0] = 3
> > $aP[1][0] = $x[0]
> > $aP[1][1] = $y[0]
> > $aP[2][0] = $x[1]
> > $aP[2][1] = $y[1]
> > $aP[3][0] = $x[2]
> > $aP[3][1] = $y[2]
> > 
> > _GDIP_GraphicsFillClosedCurve($hGraphic, $aP)
> > endfunc
> > 
> > About Sierpinski Triangle:
> > http://en.wikipedia.org/wiki/Sierpinski_triangle
> > 
> > Enjoy,
> > Valery
> >
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.