Skip to content

Commit

Permalink
Circle drawing alignment fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianstevens committed Oct 28, 2023
1 parent f9536fa commit 9ff63a1
Showing 1 changed file with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public partial class MicroGraphics
/// <summary>
/// PixelBuffer draw target
/// </summary>
protected IPixelBuffer PixelBuffer => (display != null) ? display.PixelBuffer : _memoryBuffer;
private readonly IPixelBuffer _memoryBuffer = default!;
protected IPixelBuffer PixelBuffer => (display != null) ? display.PixelBuffer : memoryBuffer;
private readonly IPixelBuffer memoryBuffer = default!;

/// <summary>
/// ignore pixels that are outside of the pixel buffer coordinate space
Expand Down Expand Up @@ -123,7 +123,7 @@ public int Width
/// </summary>
public TimeSpan DelayBetweenFrames { get; set; } = TimeSpan.Zero;

private readonly object _lock = new object();
private readonly object _lock = new();
private bool isUpdating = false;
private bool isUpdateRequested = false;

Expand All @@ -148,7 +148,7 @@ public MicroGraphics(IGraphicsDisplay display)
/// <param name="initializeBuffer">Initialize the off-screen buffer if true</param>
public MicroGraphics(PixelBufferBase pixelBuffer, bool initializeBuffer)
{
_memoryBuffer = pixelBuffer;
memoryBuffer = pixelBuffer;

if (initializeBuffer)
{
Expand Down Expand Up @@ -810,11 +810,18 @@ public void DrawCircle(int centerX, int centerY, int radius, Color color, bool f
}
else
{
int offset = Stroke >> 1;

for (int i = 0; i < Stroke; i++)
if (Stroke == 1)
{
DrawCircleOutline(centerX, centerY, radius - offset + i, centerBetweenPixels, color);
DrawCircleOutline(centerX, centerY, radius, centerBetweenPixels, color);
}
else
{
int offset = (Stroke - 1) >> 1;

for (int i = 0; i < Stroke; i++)
{
DrawCircleOutline(centerX, centerY, radius - offset + i, centerBetweenPixels, color);
}
}
}
}
Expand Down Expand Up @@ -1018,12 +1025,21 @@ private void DrawCircleOutline(int centerX, int centerY, int radius, bool center

private void DrawCircleFilled(int centerX, int centerY, int radius, bool centerBetweenPixels, Color color)
{
if (Stroke > 1)
{
radius += Stroke >> 1;
}

var d = 3 - (2 * radius);
var x = 0;
var y = radius;

int offset = centerBetweenPixels ? 1 : 0;

//override the stroke behavior
var stroke = Stroke;
Stroke = 1;

while (x <= y)
{
DrawHorizontalLine(centerX - x, centerY + y - offset, (2 * x) - offset + 1, color);
Expand All @@ -1043,15 +1059,7 @@ private void DrawCircleFilled(int centerX, int centerY, int radius, bool centerB
x++;
}

if (Stroke > 1)
{
offset = Stroke >> 1;

for (int i = 0; i < Stroke; i++)
{
DrawCircleOutline(centerX, centerY, radius - offset + i, centerBetweenPixels, color);
}
}
Stroke = stroke;
}

/// <summary>
Expand Down

0 comments on commit 9ff63a1

Please sign in to comment.