Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 30, 2026

Integer division and insufficient bit precision cause endpoint errors on large bitmaps. Drawing from (0,0) to (29999,9999) on a 30000×10000 bitmap produces a 39-pixel error in DrawLine and 1-pixel error in DrawLineAa.

Changes

DrawLine

  • Add EXTRA_PRECISION_SHIFT = 16 for slope calculations (24-bit total precision vs 8-bit)
  • Change slope and accumulator variables from int to long to prevent overflow
  • Apply to both x-dominant and y-dominant code paths

DrawLineAa

  • Increase PRECISION_SHIFT from 10 to 18 to prevent invD rounding to zero
  • Add overflow protection: (long)du * du + (long)dv * dv
  • Promote intermediate calculation variables to long

Root Causes

DrawLine: Slope calculation (dy << 8) / dx loses fractional bits. For dy=9999, dx=29999, true slope is 85.326 but truncates to 85. Over 30000 iterations: error = 0.326 × 30000 / 256 ≈ 39 pixels.

DrawLineAa: For long lines, invDFloat ≈ 0.000008. At PRECISION_SHIFT=10, (int)(0.000008 × 1024) = 0, breaking anti-aliasing. At PRECISION_SHIFT=18, (int)(0.000008 × 262144) = 2.

// Before (loses precision)
int incy = (dy << PRECISION_SHIFT) / dx;  // 8-bit precision

// After (maintains precision)
const int EXTRA_PRECISION_SHIFT = 16;
long incy = ((long)dy << (PRECISION_SHIFT + EXTRA_PRECISION_SHIFT)) / dx;  // 24-bit precision
Original prompt

This section details on the original issue you should resolve

<issue_title>DrawLine and DrawLineAa don't draw correctly on larger WriteableBitmaps</issue_title>
<issue_description>Drawing a line over large distances with DrawLine, the end will be off. In the example below there is a difference of 39 pixels in height.
Drawing a line with DrawLineAa, the end will be of by 1 in width and height.

 WriteableBitmap image = new WriteableBitmap(30000, 10000, 96, 96, PixelFormats.Pbgra32, null);
 image.Clear(Colors.White);
 image.DrawLine(0, 0, 29999, 9999, Colors.Black);
 image.DrawLineAa(0, 0, 29999, 9999, Colors.Red);

Image</issue_description>

<agent_instructions>First validate the issue and then go ahead and fix it. Make sure to keep performance in mind and only make minimal changes. Take your time and test all well. </agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: reneschulte <7439129+reneschulte@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix DrawLine and DrawLineAa issues on large WriteableBitmaps Fix precision loss in DrawLine and DrawLineAa for large bitmaps Jan 30, 2026
Copilot AI requested a review from reneschulte January 30, 2026 11:14
@reneschulte reneschulte marked this pull request as ready for review January 30, 2026 11:14
@reneschulte reneschulte merged commit 09f0497 into master Feb 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DrawLine and DrawLineAa don't draw correctly on larger WriteableBitmaps

2 participants