Production rules : the description of how one string of symbols generate another.
An example
Alphabets: F, -, +
Axiom: F
Rules: F -> F + F - - F + F
The first generation is : F+F--F+F
The second generation is : F+F--F+F + F+F--F+F - - F+F--F+F + F+F--F+F
We are working on a pure syntactic level. These strings of symbols do not make any sense to us.
Examples
In the natural environment, we may have this,
Compare the above leaves with this one,
It will be quite difficult to describe systematically the growth of this simple plant.
Consider this L-System language.
Alphabets: F, B, -, +, [, ]
Axiom: B
Rules: B -> F
B -> F[-B]+B
Let's expand the sentence.
First generation
F
Second generation
F[-F]+F
Third generation
F[-F[-F]+F]+F[-F]+F
Topic 3: Meaning
Remember the Logo program we learnt in primary school.
If we assign the meaning to the abstract symbols in the last topic, we can have,
Command
Explanation
F
Draw forward by a fixed length
f
Move forward by a fixed length
+
Turn anti-clockwise for a fixed angle
-
Turn clockwise for a fixed angle
[
Store the current position and direction
]
Restore the last stored position and direction
The sentence, F[-F]+F will start to make sense, i.e., we have assigned meaning to it. Using the above table, try to draw out the sentence,
F[-F[-F]+F]+F[-F]+F
The next step is to automate the drawing in ActionScript.
Topic 4: Recursion in programming
For loop re-visit.
The program is a very simple for loop iteration.
var sum:int = 0;
output.text = "";
final var MAX:int = 10;
for (var i:int=0;i<=MAX;i++) {
sum += i;
output.text += "Loop " + i + " - " + sum + "\n";
}
The same problem, another approach - recursive function.
The code for the recursive function.
function sum(_n:int):int {
if (_n==0) {
return 0;
} else {
output.text += "call : " + _n + "\n";
var s:int = sum(_n-1)+_n;
output.text += "sum : " + s + "\n";
return s;
}
}
output.text = "";
sum(10);
Note that in the defintion of the sum() function, it calls itself.
More practical use of recursion.
In the example, we have defined a movieclip named Square.
function onOver(e:MouseEvent):void {
var s:Square = new Square();
s.x = e.target.x+(Math.random()*s.width-s.width/2);
s.y = e.target.y-s.height-5;
var d:int = (stage.stageHeight-s.y)/5;
s.x = e.target.x+(Math.random()*d-d/2);
s.alpha = s.y/stage.stageHeight;
s.rotation = Math.random()*180;
s.addEventListener(MouseEvent.ROLL_OVER,onOver);
stage.addChild(s);
}
var s:Square = new Square();
s.x = stage.stageWidth/2;
s.y = stage.stageHeight-s.height/2;
s.addEventListener(MouseEvent.ROLL_OVER,onOver);
addChild(s);
Note in this case we add the same event listener onOver() within the defintion of itself.
Key points to remember in writing recursive function.
Break a complex problem into a repetition of it ownself.
Establish the initial condition for stopping the execution.
Topic 5: Turtle graphics
Use this Turtle class for the subsequent exercises. Again, put it inside a sm2220 folder.
package sm2220 {
import flash.display.Sprite;
public class Turtle extends Sprite {
private var xpos:Number;
private var ypos:Number;
private var ang:Number;
private var dir:Number;
private var src:Object;
private var stk:Array;
public function Turtle(_p:Object,
_x:Number, _y:Number, _d:Number, _a:Number) {
src = _p;
this.graphics.clear();
this.graphics.lineStyle(1,0x000000,1);
dir = _d;
xpos = _x;
ypos = _y;
ang = _a;
stk = new Array();
src.addChild(this);
}
public function clear():void {
this.graphics.clear();
this.graphics.lineStyle(1,0x000000,1);
}
public function move(_l:Number):void {
var da:Number = dir*Math.PI/180.0;
xpos += _l*Math.cos(da);
ypos += _l*Math.sin(da);
}
public function mark(_l:Number):void {
var da:Number = dir*Math.PI/180.0;
this.graphics.moveTo(xpos,ypos);
xpos += _l*Math.cos(da);
ypos += _l*Math.sin(da);
this.graphics.lineTo(xpos,ypos);
}
public function left():void {
dir -= ang;
dir %= 360;
}
public function right():void {
dir += ang;
dir %= 360;
}
public function push():void {
stk.push({xpos: xpos, ypos:ypos, dir:dir});
}
public function pop():void {
var obj:Object = stk.pop();
xpos = obj.xpos;
ypos = obj.ypos;
dir = obj.dir;
}
public function turn(_a:Number):void {
dir += _a;
dir %= 360;
}
}
}
Define an instance of the class by,
var t1:Turtle = new Turtle(this,200,200,0,30);
The first parameter refers the movie main timeline.
The second and third are the starting point x and y position.
The third one is the initial direction of the turtle.
The forth one is the angle increment for each turn.