[rtextures] ImageDrawLine() rounding and length fix (#5896)

* 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.

* Changed ImagDrawLine to be more acurate
This commit is contained in:
Kaggen67
2026-05-30 19:39:16 +02:00
committed by GitHub
parent 2ee6a76464
commit b7f50b2fb6

View File

@@ -3506,7 +3506,7 @@ void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int en
}
// Initialize variables for drawing loop
int endVal = longLen;
int endVal = longLen + 1;
int sgnInc = 1;
// 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;
sgnInc = -1;
endVal -= 2;
}
// Calculate fixed-point increment for shorter length
@@ -3523,7 +3524,8 @@ void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int en
if (yLonger)
{
// If line is more vertical, iterate over y-axis
for (int i = 0, j = 0; i != endVal; i += sgnInc, j += decInc)
// Init j with 0.5 in 16-bit fixed point (1 << 15) for better rounding.
for (int i = 0, j = (1 << 15); i != endVal; i += sgnInc, j += decInc)
{
// Calculate pixel position and draw it
ImageDrawPixel(dst, startPosX + (j >> 16), startPosY + i, color);
@@ -3532,7 +3534,7 @@ void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int en
else
{
// 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
ImageDrawPixel(dst, startPosX + i, startPosY + (j >> 16), color);