{"id":151,"date":"2026-11-13T12:32:51","date_gmt":"2026-11-13T04:32:51","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-bresenham-rasters-a-line-with-integer-steps\/"},"modified":"2026-11-13T21:51:22","modified_gmt":"2026-11-13T13:51:22","slug":"how-bresenham-rasters-a-line-with-integer-steps","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-bresenham-rasters-a-line-with-integer-steps\/","title":{"rendered":"How Bresenham rasters a line with integer steps"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Bresenham&#8217;s line algorithm<\/strong> chooses the pixel sequence from (x0, y0) to (x1, y1) using only integer additions. It walks the major axis one pixel at a time and keeps an error term that decides when the minor axis steps. Starting from zero and adding twice the minor delta, then subtracting twice the major delta when the error crosses zero, stays in integers. The line is the one a raster display should draw. It is not a sub-pixel geometric intersection test.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The real segment crosses a row or column of pixels, and the pixel closest to the ideal line is the one to light. Floating-point interpolation computes that pixel and spends a division per step. Bresenham folds the comparison into an integer error that has the sign of the distance to the boundary between two candidate pixels.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The major axis is the one with the larger absolute delta, so the walk takes one step on that axis every iteration and zero or one step on the other. Swapping the axes when |dy| &gt; |dx| is required. A version written only for shallow slopes draws a broken line on a steep segment.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">For integer endpoints the algorithm emits a connected 8-connected sequence from the start to the end, inclusive, and it is symmetric if the error rule is symmetric. Time is proportional to the longer delta. Extra memory is a few integers. The decision does not use multiplication after the deltas are doubled, and many implementations keep the doubled constants outside the loop.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The raster line is not the same object as a geometric segment in Bentley-Ottmann. Two raster lines can share a pixel without the real segments intersecting, and the real segments can intersect in a pixel neither line lists if the thickness is zero and the crossing is off the chosen pixels. Use the raster algorithm for pixels and the geometric algorithm for geometry.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Let dx = x1\u2212x0 and dy = y1\u2212y0. Record the signs sx and sy as \u22121 or +1, and replace dx and dy by their absolute values.<\/li>\n<li>If dy &gt; dx, the major axis is y. Use the same error logic with the axes swapped, or loop on y.<\/li>\n<li>Set err = 2\u00b7dy \u2212 dx for a shallow line that steps in x. At each x step, emit the pixel, add 2\u00b7dy to err, and when err is positive subtract 2\u00b7dx and step y by sy.<\/li>\n<li>Include both endpoints. The loop runs dx times after the first pixel, or an equivalent inclusive count.<\/li>\n<li>For a steep line, exchange the roles of x and y in the error update.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The integer error is for a single-pixel line between integer endpoints. Wide lines, anti-aliasing, and sub-pixel endpoints are different algorithms. Wu&#8217;s algorithm anti-aliases. A geometric clipping test such as Cohen-Sutherland decides whether the real segment meets a rectangle and does not choose pixels.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when a steep line uses the shallow error update<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Always step x and sometimes step y, even when the line is steeper than 45 degrees<\/td>\n<td>The pixels then jump in y by more than one and the line has holes. The major axis has to be the longer delta.<\/td>\n<\/tr>\n<tr>\n<td>Use a floating slope and round<\/td>\n<td>The picture can match and the algorithm is no longer Bresenham. The integer error is the algorithm.<\/td>\n<\/tr>\n<tr>\n<td>Treat a shared raster pixel as a geometric intersection<\/td>\n<td>Pixel choice is a rounding of the line. Intersection of the underlying segments is an orientation test.<\/td>\n<\/tr>\n<tr>\n<td>Stop before the final endpoint<\/td>\n<td>An off-by-one in the iteration count leaves the last pixel dark. The inclusive convention has to be stated and tested on a one-pixel segment.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Anti-aliased lines are Wu&#8217;s algorithm. Rectangle clipping of the geometric segment is Cohen-Sutherland. Bresenham is the integer raster line.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment walks the major axis one pixel at a time and keeps an integer error term. From (0, 0) to (4, 1) the cells are <code>(0, 0) (1, 0) (2, 0) (3, 1) (4, 1)<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def bresenham(x0, y0, x1, y1):\n    steep = abs(y1 - y0) &gt; abs(x1 - x0)\n    if steep:\n        x0, y0, x1, y1 = y0, x0, y1, x1\n    if x0 &gt; x1:\n        x0, x1, y0, y1 = x1, x0, y1, y0\n    dx, dy = x1 - x0, abs(y1 - y0)\n    err, y, ystep = dx \/\/ 2, y0, 1 if y0 &lt; y1 else -1\n    cells = []\n    for x in range(x0, x1 + 1):\n        cells.append((y, x) if steep else (x, y))\n        err -= dy\n        if err &lt; 0:\n            y += ystep\n            err += dx\n    return cells\n\ncells = bresenham(0, 0, 4, 1)\nassert cells[0] == (0, 0) and cells[-1] == (4, 1)\nprint(*cells)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>Bresenham&#8217;s<\/strong> line algorithm is the integer stepper in raster graphics, plotters, and embedded framebuffers. Each step picks the pixel closer to the true line without a division. Antialiased and GPU lines use a different coverage test.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop a per-step division when drawing many integer-endpoint lines. Use the error term, and swap axes when the slope is steep.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Bresenham&#8217;s algorithm returns the integer error term and the major-axis swap. <strong>Bresenham&#8217;s algorithm rasters a line with integer additions by stepping the major axis every time and stepping the minor axis when an error term crosses zero.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Bresenham&#8217;s line algorithm chooses the pixel sequence from (x0, y0) to (x1, y1) using only integer additions. It walks the major axis one pixel at a time and keeps an error term that decides when the minor axis steps. Starting from zero and adding twice the minor&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,40],"tags":[],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-geometry"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":550,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/151\/revisions\/550"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}