Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Start:
- string sierpinskiTriangle = "A";
- // Replacement rules:
- Func<string, string> transformA = x => x.Replace("A", "B-A-B");
- // Auxiliary replacement rule:
- Func<string, string> markBs = x => x.Replace("B", "[B]");
- Func<string, string> transformB = x => x.Replace("[B]", "A+B+A");
- // Recursive stages:
- int length = 6;
- // Applies the transformation rules:
- Enumerable.Range(1, length)
- .ToList()
- .ForEach (k => sierpinskiTriangle =
- transformB(transformA(markBs(sierpinskiTriangle))));
- // Performs the replacements for LOGO commands:
- sierpinskiTriangle = sierpinskiTriangle
- .Replace("A", "forward 5" + Environment.NewLine)
- .Replace("B", "forward 5" + Environment.NewLine)
- .Replace("+", "left 60" + Environment.NewLine)
- .Replace("-", "right 60" + Environment.NewLine);
- // Shows the resultant LOGO commands for drawing a Sierpenski triangle:
- sierpinskiTriangle.Dump("LOGO commands for drawing a Sierpinsky Triangle");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement