Added cw3 subtask 2

This commit is contained in:
Bartosz Hejduk 2021-05-09 22:54:31 +02:00
parent c7550799c5
commit b72108aac3
2 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,25 @@
#ignore + - \ / ^ &
#axiom
C(1,3)F(0,1)G
#rules
C(a,b) > F(x,w) : b<3 -> C(a,b+1)
C(a,b) > F(x,w) : b>=3 -> #stochastic
p=2 C(1,0)
p=1 C(2,0)
p=0.5 C(3,0)
#stochastic end
C(a,b) < F(x,w) : b>=3 -> F(a,w)
F(x,w) < F(y,w1) : x>0 -> F(x,w1)
F(x,w) : x>0 -> F(0,w)
F(x,w) < G : x==1 -> F(0,1)[+B]\(137.5)G
F(x,w) < B : x==2 -> #stochastic
p=3 B
p=1 G
#stochastic end
F(x,w) < G : x==3 -> F(x,1)W
F(x,w) > [F(x1,w1)]F(x2,w2) -> F(x,(w1^2+w2^2)^(0.5))
F(x,w) > F(x1, w1) -> F(x,w1)
F(x,w) > [B]F(x1,x1) -> F(x,w1)
F(x,w) > [F(x1,x1)]B -> F(x,w1)
#end rules

View File

@ -0,0 +1,65 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurtleContextTask : TurtleLSystem {
public GameObject obj;
public GameObject flower;
public GameObject leaf;
public float angle;
private Func<float[], Matrix4x4> _roation(Vector3 axis) {
Matrix4x4 f(float[] args) {
if (args.Length == 0) {
return Matrix4x4.Rotate(Quaternion.AngleAxis(angle, axis));
}
else {
return Matrix4x4.Rotate(Quaternion.AngleAxis(args[0], axis));
}
}
return f;
}
GameObject setColor(GameObject prefab, int index) {
var result = Instantiate(prefab);
switch (index) {
case 1:
result.GetComponent<Renderer>().material.SetColor("_Color", Color.red);
break;
case 2:
result.GetComponent<Renderer>().material.SetColor("_Color", Color.blue);
break;
case 3:
result.GetComponent<Renderer>().material.SetColor("_Color", Color.white);
break;
}
return result;
}
protected override void initLiteralInterpretation() {
turtleInterpretation = new Dictionary<string, Func<float[], Tuple<GameObject, Matrix4x4>>>();
//turtleInterpretation
var transformation = Matrix4x4.Translate(new Vector3(0.0f, 0.1f, 0)) * Matrix4x4.Scale(new Vector3(0.05f, 0.1f, 0.05f));
turtleInterpretation.Add("+", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, _roation(Vector3.back)(args)));
turtleInterpretation.Add("-", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, _roation(Vector3.forward)(args)));
turtleInterpretation.Add("\\", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, _roation(Vector3.down)(args)));
turtleInterpretation.Add("/", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, _roation(Vector3.up)(args)));
turtleInterpretation.Add("^", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, _roation(Vector3.left)(args)));
turtleInterpretation.Add("&", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, _roation(Vector3.right)(args)));
turtleInterpretation.Add("f", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, Matrix4x4.Translate(Vector3.up * args[0])));
turtleInterpretation.Add("W", (float[] args) => new Tuple<GameObject, Matrix4x4>(flower, Matrix4x4.Scale(Vector3.one*0.1f)));
turtleInterpretation.Add("B", (float[] args) => new Tuple<GameObject, Matrix4x4>(leaf, Matrix4x4.Scale(Vector3.one)));
turtleInterpretation.Add("F", (float[] args) => new Tuple<GameObject, Matrix4x4>(setColor(obj, Mathf.FloorToInt(args[0])), transformation * Matrix4x4.Scale(new Vector3(args[1], 1.0f, args[1]))));
//Wildcard how to represent any other symbol
turtleInterpretation.Add("*.*", (float[] args) => new Tuple<GameObject, Matrix4x4>(obj, transformation));
}
}