From f9197573e48ac3531941eecc1d3f002d606cd9e0 Mon Sep 17 00:00:00 2001 From: Kaggen67 Date: Mon, 18 May 2026 21:43:15 +0200 Subject: [PATCH] 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. --- src/rtextures.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/rtextures.c b/src/rtextures.c index ba50d9d02..07e99a37b 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -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,7 @@ 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) + 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 +3533,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);