Implemented remaining 7/8 of ImageDrawLine
The existing code was correct for one octant, it now works for all 8 Added two internal functions, _ImageDrawLineHorizontal and _ImageDrawLineVertical, whithout which it would've been 4 times as much code.
This commit is contained in:
parent
59acb6dbea
commit
be83329b2d
105
src/textures.c
105
src/textures.c
|
|
@ -2415,21 +2415,102 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw line within an image
|
void _ImageDrawLineHorizontal(Image *dst, int startPosX, int startPosY, int endPosX, int endPosY, Color color){
|
||||||
void ImageDrawLine(Image *dst, int startPosX, int startPosY, int endPosX, int endPosY, Color color)
|
|
||||||
{
|
|
||||||
int m = 2*(endPosY - startPosY);
|
|
||||||
int slopeError = m - (endPosX - startPosX);
|
|
||||||
|
|
||||||
for (int x = startPosX, y = startPosY; x <= endPosX; x++)
|
int changeInX = (endPosX - startPosX);
|
||||||
|
int abs_changeInX = changeInX < 0 ? -changeInX : changeInX;
|
||||||
|
int changeInY = (endPosY - startPosY);
|
||||||
|
int abs_changeInY = changeInY < 0 ? -changeInY : changeInY;
|
||||||
|
|
||||||
|
// we need to go up or down depending (-1 or 1);
|
||||||
|
int y_directional_unit = changeInY < 0 ? -1 : 1;
|
||||||
|
|
||||||
|
int A = 2* abs_changeInY;
|
||||||
|
int B = A - 2* abs_changeInX;
|
||||||
|
int P = A - abs_changeInX;
|
||||||
|
|
||||||
|
ImageDrawPixel(dst, startPosX, startPosY, color);
|
||||||
|
|
||||||
|
for (int x = startPosX+1, y = startPosY; x <= endPosX; x += 1)
|
||||||
{
|
{
|
||||||
ImageDrawPixel(dst, x, y, color);
|
if (P >= 0)
|
||||||
slopeError += m;
|
|
||||||
|
|
||||||
if (slopeError >= 0)
|
|
||||||
{
|
{
|
||||||
y++;
|
y += y_directional_unit;
|
||||||
slopeError -= 2*(endPosX - startPosX);
|
P += B;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
P += A;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImageDrawPixel(dst, x, y, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _ImageDrawLineVertical(Image *dst, int startPosX, int startPosY, int endPosX, int endPosY, Color color){
|
||||||
|
// same code as *Horizontal but with all x and y flipped
|
||||||
|
|
||||||
|
int changeInX = (endPosX - startPosX);
|
||||||
|
int abs_changeInX = changeInX < 0 ? -changeInX : changeInX;
|
||||||
|
int changeInY = (endPosY - startPosY);
|
||||||
|
int abs_changeInY = changeInY < 0 ? -changeInY : changeInY;
|
||||||
|
|
||||||
|
// we need to go left or right depending (-1 or 1)
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if (P >= 0)
|
||||||
|
{
|
||||||
|
x += x_directional_unit;
|
||||||
|
P += B;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
P += A;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImageDrawPixel(dst, x, y, 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 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)
|
||||||
|
{
|
||||||
|
_ImageDrawLineHorizontal(dst, startPosX, startPosY, endPosX, endPosY, color);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_ImageDrawLineHorizontal(dst, endPosX, endPosY, startPosX, startPosY, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (changeInY > 0)
|
||||||
|
{
|
||||||
|
_ImageDrawLineVertical(dst, startPosX, startPosY, endPosX, endPosY, color);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_ImageDrawLineVertical(dst, endPosX, endPosY, startPosX, startPosY, color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user