Skip to content

Commit f34e240

Browse files
authored
Merge pull request #5042 from Sir-Irk/ImageDrawLineEx_fix
[rtextures] Fix `ImageDrawLineEx()` line thickness
2 parents 20a07a6 + fce3102 commit f34e240

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/rtextures.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3565,34 +3565,43 @@ void ImageDrawLineEx(Image *dst, Vector2 start, Vector2 end, int thick, Color co
35653565
int dx = x2 - x1;
35663566
int dy = y2 - y1;
35673567

3568-
// Draw the main line between (x1, y1) and (x2, y2)
3569-
ImageDrawLine(dst, x1, y1, x2, y2, color);
3570-
35713568
// Determine if the line is more horizontal or vertical
35723569
if ((dx != 0) && (abs(dy/dx) < 1))
35733570
{
35743571
// Line is more horizontal
3575-
// Calculate half the width of the line
3576-
int wy = (thick - 1)*(int)sqrtf((float)(dx*dx + dy*dy))/(2*abs(dx));
35773572

3578-
// Draw additional lines above and below the main line
3579-
for (int i = 1; i <= wy; i++)
3573+
// How many additional lines to draw
3574+
int wy = thick - 1;
3575+
3576+
// Draw the main line and lower half
3577+
for (int i = 0; i <= ((wy+1)/2); i++)
35803578
{
3581-
ImageDrawLine(dst, x1, y1 - i, x2, y2 - i, color); // Draw above the main line
3582-
ImageDrawLine(dst, x1, y1 + i, x2, y2 + i, color); // Draw below the main line
3579+
ImageDrawLine(dst, x1, y1 + i, x2, y2 + i, color);
3580+
}
3581+
3582+
// Draw the upper half
3583+
for (int i = 1; i <= (wy/2); i++)
3584+
{
3585+
ImageDrawLine(dst, x1, y1 - i, x2, y2 - i, color);
35833586
}
35843587
}
35853588
else if (dy != 0)
35863589
{
35873590
// Line is more vertical or perfectly horizontal
3588-
// Calculate half the width of the line
3589-
int wx = (thick - 1)*(int)sqrtf((float)(dx*dx + dy*dy))/(2*abs(dy));
3591+
3592+
// How many additional lines to draw
3593+
int wx = thick - 1;
3594+
3595+
//Draw the main line and right half
3596+
for (int i = 0; i <= ((wx+1)/2); i++)
3597+
{
3598+
ImageDrawLine(dst, x1 + i, y1, x2 + i, y2, color);
3599+
}
35903600

3591-
// Draw additional lines to the left and right of the main line
3592-
for (int i = 1; i <= wx; i++)
3601+
// Draw the left half
3602+
for (int i = 1; i <= (wx/2); i++)
35933603
{
3594-
ImageDrawLine(dst, x1 - i, y1, x2 - i, y2, color); // Draw left of the main line
3595-
ImageDrawLine(dst, x1 + i, y1, x2 + i, y2, color); // Draw right of the main line
3604+
ImageDrawLine(dst, x1 - i, y1, x2 - i, y2, color);
35963605
}
35973606
}
35983607
}

0 commit comments

Comments
 (0)