高速直线绘制程序

5 11 月, 2009 by admin Leave a reply »

程序:罗健军


我特别的感谢罗健军大师,罗健军是一位水平高超的程序设计师。按我的推断,他应该有着丰富、高超的编程经验,远非在下能及。我再次向他表示感谢。

下面的算法中,var16表示有符号16位数据类型;HLine(x,y,n)是画水平线函数;VLine(x,y,n)是画垂直线函数;PutPixel(x,y)是画点函数。请看程序。

void Line(var16 x1,var16 y1,var16 x2,var16 y2)
{
 var16 p,n,x,y,tn;
 if(y1==y2)
 {
  if(x1>x2)
  {
   x=x2;x2=x1;x1=x;
  }
  HLine(x1,y1,x2-x1+1);
  return;
 }
 if(x1==x2)
 {
  if(y1>y2)
  {
   y=y2;y2=y1;y1=y;
  }
  VLine(x1,y1,y2-y1+1);
  return;
 }
 if(abs(y2-y1)<=abs(x2-x1))
 {
  if((y2<y1&&x2<x1)||(y1<=y2&&x1>x2))
  {
   x=x2;y=y2;x2=x1;y2=y1;x1=x;y1=y;
  }
  if(y2>=y1&&x2>=x1)
  {
   x=x2-x1;y=y2-y1;
   p=2*y;n=2*x-2*y;tn=x;
   while(x1<=x2)
   {
    if(tn>=0)tn-=p;
     else {tn+=n;y1++;}
    PutPixel(x1++,y1);
   }
  }
  else
  {
   x=x2-x1;y=y2-y1;
   p=-2*y;n=2*x+2*y;tn=x;
   while(x1<=x2)
   {
    if(tn>=0)tn-=p;
     else {tn+=n;y1--;}
    PutPixel(x1++,y1);
   }
  }
 }
 else
 {
  x=x1;x1=y2;y2=x;y=y1;y1=x2;x2=y;
  if((y2<y1&&x2<x1)||(y1<=y2&&x1>x2))
  {
   x=x2;y=y2;x2=x1;y2=y1;x1=x;y1=y;
  }
  if(y2>=y1&&x2>=x1)
  {
   x=x2-x1;y=y2-y1;p=2*y;n=2*x-2*y;tn=x;
   while(x1<=x2)
   {
    if(tn>=0)tn-=p;
     else {tn+=n;y1++;}
    PutPixel(y1,x1++);
   }
  }
  else
  {
   x=x2-x1;y=y2-y1;p=-2*y;n=2*x+2*y;tn=x;
   while(x1<=x2)
   {
    if(tn>=0)tn-=p;
     else {tn+=n;y1--;}
    PutPixel(y1,x1++);
   }
  }
 }
}
Advertisement

发表回复

You must be logged in to post a comment.
鄂ICP备09027626号