Re: Drawing Sierpinski Triangle fractal with AutoIt

"valery_vi" <[email protected]>
Newsgroups gmane.comp.windows.autoit.user
Message-ID <[email protected]>
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.