I am attempting to draw a circle (and eventually fill the entire circle) using Bresenham's Midpoint Algorithm. It will become the outline for fog of war in a basic 2D game.
Instead of drawing a line or filling a pixel I am changing values in a 2D vector. This works correctly and the locations in mapMatrix
that are changed in this code display correctly.
However only the bottom and top sections of the circle appear. How do I fill in the gaps?
int d = 3 - (2 * radius);
int x = 0;
int y = radius;
while (x <= y)
{
mapMatrix[centerX + x][centerY + y].fog = false;
mapMatrix[centerX + x][centerY - y].fog = false;
mapMatrix[centerX - x][centerY + y].fog = false;
mapMatrix[centerX - x][centerY - y].fog = false;
mapMatrix[centerX + x][centerY + y].fog = false;
mapMatrix[centerX + x][centerY - y].fog = false;
mapMatrix[centerX - x][centerY + y].fog = false;
mapMatrix[centerX - x][centerY - y].fog = false;
if (d < 0)
{
d = (d + (4*x) + 6);
}
else
{
d = ((d + 4 * (x - y)) + 10);
y--;
}
x++;
}
I can put a picture of my output so please see this crude ASCII drawing.
-------------
----ooooo----
---o-----o---
-------------
-------------
-------------
------o------
-------------
-------------
-------------
---o-----o---
----ooooo----
-------------
Thanks in advance!
A more complete answer:
You are looping while (x<=y). that means that the last iteration is when x == y. but x == y only on the diagonal so that is where you stop:
x------------
-x--ooooo----
--xo-----o---
---x---------
----x--------
-----x-------
------x------
-------x-----
--------x----
---------x---
---o-----ox--
----ooooo--x-
------------x
You are not iterating on the circle, you are only iterating on a line (x), and you calculate top and bottom y's.
Note that there are only two y's for every x and, near the end, you would need more y's for every x. This is why you need to iterate again but this time on a column (y) and calculate two x's for every y, basically switching x and y in the above algorithm.