MFC工程中GDI(非GDI+)画图反走样 抗锯齿 算法

MFC工程中GDI(非GDI+)画图反走样 抗锯齿 算法


我们先来看一下处理前后的效果图

处理前

这里写图片描述

处理后

这里写图片描述
在使用GDI画图时很容易就会出现锯齿,网上给出的方法是使用GDI+,虽然简单,但是限制了软件的移植。
在这向大家分享一套反走样的算法


代码

void DrawLine( CDC *pDC, int X0, int Y0, int X1, int Y1, COLORREF clrLine )
{
    /* Make sure the line runs top to bottom */
    if (Y0 > Y1)
    {
        int Temp = Y0; Y0 = Y1; Y1 = Temp;
        Temp = X0; X0 = X1; X1 = Temp;
    }

    /* Draw the initial pixel, which is always exactly intersected by
    the line and so needs no weighting */
    pDC->SetPixel( X0, Y0, clrLine );

    int XDir, DeltaX = X1 - X0;
    if( DeltaX >= 0 )
    {
        XDir = 1;
    }
    else
    {
        XDir   = -1;
        DeltaX = 0 - DeltaX; /* make DeltaX positive */
    }

    /* Special-case horizontal, vertical, and diagonal lines, which
    require no weighting because they go right through the center of
    every pixel */
    int DeltaY = Y1 - Y0;
    if (DeltaY == 0)
    {
        /* Horizontal line */
        while (DeltaX-- != 0)
        {
            X0 += XDir;
            pDC->SetPixel( X0, Y0, clrLine );
        }
        return;
    }
    if (DeltaX == 0)
    {
        /* Vertical line */
        do
        {
            Y0++;
            pDC->SetPixel( X0, Y0, clrLine );
        } while (--DeltaY != 0);
        return;
    }

    if (DeltaX == DeltaY)
    {
        /* Diagonal line */
        do
        {
            X0 += XDir;
            Y0++;
            pDC->SetPixel( X0, Y0, clrLine );
        } while (--DeltaY != 0);
        return;
    }

    unsigned short ErrorAdj;
    unsigned short ErrorAccTemp,
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值