Пример #1
0
        public static void FloodFill(Bitmap b, Point p, Color newColor)
        {
            var oldColor = b.GetPixel(p.X, p.Y);
            var stack    = new StackArrayList <Point>();

            stack.Push(p);
            while (stack.Count != 0)
            {
                p = stack.Pop();
                if (b.GetPixel(p.X, p.Y).ToArgb() != oldColor.ToArgb())
                {
                    continue;
                }
                b.SetPixel(p.X, p.Y, newColor);

                stack.Push(new Point(p.X, p.Y + 1));
                stack.Push(new Point(p.X + 1, p.Y));
                stack.Push(new Point(p.X, p.Y - 1));
                stack.Push(new Point(p.X - 1, p.Y));

                stack.Push(new Point(p.X + 1, p.Y + 1));
                stack.Push(new Point(p.X - 1, p.Y + 1));
                stack.Push(new Point(p.X + 1, p.Y - 1));
                stack.Push(new Point(p.X - 1, p.Y - 1));
            }
        }
        public static double EvaluateExpression(this string expression)
        {
            var operations = new StackArrayList <string>();
            var values     = new StackArrayList <double>();
            var tokens     = expression.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var t in tokens)
            {
                if (t.IsNumeric())
                {
                    values.Push(double.Parse(t));
                }
                else
                {
                    operations.Push(t);
                }

                if (values.Count != 2)
                {
                    continue;
                }
                var v2 = values.Pop();
                var v1 = values.Pop();
                switch (operations.Pop())
                {
                case "+":
                    values.Push(v1 + v2);
                    break;

                case "-":
                    values.Push(v1 - v2);
                    break;

                case "*":
                    values.Push(v1 * v2);
                    break;

                case "/":
                    values.Push(v1 / v2);
                    break;
                }
            }
            return(values.Pop());
        }
Пример #3
0
        public void Paint(Graphics g)
        {
            Transform(g);

            foreach (var command in _commands)
            {
                var stack = new StackArrayList <string>();
                foreach (var param in command)
                {
                    stack.Push(param);
                }

                switch (stack.Pop().ToLower())
                {
                case "setlinewidth":
                    p.Width = int.Parse(stack.Pop());
                    break;

                case "setrgbcolor":
                    var ci    = CultureInfo.InvariantCulture;
                    var blue  = (int)(double.Parse(stack.Pop(), ci) * 255);
                    var green = (int)(double.Parse(stack.Pop(), ci) * 255);
                    var red   = (int)(double.Parse(stack.Pop(), ci) * 255);
                    p.Brush = new SolidBrush(Color.FromArgb(red, green, blue));
                    break;

                case "arc":
                    var sweepAngle = float.Parse(stack.Pop());
                    var startAngle = float.Parse(stack.Pop());
                    var radius     = float.Parse(stack.Pop());
                    var y          = float.Parse(stack.Pop());
                    var x          = float.Parse(stack.Pop());
                    g.DrawArc(p, x - radius, y - radius, radius * 2, radius * 2, startAngle, sweepAngle);
                    break;

                case "moveto":
                    _y1 = int.Parse(stack.Pop());
                    _x1 = int.Parse(stack.Pop());
                    break;

                case "lineto":
                    var y2 = int.Parse(stack.Pop());
                    var x2 = int.Parse(stack.Pop());
                    g.DrawLine(p, _x1, _y1, x2, y2);
                    _x1 = x2;
                    _y1 = y2;
                    break;
                }
            }
        }