DoogeJ / MonoGame.Primitives2D

Easy-to-use 2D primitives
https://www.nuget.org/packages/MonoGame.Primitives2D/
zlib License
54 stars 12 forks source link

Add an option to fill circles #4

Open DoogeJ opened 4 years ago

DoogeJ commented 4 years ago

DrawCircle has no way of filling circles.

This should be implemented in the same way as #3

differenceclouds commented 1 year ago

I added this, adapted from http://fredericgoset.ovh/mathematiques/courbes/en/filled_circle.html. That author goes to further steps to increase efficiency, so it's probably worth taking a look at. Of course it's still a separate method, not combined.

public static void FillCircle(this SpriteBatch spriteBatch, Vector2 center, float radius, Color color)
{
    float x = 0;
    float y = radius;
    float m = 5 - 4 * radius;

    while (x <= y) {
        DrawLine(spriteBatch, center.X - x, center.Y - y, center.X + x, center.Y - y, color);
        DrawLine(spriteBatch, center.X - y, center.Y - x, center.X + y, center.Y - x, color);
        DrawLine(spriteBatch, center.X - y, center.Y + x, center.X + y, center.Y + x, color);
        DrawLine(spriteBatch, center.X - x, center.Y + y, center.X + x, center.Y + y, color);

        if (m > 0) {
            y--;
            m -= 8 * y;
        }
        x++;
        m += 8 * x + 4;
    }
}