private void Recursion(IGraphics2DContext context, Point p1, Point p2, Point p3, int level) { if (level > 0) { Point p1n = new Point((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2); Point p2n = new Point((p2.X + p3.X) / 2, (p2.Y + p3.Y) / 2); Point p3n = new Point((p3.X + p1.X) / 2, (p3.Y + p1.Y) / 2); Color color = _color ?? Color.Random(); context.DrawLine(p1, p2, color); context.DrawLine(p2, p3, color); context.DrawLine(p3, p1, color); Recursion(context, p1, p1n, p3n, level - 1); Recursion(context, p2, p1n, p2n, level - 1); Recursion(context, p3, p2n, p3n, level - 1); } }
private Rectangle RenderOrCalcMeasure(IGraphics2DContext context, string generator, double angle, double initAngle, Point loc, double step, bool render = true) { Stack <State> states = new Stack <State>(); State state; double x = loc.X; double y = loc.Y; double xn; double yn; double xmin = x; double ymin = y; double xmax = x; double ymax = y; double currAngle = initAngle; foreach (char c in generator) { switch (c) { case '+': currAngle += angle; break; case '-': currAngle -= angle; break; case '[': states.Push(new State(currAngle, x, y)); break; case ']': state = states.Pop(); currAngle = state.Angle; x = state.X; y = state.Y; break; case 'f': case 'F': xn = x + step * Math.Cos(currAngle * Math.PI / 180); yn = y + step * Math.Sin(currAngle * Math.PI / 180); if (c == 'F' && render) { context.DrawLine(x, y, xn, yn, Color); } x = xn; y = yn; if (x > xmax) { xmax = x; } if (x < xmin) { xmin = x; } if (y > ymax) { ymax = y; } if (y < ymin) { ymin = y; } break; default: break; } } return(new Rectangle(xmin, ymin, xmax - xmin, ymax - ymin)); }