1
$\begingroup$

I have a grid with a variable width/height spacing. Let's say 100 horizontal lines separated by 80 pixels and 100 hundred vertical lines separated by 50 pixels.

Each cell of the grid in my logic have the same function as a pixel. So each of my pixels will be 50 px width and 100 px height.

I need to draw a circle filled with a color (let's say black) and without stroke.

I also need my circle to (as it is made out of filled cells) be with the less rectangles as possible, so certain rectangles are bigger than other ones.

for example the center of the circle is made out of one big rectangle then as we go closer to the edge the rectangles becomes smaller.

for example here I have circle with a grid of same width/height spacing made out of 9 rectangles (sorry for the quality of the image)

enter image description here Any algorithm to do that?

$\endgroup$

1 Answer 1

1
$\begingroup$

To get a proper circle, you can use a transformation matrix to transform your cell coordinates into equally spaced cartesian coordinates. In the case of your 50 x 100 pixel cells, your transformation matrix would probably either double the width or halve the height. So something like this:

| 0.5 0.0 |
| 0.0 1.0 |

If you want to draw a circle of radius r at a center point (cx, cy), you could iterate over the cells from (cx - r, cy - r) to (cx + r, cy + r) and test if each one is in the circle, and if so, fill it in, otherwise, don't. Something like this pseudocode:

double rSquared = r * r;
for (int row = cy - r; row < cy + r; row++)
{
    for (int col = cx - r; col < cx + r; col++)
    {
        double x = col * transform[0][0] + row * transform[0][1];
        double y = col * transform[1][0] + row * transform[1][1];
        double distSquared = x * x + y * y;
        if (distSquared <= rSquared)
        {
            // inside the circle, fill in this cell (col, row)
        }
        else
        {
            // outside the circle, don't fill in this cell (col, row)
        }
    }
}

As for drawing rectangles, it's not clear what you're trying to accomplish there. There's no need to use rectangles when drawing a circle.

$\endgroup$
2
  • $\begingroup$ thanks for the answer I'll had a closer look to your answer. I need to have rectangles for a reason of weight. I want (like for the example I've shown above) to have 9 rectangles (so 9 {x, y , width, height} coordinates) instead of having a large amount of pixels coordinates or lines (which are also rectangles when you think about it) $\endgroup$
    – JSmith
    Jul 9, 2018 at 7:25
  • $\begingroup$ I've implemente your solution but it doesn't work yet. for the rectangle I was searching an implementation of this $\endgroup$
    – JSmith
    Jul 15, 2018 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.