Example #1
0
        public static Bitmap Bresenham(Bitmap img, int x1, int y1, int x2, int y2, Color cor)
        {
            Bitmap btm = new Bitmap(img);
            int    declive;
            int    dx, dy, incE, incNE, d, x, y;

            dx = x2 - x1;
            dy = y2 - y1;
            if (Math.Abs(dx) > Math.Abs(dy))
            {
                if (x1 > x2)
                {
                    btm = DrawLine.Bresenham(btm, x2, y2, x1, y1, cor);
                }
                else
                {
                    if (dy < 0)
                    {
                        declive = -1;
                        dy      = -dy;
                    }
                    else
                    {
                        declive = 1;
                    }
                    incE  = 2 * dy;
                    incNE = 2 * dy - 2 * dx;
                    d     = 2 * dy - dx;
                    y     = y1;
                    for (x = x1; x <= x2; x++)
                    {
                        Paint.Draw(btm, x, y, cor);
                        if (d <= 0)
                        {
                            d += incE;
                        }
                        else
                        {
                            d += incNE;
                            y += declive;
                        }
                    }
                }
            }
            else
            {
                if (y1 > y2)
                {
                    btm = DrawLine.Bresenham(btm, x2, y2, x1, y1, cor);
                }
                else
                {
                    if (dx < 0)
                    {
                        declive = -1;
                        dx      = -dx;
                    }
                    else
                    {
                        declive = 1;
                    }
                    incE  = 2 * dx;
                    incNE = 2 * dx - 2 * dy;
                    d     = 2 * dx - dy;
                    x     = x1;
                    for (y = y1; y <= y2; y++)
                    {
                        Paint.Draw(btm, x, y, cor);
                        if (d <= 0)
                        {
                            d += incE;
                        }
                        else
                        {
                            d += incNE;
                            x += declive;
                        }
                    }
                }
            }
            return(btm);
        }