public ToothpickCollection(int iterations) { ToothpickLength = 1; IterationCount = iterations; var init = new Toothpick(1, 1, 1, 2); //var init = new Toothpick(1, 1, 1.5, 2); this.Toothpicks.Add(init); this.ToothpickIterations.Add(new List <Toothpick>() { init }); for (int i = 0; i < IterationCount; i++) { var newpicks = Add(); this.ToothpickIterations.Add(newpicks); } }
public List <Toothpick> Add() { var newpicks = new List <Toothpick>(); foreach (var pick in this.Toothpicks) { var pcount1 = Count(pick.x0, pick.y0); if (pcount1 < 2) { var x3 = pick.x0 - (pick.y1 - pick.y0) / 2; var y3 = pick.y0 + (pick.x1 - pick.x0) / 2; var x4 = pick.x0 + (pick.y1 - pick.y0) / 2; var y4 = pick.y0 - (pick.x1 - pick.x0) / 2; var newpick1 = new Toothpick(x3, y3, x4, y4); var dupe = Count(newpick1.x0, newpick1.y0, newpick1.x1, newpick1.y1); if (dupe == 0) { newpicks.Add(newpick1); } } var pcount2 = Count(pick.x1, pick.y1); if (pcount2 < 2) { var x3b = pick.x1 - (pick.y1 - pick.y0) / 2; var y3b = pick.y1 + (pick.x1 - pick.x0) / 2; var x4b = pick.x1 + (pick.y1 - pick.y0) / 2; var y4b = pick.y1 - (pick.x1 - pick.x0) / 2; var newpick2 = new Toothpick(x3b, y3b, x4b, y4b); var dupe = Count(newpick2.x0, newpick2.y0, newpick2.x1, newpick2.y1); if (dupe == 0) { newpicks.Add(newpick2); } } } this.Toothpicks.AddRange(newpicks); return(newpicks); }