【问题标题】:Can I easily skip pixels in Bresenham's line algorithm?我可以轻松跳过 Bresenham 线算法中的像素吗?
【发布时间】:2013-03-08 13:35:41
【问题描述】:

我有一个程序使用Bresenham's line algorithm 扫描一行中的像素。这是读取像素而不是写入像素,在我的特定情况下,读取它们的成本很高。

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

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 步计算所需的值吗?

【问题讨论】:

    标签: algorithm optimization graphics bresenham


    【解决方案1】:

    Bresenhams 中点算法通过总结 数字差异 delta_x = (by-ay) 来计算点到从 (ax,ay)->(bx,by) 的理论线的“距离”, delta_y = (ax-bx)。

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

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

    免责声明:所讲的内容可能需要调整一半。

    【讨论】:

      【解决方案2】:

      你的主循环看起来基本上是这样的:

        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--;
        }
      

      所以,跳过一些 q 像素相当于进行以下调整(除非我在某处犯了错误):

      • cnt = cnt - q
      • k = (k + n * q) % m
      • xnew = xold + ((kold + n * q) / m) * dx1 + (q - (( k + n * q) / m)) * dx2
      • ynew = yold + ((kold + n * q) / m) * dy1 + (q - (( k + n * q) / m)) * dy2

      注意 / 和 % 是整数除法和模运算符。

      【讨论】:

        猜你喜欢
        • 2011-06-19
        • 1970-01-01
        • 1970-01-01
        • 2021-04-15
        • 1970-01-01
        • 1970-01-01
        • 2018-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多