Bresenham算法画线

原文链接:http://www.cnblogs.com/WilliamJiang/archive/2012/07/18/2597948.html
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/archive/2012/07/18/2597948.html

上一篇:python – 我对Bresenham算法的实现在特定角度的线路上失败了


下一篇:STM32 ADC多通道转换详解(附源代码)