Algorithm 我是否可以轻松跳过Bresenham';s线算法?

Algorithm 我是否可以轻松跳过Bresenham';s线算法?,algorithm,optimization,graphics,bresenham,Algorithm,Optimization,Graphics,Bresenham,我有一个程序,用来扫描一行中的像素。这是读取像素而不是写入像素,在我的特殊情况下,读取像素是昂贵的 但是,我可以确定不需要读取某些像素范围。它看起来像这样: Normal scan of all pixels: *start \ \ \ \ \ *end Scan without reading all pixels: *start \ \ - At this point I know I can skip (for exam

我有一个程序,用来扫描一行中的像素。这是读取像素而不是写入像素,在我的特殊情况下,读取像素是昂贵的

但是,我可以确定不需要读取某些像素范围。它看起来像这样:

Normal scan of all pixels:

*start
 \
  \
   \
    \
     \
      *end

Scan without reading all pixels:

*start
 \
  \
        - At this point I know I can skip (for example) the next 100 pixels
          in the loop. Crucially, I can't know this until I reach the gap.
     \
      *end

中间的间隙要快得多,因为我可以在不读取它们的情况下迭代像素。


但是,我是否可以通过任何方式修改循环,使其在循环内直接向前跳100个像素,在直线算法中直接向前跳100步计算所需的值?

Bresenhams中点算法通过将数字差delta_x=(按y),δy=(ax bx)

因此,如果想要跳过7个像素,必须添加accum+=7*delta_x;然后除以delta_y,可以检查应该在y方向上移动了多少像素,并取剩余的accum=accum%delta_y,应该能够在适当的位置继续

好在该算法源于避免除法的必要性


免责声明:无论告诉什么,都可能需要调整半个增量。

您的主循环基本上类似于:

  while (cnt > 0) // cnt is 1 + the biggest of abs(x2-x1) and abs(y2-y1)
  {
    ReadOrWritePixel(x, y);

    k += n; // n is the smallest of abs(x2-x1) and abs(y2-y1)
    if (k < m) // m is the biggest of abs(x2-x1) and abs(y2-y1)
    {
      // continuing a horizontal/vertical segment
      x += dx2; // dx2 = sgn(x2-x1) or 0
      y += dy2; // dy2 = sgn(y2-y1) or 0
    }
    else
    {
      // beginning a new horizontal/vertical segment
      k -= m;
      x += dx1; // dx1 = sgn(x2-x1)
      y += dy1; // dy1 = sgn(y2-y1)
    }

    cnt--;
  }
while(cnt>0)//cnt为1+abs(x2-x1)和abs(y2-y1)中的最大值
{
可读写像素(x,y);
k+=n;//n是abs(x2-x1)和abs(y2-y1)中的最小值
if(k
因此,跳过一些q像素相当于以下调整(除非我在某个地方出错):

  • cntnew=cntold-q
  • 已知=(kold+n*q)%m
  • xnew=xold+((kold+n*q)/m)*dx1+(q-((kold+n*q)/m))*dx2
  • ynew=yold+((kold+n*q)/m)*dy1+(q-((kold+n*q)/m))*dy2
请注意,/和%是整数除法和模运算