Advertisement
Fhernd

SierpinskiTriangle.cs

Jul 1st, 2016
954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. // Start:
  2. string sierpinskiTriangle = "A";
  3.  
  4. // Replacement rules:
  5. Func<string, string> transformA = x => x.Replace("A", "B-A-B");
  6. // Auxiliary replacement rule:
  7. Func<string, string> markBs = x => x.Replace("B", "[B]");
  8. Func<string, string> transformB = x => x.Replace("[B]", "A+B+A");
  9.  
  10. // Recursive stages:
  11. int length = 6;
  12.  
  13. // Applies the transformation rules:
  14. Enumerable.Range(1, length)
  15.     .ToList()
  16.     .ForEach (k => sierpinskiTriangle =
  17.         transformB(transformA(markBs(sierpinskiTriangle))));
  18.  
  19. // Performs the replacements for LOGO commands:
  20. sierpinskiTriangle = sierpinskiTriangle
  21.     .Replace("A", "forward 5" + Environment.NewLine)
  22.     .Replace("B", "forward 5" + Environment.NewLine)
  23.     .Replace("+", "left 60" + Environment.NewLine)
  24.     .Replace("-", "right 60" + Environment.NewLine);
  25.  
  26. // Shows the resultant LOGO commands for drawing a Sierpenski triangle:
  27. sierpinskiTriangle.Dump("LOGO commands for drawing a Sierpinsky Triangle");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement