ImageDrawLine: Replaced 3 functions with 1

Removed both freshly added internal functions
Crammed it all into one
This commit is contained in:
Alexander Buhl 2021-07-20 21:46:09 +02:00 committed by GitHub
parent be83329b2d
commit 6a01c3f31a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2415,13 +2415,21 @@ void ImageDrawPixelV(Image *dst, Vector2 position, Color color)
ImageDrawPixel(dst, (int)position.x, (int)position.y, color); ImageDrawPixel(dst, (int)position.x, (int)position.y, color);
} }
void _ImageDrawLineHorizontal(Image *dst, int startPosX, int startPosY, int endPosX, int endPosY, Color color){ // Draw line within an image
void ImageDrawLine (Image *dst, int startPosX, int startPosY, int endPosX, int endPosY, Color color){
// Using Bresenham's algorithm as described in
// Drawing Lines with Pixels - Joshua Scott - March 2012
// https://classic.csunplugged.org/wp-content/uploads/2014/12/Lines.pdf
int changeInX = (endPosX - startPosX); int changeInX = (endPosX - startPosX);
int abs_changeInX = changeInX < 0 ? -changeInX : changeInX; int abs_changeInX = changeInX < 0 ? -changeInX : changeInX;
int changeInY = (endPosY - startPosY); int changeInY = (endPosY - startPosY);
int abs_changeInY = changeInY < 0 ? -changeInY : changeInY; int abs_changeInY = changeInY < 0 ? -changeInY : changeInY;
if (abs_changeInY < abs_changeInX)
{
if (changeInX > 0)
{
// we need to go up or down depending (-1 or 1); // we need to go up or down depending (-1 or 1);
int y_directional_unit = changeInY < 0 ? -1 : 1; int y_directional_unit = changeInY < 0 ? -1 : 1;
@ -2442,20 +2450,49 @@ void _ImageDrawLineHorizontal(Image *dst, int startPosX, int startPosY, int endP
{ {
P += A; P += A;
} }
ImageDrawPixel(dst, x, y, color); ImageDrawPixel(dst, x, y, color);
} }
} }
else
{
// doing the equivalent of "calling" the branch above with inverted parameters:
changeInX = -changeInX;
changeInY = -changeInY;
int help;
help = startPosX;
startPosX = endPosX;
endPosX = help;
help = startPosY;
startPosY = endPosY;
endPosY = help;
void _ImageDrawLineVertical(Image *dst, int startPosX, int startPosY, int endPosX, int endPosY, Color color){ int y_directional_unit = changeInY < 0 ? -1 : 1;
// same code as *Horizontal but with all x and y flipped
int changeInX = (endPosX - startPosX); int A = 2* abs_changeInY;
int abs_changeInX = changeInX < 0 ? -changeInX : changeInX; int B = A - 2* abs_changeInX;
int changeInY = (endPosY - startPosY); int P = A - abs_changeInX;
int abs_changeInY = changeInY < 0 ? -changeInY : changeInY;
// we need to go left or right depending (-1 or 1) ImageDrawPixel(dst, startPosX, startPosY, color);
for (int x = startPosX+1, y = startPosY; x <= endPosX; x += 1)
{
if (P >= 0)
{
y += y_directional_unit;
P += B;
}
else
{
P += A;
}
ImageDrawPixel(dst, x, y, color);
}
}
}
else
{
if (changeInY > 0)
{
int x_directional_unit = changeInX < 0 ? -1 : 1; int x_directional_unit = changeInX < 0 ? -1 : 1;
int A = 2* abs_changeInX; int A = 2* abs_changeInX;
@ -2475,42 +2512,43 @@ void _ImageDrawLineVertical(Image *dst, int startPosX, int startPosY, int endPos
{ {
P += A; P += A;
} }
ImageDrawPixel(dst, x, y, color); ImageDrawPixel(dst, x, y, color);
} }
} }
else
// Draw line within an image
void ImageDrawLine (Image *dst, int startPosX, int startPosY, int endPosX, int endPosY, Color color){
// Using Bresenham's algorithm as described in
// Drawing Lines with Pixels - Joshua Scott - March 2012
// https://classic.csunplugged.org/wp-content/uploads/2014/12/Lines.pdf
int changeInX = (endPosX - startPosX);
int abs_changeInX = changeInX < 0 ? -changeInX : changeInX;
int changeInY = (endPosY - startPosY);
int abs_changeInY = changeInY < 0 ? -changeInY : changeInY;
if (abs_changeInY < abs_changeInX)
{ {
if (changeInX > 0) // doing the equivalent of "calling" the branch above with inverted parameters:
changeInX = -changeInX;
changeInY = -changeInY;
int help;
help = startPosX;
startPosX = endPosX;
endPosX = help;
help = startPosY;
startPosY = endPosY;
endPosY = help;
int x_directional_unit = changeInX < 0 ? -1 : 1;
int A = 2* abs_changeInX;
int B = A - 2* abs_changeInY;
int P = A - abs_changeInY;
ImageDrawPixel(dst, startPosX, startPosY, color);
for (int y = startPosY+1, x = startPosX; y <= endPosY; y += 1)
{ {
_ImageDrawLineHorizontal(dst, startPosX, startPosY, endPosX, endPosY, color); if (P >= 0)
{
x += x_directional_unit;
P += B;
} }
else else
{ {
_ImageDrawLineHorizontal(dst, endPosX, endPosY, startPosX, startPosY, color); P += A;
} }
ImageDrawPixel(dst, x, y, color);
} }
else
{
if (changeInY > 0)
{
_ImageDrawLineVertical(dst, startPosX, startPosY, endPosX, endPosY, color);
}
else
{
_ImageDrawLineVertical(dst, endPosX, endPosY, startPosX, startPosY, color);
} }
} }
} }