Fixing ImageDrawLine to be more pixel accurate

Changes to ImageDrawLine() function.
. Added one pixel to the length of a line so it wont draw lines one pixel short.
. Changed rounding by adding 0.5 in 16 bit fixed point to 'j' in for-loop.
This commit is contained in:
Kaggen67 2026-05-18 21:43:15 +02:00 committed by GitHub
parent c04e57399a
commit f9197573e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3506,7 +3506,7 @@ void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int en
} }
// Initialize variables for drawing loop // Initialize variables for drawing loop
int endVal = longLen; int endVal = longLen + 1;
int sgnInc = 1; int sgnInc = 1;
// Adjust direction increment based on longLen sign // Adjust direction increment based on longLen sign
@ -3514,6 +3514,7 @@ void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int en
{ {
longLen = -longLen; longLen = -longLen;
sgnInc = -1; sgnInc = -1;
endVal -= 2;
} }
// Calculate fixed-point increment for shorter length // Calculate fixed-point increment for shorter length
@ -3523,7 +3524,7 @@ void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int en
if (yLonger) if (yLonger)
{ {
// If line is more vertical, iterate over y-axis // If line is more vertical, iterate over y-axis
for (int i = 0, j = 0; i != endVal; i += sgnInc, j += decInc) for (int i = 0, j = (1 << 15); i != endVal; i += sgnInc, j += decInc)
{ {
// Calculate pixel position and draw it // Calculate pixel position and draw it
ImageDrawPixel(dst, startPosX + (j >> 16), startPosY + i, color); ImageDrawPixel(dst, startPosX + (j >> 16), startPosY + i, color);
@ -3532,7 +3533,7 @@ void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int en
else else
{ {
// If line is more horizontal, iterate over x-axis // If line is more horizontal, iterate over x-axis
for (int i = 0, j = 0; i != endVal; i += sgnInc, j += decInc) for (int i = 0, j = (1 << 15); i != endVal; i += sgnInc, j += decInc)
{ {
// Calculate pixel position and draw it // Calculate pixel position and draw it
ImageDrawPixel(dst, startPosX + i, startPosY + (j >> 16), color); ImageDrawPixel(dst, startPosX + i, startPosY + (j >> 16), color);