直线Bresenham算法(2)


一、经过改造的二维笛卡尔坐标系

二、算法分析

坐标规律

三、算法C#实现

1、代码

①方法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DDABresenham
{
    public partial class MainForm : Form
    {
        public void Bresenham(int x1, int y1, int x2, int y2, Color color)
        {
            int x, y, dx, dy, p, const1, const2, inc, x11, x22, y11, y22;
            dx = x2 - x1;
            dy = y2 - y1;

            //替换参数,更安全。
            x11 = x1;
            x22 = x2;
            y11 = y1;
            y22 = y2;

            if (dx < 0)
            {
                if (dy > 0)
                {
                    inc = -1;
                }
                else
                {
                    inc = 1;
                }
            }
            else
            {
                if (dy > 0)
                {
                    inc = 1;
                }
                else
                {
                    inc = -1;
                }
            }

            if (Math.Abs(dx) > Math.Abs(dy))
            {
                if (dx < 0)
                {
                    int temp;
                    temp = x11;
                    x11 = x22;
                    x22 = temp;

                    temp = y11;
                    y11 = y22;
                    y22 = temp;

                    dx = -dx;
                    dy = -dy;
                }

                if (dy < 0)
                {
                    dy = -dy;
                }

                p = 2 * dy - dx;
                const1 = 2 * dy;
                const2 = 2 * (dy - dx);

                x = x11;
                y = y11;
                bitmap.SetPixel(x, y, color);
                while (x < x22)
                {
                    x++;
                    if (p < 0)
                    {
                        p += const1;
                    }
                    else
                    {
                        y += inc;
                        p += const2;
                    }
                    bitmap.SetPixel(x, y, color);
                }
            }

            else
            {
                if (dy < 0)
                {
                    int temp;
                    temp = x11;
                    x11 = x22;
                    x22 = temp;

                    temp = y11;
                    y11 = y22;
                    y22 = temp;

                    dx = -dx;
                    dy = -dy;
                }

                if (dx < 0)
                {
                    dx = -dx;
                }

                p = 2 * dx - dy;
                const1 = 2 * dx;
                const2 = 2 * (dx - dy);

                x = x11;
                y = y11;
                bitmap.SetPixel(x, y, color);
                while (y < y22)
                {
                    y++;
                    if (p < 0)
                    {
                        p += const1;
                    }
                    else
                    {
                        x += inc;
                        p += const2;
                    }
                    bitmap.SetPixel(x, y, color);
                }
            }
        }
    }
}

②使用部分

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DDABresenham
{
    public partial class MainForm : Form
    {
        Graphics graphics;
        Bitmap bitmap;
        public MainForm()
        {
            InitializeComponent();
            graphics = panel1.CreateGraphics();
            bitmap = new Bitmap(panel1.Width,panel1.Height);
        }

        private void buttonDDA_Click(object sender, EventArgs e)
        {
            DDA(300, 300, 800, 400, Color.Blue);//1a
            graphics.DrawImage(bitmap,0,0,panel1.Width,panel1.Height);
        }

        private void buttonBresenham_Click(object sender, EventArgs e)
        {
            Bresenham(300, 300, 500, 500, Color.Red);//一象限对角线
            Bresenham(300, 300, 100, 500, Color.Blue);//二象限对角线
            Bresenham(300, 300, 100, 100, Color.Black);//三象限对角线
            Bresenham(300, 300, 500, 100, Color.DarkOrange);//四象限对角线


            Bresenham(300, 300, 300, 600, Color.Red);//向下
            Bresenham(300, 300, 300, 100, Color.Blue);//2a
            Bresenham(300, 300, 100, 300, Color.Black);//3a
            Bresenham(300, 300, 500, 300, Color.Yellow);//4a


            Bresenham(300, 300, 400, 500, Color.Green);//1b
            Bresenham(300, 300, 250, 500, Color.Blue);//2b
            Bresenham(300, 300, 250, 100, Color.Black);//3b
            Bresenham(300, 300, 350, 100, Color.DarkBlue);//4b


            Bresenham(300, 300, 500, 400, Color.Red);//1a
            Bresenham(300, 300, 0, 400, Color.Blue);//2a
            Bresenham(300, 300, 100, 250, Color.Black);//3a
            Bresenham(300, 300, 500, 200, Color.Yellow);//4a

            graphics.DrawImage(bitmap, 0, 0, panel1.Width, panel1.Height);
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

③程序结构

2、Visio流程图

3、运行结果


文章作者: 李世昱
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 李世昱 !
评论
  目录