Skip to content

Commit b7f50b2

Browse files
authored
[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
1 parent 2ee6a76 commit b7f50b2

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/rtextures.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3506,14 +3506,15 @@ void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int en
35063506
}
35073507

35083508
// Initialize variables for drawing loop
3509-
int endVal = longLen;
3509+
int endVal = longLen + 1;
35103510
int sgnInc = 1;
35113511

35123512
// Adjust direction increment based on longLen sign
35133513
if (longLen < 0)
35143514
{
35153515
longLen = -longLen;
35163516
sgnInc = -1;
3517+
endVal -= 2;
35173518
}
35183519

35193520
// Calculate fixed-point increment for shorter length
@@ -3523,7 +3524,8 @@ void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int en
35233524
if (yLonger)
35243525
{
35253526
// If line is more vertical, iterate over y-axis
3526-
for (int i = 0, j = 0; i != endVal; i += sgnInc, j += decInc)
3527+
// Init j with 0.5 in 16-bit fixed point (1 << 15) for better rounding.
3528+
for (int i = 0, j = (1 << 15); i != endVal; i += sgnInc, j += decInc)
35273529
{
35283530
// Calculate pixel position and draw it
35293531
ImageDrawPixel(dst, startPosX + (j >> 16), startPosY + i, color);
@@ -3532,7 +3534,7 @@ void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int en
35323534
else
35333535
{
35343536
// If line is more horizontal, iterate over x-axis
3535-
for (int i = 0, j = 0; i != endVal; i += sgnInc, j += decInc)
3537+
for (int i = 0, j = (1 << 15); i != endVal; i += sgnInc, j += decInc)
35363538
{
35373539
// Calculate pixel position and draw it
35383540
ImageDrawPixel(dst, startPosX + i, startPosY + (j >> 16), color);

0 commit comments

Comments
 (0)