Auto Lindenmayer System viewer
"valery_vi" <[email protected]>
| Newsgroups | gmane.comp.windows.autoit.user |
|---|---|
| Message-ID | <[email protected]> |
Hi,
This example is easy 2D viewer for
Lindenmayer System's generation:
;================================
; ALS, © Valery Ivanov, 24 June, 2007
; ALS is Auto Lindenmayer System viewer example
; About LS read book:
; The Algorithmic Beauty of Plants
; by P.Prusinkiewich, A.Lindenmayer
; Home page:
; http://algorithmicbotany.org/
;
#include <A3LGDIPlus.au3>
;======================
; Lindenmayer system (LS) definition
; The alphabet of the LS
Global $V
; LS Axiom
Global $Omega
; LS Production Rules
Global $ProductionCount
; LS Array of Predecessors
Global $Predecessor
; LS Array of Successors
Global $Successor
; LS Predecessors - list of left parts of rules
Global $Predecessors
; Successors - list of right parts of rules
Global $Successors
;======================
; A turtle state
Global $x = 100, $y = 250, $alpha = 0
; turtle step size
Global $d
; turtle angle increment
Global $delta
;======================
Global $Caption = 'ALS, © Valery Ivanov, 24 June, 2007'
Global $hGUI, $hWnd, $hGraphic
Global $hPenRed, $hPenGreen, $hPenBlue
$hGUI = GUICreate($Caption, 360, 360)
$hWnd = WinGetHandle($Caption)
_GDIP_Startup()
$hPenRed = _GDIP_PenCreate(0xFFFF8080)
$hPenGreen = _GDIP_PenCreate(0xFF80FF80)
$hPenBlue = _GDIP_PenCreate(0xFF8080FF)
GUISetState()
$hGraphic = _GDIP_GraphicsCreateFromHWND($hWnd)
;Define real LS
; Quadratic Koch Island (default)
defineLS()
;set level of LS generation
$Generation_Level = 2
; get string view of this LS generation
$stringLS = getLS($Generation_Level)
; draw this LS generation
viewLS($hGraphic, $stringLS, $hPenBlue)
; Loop until user exits
do
until GUIGetMsg() = $GUI_EVENT_CLOSE
; Clean up resources
_GDIP_PenDispose($hPenRed)
_GDIP_PenDispose($hPenGreen)
_GDIP_PenDispose($hPenBlue)
_GDIP_GraphicsDispose($hGraphic)
_GDIP_Shutdown()
exit
;===============
func defineLS($LS = 'Default')
if $LS = 'Default' then
$Omega = 'F-F-F-F'
$V = 'F,+,-'
$Predecessors = 'F'
$Successors = 'F-F+F+FF-F-F+F'
; It's to view LS!
; turtle step size
$d = 10
; start angle
$alpha = 0
; angle increment
$delta = 90
endif
$Predecessor = StringSplit($Predecessors,',')
$Successor = StringSplit($Successors,',')
$ProductionCount = $Predecessor[0]
endfunc
;===============
func getLS($Generation_Level)
local $l
; Get axiom as a first LS generation
$l = $Omega
for $i = 1 to $Generation_Level
;Get next LS generation from the last one
$l = getDerivation($l)
next
return $L
endfunc
;===============
; Get derivation based on rules specified
func getDerivation($String)
local $s, $n, $r, $l, $t
$s = StringSplit($String,'')
$n = $s[0]
$r = ''
for $i = 1 to $n
$l = $s[$i]
for $j = 1 to $ProductionCount
$t = $l
if $l = $Predecessor[$j] then
$t = $Successor[$j]
exitloop
endif
next
$r &= $t
next
return $r
endfunc
;============================================
; View of LS string obtained from Get_LS
func viewLS($hGraphic, $LS_String, $hPen)
local $s
$s = StringSplit($LS_String,'')
for $i = 1 to $s[0]
ATurtle($hGraphic, $s[$i], $hPen)
next
endfunc
;============================================
; About Turtle interpretation of strings read in book above
;Turtle interpretation of DOL-systems
;F : Move forward a step of length d. The state of the turtle changes
to (xn, yn, alpha)
;, where
;xn = x + d*cos(alpha)
;and
;yn = y + d*sin(alpha)
;A line segment between points (x, y) and (xn, yn) is drawn.
;
;f : Move forward a step of length d without drawing a line.
;
;+ : Turn left by angle delta. The next state of the turtle is (x, y,
alpha+delta). The positive orientation of angles is counterclockwise.
;- : Turn right by angle delta. The next state of the turtle is (x,
y, alpha-delta).
;
;============================================
func ATurtle($hG, $c, $hPen)
local $xn, $yn
select
case $c == 'F'
$xn = $x + $d*Cos(Rad($alpha))
$yn = $y + $d*Sin(Rad($alpha))
_GDIP_GraphicsDrawLine($hG, $x, $y, $xn, $yn, $hPen)
$x = $xn
$y = $yn
case $c == 'f'
$x += $d*Cos(Rad($alpha))
$y += $d*Sin(Rad($alpha))
case $c == '+'
$alpha += $delta
case $c == '-'
$alpha -= $delta
endselect
endfunc
;============================================
func Rad($f)
return 3.141592653589793238462643*$f/180
endfunc
There is free book about LS:
"The Algorithmic Beauty of Plants"
by P.Prusinkiewich, A.Lindenmayer
and
Home page of LS projects:
http://algorithmicbotany.org/
Enjoy,
Valery