Bresenham算法画线

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using System.Drawing;namespace BresenhamLine.Util
{
    class DrawLine
    {
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        internal static extern bool SetPixel(IntPtr hdc, int x, int y, uint color);

        public void MyDrawLine(Graphics g, Point pStart, Point pEnd)
        {
            int x, y, dx, dy, m;
            float e;
            x =pStart.X;
            y =pStart.Y;
            dx = pEnd.X - pStart.X;
            dy = pEnd.Y - pStart.Y;
            m = dy / dx;
            e = m - 0.5f;

            Pen pen = new Pen(Color.Red,1);
            IntPtr hdc = g.GetHdc();

            for (int i = 1; i < dx; i++)
            {
                SetPixel(hdc, x, y, 0xffff0000);
                while (e > 0)
                {
                    y = y + 1;
                    e = e - 1;
                }
                x = x + 1;
                e = e + m;
            }
            g.ReleaseHdc(hdc);
        }
    }
}
原文地址:https://www.cnblogs.com/WilliamJiang/p/2597948.html