Note that there are some explanatory texts on larger screens.

plurals
  1. POBresenham concentric circles leaving empty pixels
    primarykey
    data
    text
    <p>I am using the midpoint circle algorithm, also known as Bresenham's, to draw concentric circles. The difference between each circle's radius and that of the next is always 1, so the final result should be a full circular area.</p> <p>However, some pixels are left empty, as shown in the attached image.</p> <p>I'm using Javascript to paint on an HTML5 canvas, manipulating the canvas.getContext("2d").getImageData(...).data array.</p> <p>The circles are alternatively white and red, and the empty pixels are black. You might have to zoom in in order to see what I mean properly.</p> <p><img src="https://i.stack.imgur.com/JE0fM.png" alt="Bresenham concentric circles"></p> <p>I'm trying to add some code to the algorithm so that those pixels are filled when drawing the corresponding arc. There seems to be no reason for any of those pixels to belong to one arc rather than the next one, so I don't care if they are filled along with arcs that have an even radius or with arcs that have an odd radius (I hope I'm making myself clear).</p> <p>The pixels seem to be following a pattern, but I'm clueless about what could that be. Could anyone help me find it?</p> <pre><code>function drawCircles(radius, x, y){ var f = 1 - radius; var ddF_x = 1; var ddF_y = -2 * radius; var x = 0; var y = radius; //Colors var red = 255; var green = radius%2==0?255:0; var blue = radius%2==0?255:0; paintPixel(x, y + radius, red, green, blue); paintPixel(x, y - radius, red, green, blue); paintPixel(x + radius, y, red, green, blue); paintPixel(x - radius, y, red, green, blue); while(x &lt; y){ // ddF_x == 2 * x + 1; // ddF_y == -2 * y; // f == x*x + y*y - radius*radius + 2*x - y + 1; if(f &gt;= 0) { y--; ddF_y += 2; f += ddF_y; } x++; ddF_x += 2; f += ddF_x; paintPixel(x + x, y + y, red, green, blue); paintPixel(x - x, y + y, red, green, blue); paintPixel(x + x, y - y, red, green, blue); paintPixel(x - x, y - y, red, green, blue); paintPixel(x + y, y + x, red, green, blue); paintPixel(x - y, y + x, red, green, blue); paintPixel(x + y, y - x, red, green, blue); paintPixel(x - y, y - x, red, green, blue); } } function paintPixel(x, y, red, green, blue){ imageData.data[grid[y][x]] = red; imageData.data[grid[y][x]+1] = green; imageData.data[grid[y][x]+2] = blue; imageData.data[grid[y][x]+3] = 255; //Alpha } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload