这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » LCD 在LCD画斜线算法

共2条 1/1 1 跳转至

LCD 在LCD画斜线算法

院士
2006-09-17 18:14:16     打赏
LCD 在LCD画斜线算法



关键词: 斜线     算法    

院士
2006-12-22 22:43:00     打赏
2楼
问 在LCD中画横线和竖线很容易,但画斜线就有点麻烦。
看了各类画斜线函数都SO 长,看的很费劲,我比较懒
没去看,自己写了个用对递归-添中点的方式,很容易
看懂,只是画的线不是很平滑。放在这儿供大家讨论。
void Line(int x0,int y0,int x1,int y1,int color)
{
  int xCenter,yCenter;
  int dx,dy;
  static int oneTime=1;/*两端点独立画,且只画一次*/
  dx=(x1-x0)/2;
  dy=(y1-y0)/2;
  xCenter=x0+dx;
  yCenter=y0+dy;
  if(oneTime==1)/*两端点独立画,且只画一次*/
  {
    putpixel(x0,y0,color);
    putpixel(x1,y1,color);
    oneTime=0;
  }
  if(dx==0&&dy==0)/*如果已经填满了,退出*/
     return;
  else if(dx!=0|dy!=0)
  {
    putpixel(xCenter,yCenter,color);/*画中点*/
    Line(x0,y0,xCenter,yCenter,color);/*上半线条继续画*/
    Line(xCenter,yCenter,x1,y1,color);/*下半线条继续画*/
  }
}
1: ...画线关键是要速度快,避免乘法,开销尽量小。你这样只能自娱一下。还是研究一下Bresenham算法吧,很简单的。 2: Bresenham画斜线算法void Bre_Line(int x1,int y1,int x2,int y2,int color)
{
    int dx,dy,e;
    dx=x2-x1;
    dy=y2-y1;

    if(dx>=0)
    {
        if(dy >= 0) // dy>=0
        {
            if(dx>=dy) // 1/8 octant
            {
                e=dy-dx/2;
                while(x1<=x2)
                {
                    putpixel(x1,y1,color);
                    if(e>0){y1+=1;e-=dx;}
                    x1+=1;
                    e+=dy;
                }
            }
            else        // 2/8 octant
            {
                e=dx-dy/2;
                while(y1<=y2)
                {
                    putpixel(x1,y1,color);
                    if(e>0){x1+=1;e-=dy;}
                    y1+=1;
                    e+=dx;
                }
            }
        }
        else           // dy<0
        {
            dy=-dy;   // dy=abs(dy)

            if(dx>=dy) // 8/8 octant
            {
                e=dy-dx/2;
                while(x1<=x2)
                {
                    putpixel(x1,y1,color);
                    if(e>0){y1-=1;e-=dx;}
                    x1+=1;
                    e+=dy;
                }
            }
            else        // 7/8 octant
            {
                e=dx-dy/2;
                while(y1>=y2)
                {
                    putpixel(x1,y1,color);
                    if(e>0){x1+=1;e-=dy;}
                    y1-=1;
                    e+=dx;
                }
            }
        }
    }
    else //dx<0
    {
        dx=-dx;        //dx=abs(dx)
        if(dy >= 0) // dy>=0
        {
            if(dx>=dy) // 4/8 octant
            {
                e=dy-dx/2;
                while(x1>=x2)
                {
                    putpixel(x1,y1,color);
                    if(e>0){y1+=1;e-=dx;}
                    x1-=1;
                    e+=dy;
                }
            }
            else        // 3/8 octant
            {
                e=dx-dy/2;
                while(y1<=y2)
                {
                    putpixel(x1,y1,color);
                    if(e>0){x1-=1;e-=dy;}
                    y1+=1;
                    e+=dx;
                }
            }
        }
        else           // dy<0
        {
            dy=-dy;   // dy=abs(dy)

            if(dx>=dy) // 5/8 octant
            {
                e=dy-dx/2;
                while(x1>=x2)
                {
                    putpixel(x1,y1,color);
                    if(e>0){y1-=1;e-=dx;}
                    x1-=1;
                    e+=dy;
                }
            }
            else        // 6/8 octant
            {
                e=dx-dy/2;
                while(y1>=y2)
                {
                    putpixel(x1,y1,color);
                    if(e>0){x1-=1;e-=dy;}
                    y1-=1;
                    e+=dx;
                }
            }
        }
    }
}
3: 对比Bresenham算法和中点算法我们可以看到,Bresenham算法和中点算法一样都是乘2或者除2,这里都是除2,而乘2或者除2在计算机中只是左移1位(乘2)或右移1位(除2),速度相当快,因此这两个算法的速度都非常快,而且中点法略快于Bresenham算法(因为程序简单),但光滑度比不上Bresenham算法。如果两点间距离能被2连续整除的次数很多,则中点法画出来的直线也相当光滑大家可以试试:) 4: 中点算法不直么?我看<计算机图形学>中的中点算法原理,应该是很直的才对,每一点都是最接近真实值的栅格点.

不过我用Bresenham,老社区有人贴过用PLM写的程序,我懒,直接找过来改成C,呵呵! 5: 中点法如果长度能被2整除的次数越多越直中点法如果长度能被2整除的次数越多越直
你可以用这种方法画,改变两点间的距离,很容易就能理解的,不理解也没关系,呵呵。

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]