acr8 发表于 2006-3-16 21:31:37

请大家看一下我这个在液晶中作直线的程序,我做了几个都没成功

我的是一个sed1335程序, 我已经作出一个没问题的作点程序

作直线程序可怎么也不怎么成功。应该是算法问题



例如,起点的x坐标、y坐标比终点小就能很好的画出



但是反过来就不可以了



/************************************************/

/*画线。任意方向的斜线,不支持垂直的或水平线 */

/************************************************/

   

void disp_Linexy(Uchar x_start,Uchar y_start,Uchar x_end,Uchar y_end,Uchar screen)

{

register Uchar t;

int xerr=0,yerr=0,delta_x,delta_y,distance;

Uchar incx,incy;



/*compute the distance in both directions */

delta_x = x_end - x_start;

delta_y = y_end - y_start;



/* computethe direction of the increment ,

an increment of "0" means either a vertical or horizontal lines */



if(delta_x>0) incx=1;

else if( delta_x==0 ) incx=0;

else incx=-1;





if(delta_y>0) incy=1;

else if( delta_y==0 ) incy=0;

else incy=-1;



/*determine which distance is greater */

delta_x = cabs( delta_x );

delta_y = cabs( delta_y );



if( delta_x > delta_y ) distance=delta_x;

else distance=delta_y;



/* draw the line */

for( t=0;t <= distance+1; t++ ) {

    disp_Point(x_start,y_start,screen);

   xerr += delta_x ;

   yerr += delta_y ;

if( xerr > distance ) {

   xerr-=distance;

       x_start+=incx;

}

if( yerr > distance ) {

   yerr-=distance;

       y_start+=incy;

}

delay_ms(300);

}

}











/************************************************/

/*画线。任意方向的斜线,不支持垂直的或水平线 */

/************************************************/

void Linexy_new(Uint x1,Uchar y1,Uint xt,Uchar yt,Uchar screen)

{

    register Uchar t;

    int dx;

    char dy;

    Uint ix,iy,inc,plotx,x;

    Uchar ploty,y,plot,i;

    plotx=x1;

    ploty=y1;

    x=0;y=0;plot=1;

    dx=xt-x1;

    dy=yt-y1;

    ix=(dx>0)?dx:(-dx);

    iy=(dy>0)?dy:(-dy);

    inc=(ix>iy)?ix:iy;

    for(t=0;t<inc;t++)

    {

      if(plot)

       disp_Point(plotx,ploty,screen);

       x+=ix;

       y+=iy;

       plot=0;

       if(y>inc)

      {plot=1;

         y-=inc;

          if(dy>0)

            ploty++;

          if(dy<0)

            ploty--;

         }

       if(x>inc)

      {plot=1;

         x-=inc;

      if(dx>0)

          plotx++;

      if(dx<0)

          plotx--;

      }

   

}

   

}                        //画点



















/************************************************/

/*画线。任意方向的斜线,不支持垂直的或水平线 */

/************************************************/



void Linexy(Uchar x0,Uchar y0,Uchar xt,Uchar yt,Uchar screen)

{

    register Uchar t;

    int xerr=0,yerr=0,delta_x,delta_y,distance;

    int incx,incy;



    delta_x = xt-x0;                            //计算坐标增量

    delta_y = yt-y0;

   

    if(delta_x>0) incx=1;                     //设置单步方向

    else

    {

      if( delta_x==0) incx=0;               //垂直线

      else {incx=-1;delta_x=-delta_x;}

    }

    if(delta_y>0) incy=1;

    else

    {

      if( delta_y==0) incy=0;               //水平线

      else {incy=-1;delta_y=-delta_y;}

    }

    if( delta_x > delta_y ) distance=delta_x;   //选取基本增量坐标轴

    else distance=delta_y;



    for( t=0;t <= distance+1; t++ )

    {                                           //画线输出

      disp_Point(x0,y0,screen);                              //画点

      xerr += delta_x ;

      yerr += delta_y ;

      

      if( xerr >= distance )

      {

            xerr-=distance;

            x0+=incx;

      }

      if( yerr >= distance )

      {

            yerr-=distance;

         y0+=incy;

      }

    }

}
-----此内容被acr8于2006-03-16,21:32:48编辑过

yizheng 发表于 2006-3-17 11:22:22

我给你一个我写的画线的程序(与硬件无关),没有什么限制,但你要把你画点的程序做出来,该程序与硬件相关。如果你不需要清除,可去掉 bool clr 参数,当然你画点不要这个参数。



void line(uint8 x1, uint8 y1, uint8 x2, uint8 y2, bool clr)

{uint8 i, xsub, ysub;

bool dir;



    if(y1 > y2)

    {

      if(x2 > x1)

      {

            dir = true;

      }

      else

      {

            dir = false;

            i = x2;

            x2 = x1;

            x1 = i;

      }

      i = y2;

      y2 = y1;

      y1 = i;

    }

    else

    {

      dir = false;

      if(x1 > x2)

      {

            dir = true;

            i = x2;

            x2 = x1;

            x1 = i;

      }

    }



    xsub = x2 - x1 + 1;

    ysub = y2 - y1 + 1;

    x2 = x1;

    if(!dir)

    {

      y2 = y1;

    }

      

    for(y1 = 1; y1 <= ysub; y1++)

    {

      i = (uint16)(y1 * xsub) / ysub + x1;

      do

      {

            dot(x2, y2, clr);



            if(x2 < i)

            {

                ++x2;

            }

      }while(x2 < i);



      if(dir)

      {

            --y2;

      }

      else

      {

            ++y2;

      }

    }

}
-----此内容被yizheng于2006-03-17,11:33:28编辑过

kevinsun09 发表于 2012-10-22 13:28:20

我遇到了跟你同样的问题,请问你后来是怎么解决的?能不能指点一下我。。。我用的芯片是STM32F407VGT6 ,想用它驱动LCD液晶屏 ,在液晶屏上画线 ,液晶屏驱动芯片是SSD1289,但是使用画线函数时,起点坐标比终点坐标小时可以正常画线,起点坐标比终点坐标大时,比如x0大于x1 或y0大于y1时,画出的直线就不对了
页: [1]
查看完整版本: 请大家看一下我这个在液晶中作直线的程序,我做了几个都没成功