I made a program in C++ with SDL2 which draws a curve from a list of points by linking them with lines. I'm using the SDL_RenderDrawLine function, which is based on Bresenham's algorithm, I assume.
Then I made an other function based on Xiaolin Wu's algorithm to replace the SDL one (because it's smoother). It works well, but it is much (much!) slower than the SDL function. In fact I struggle to draw more than a dozen of lines per second, wheras I can draw thousands of lines per second with SDL function without any problem.
So I am looking for an SDL Library where Xiaolin Wu's algorithm is already implemented, because computer scientists are of course better than me. Does this Library exists ? Or do I have to do it myself ?
If I have to do it myself, do you have tips to improve my function ? Here it is, based on Wikipedia pseudo-code :
void drawXWLine(SDL_Renderer *renderer, double x1, double y1, double x2, double y2, SDL_Color color, uint8_t a)
{
double dx = x2 - x1;
double dy = y2 - y1;
if (abs(dx) > abs(dy)) // horizontal lines
{
if (x2 < x1)
{
// swap the two points
}
double gradient = dy / dx;
// draw the extreme points of the line
int xend, xpxl1, xpxl2, ypxl1, ypxl2;
double yend, xgap, intery;
xend = round(x1);
yend = y1 + gradient * (xend - x1);
xgap = 1.0 - mod(x1 + 0.5, 1.0);
xpxl1 = xend;
ypxl1 = int(yend);
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, uint8_t((1.0 - mod(yend, 1.0)) * xgap * a));
SDL_RenderDrawPoint(renderer, xpxl1, ypxl1);
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, uint8_t(mod(yend, 1.0) * xgap * a));
SDL_RenderDrawPoint(renderer, xpxl1, ypxl1+1);
intery = yend + gradient;
xend = round(x2);
yend = y2 + gradient * (xend - x2);
xgap = 1.0 - mod(x2 + 0.5, 1.0);
xpxl2 = xend;
ypxl2 = int(yend);
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, uint8_t((1.0 - mod(yend, 1.0)) * xgap * a));
SDL_RenderDrawPoint(renderer, xpxl2, ypxl2);
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, uint8_t(mod(yend, 1.0) * xgap * a));
SDL_RenderDrawPoint(renderer, xpxl2, ypxl2+1);
// draw the whole line
for(int x = xpxl1 + 1 ; x < xpxl2 ; x++)
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, uint8_t((1.0 - mod(intery, 1.0)) * xgap * a));
SDL_RenderDrawPoint(renderer, x, int(intery));
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, uint8_t(mod(intery, 1.0) * xgap * a));
SDL_RenderDrawPoint(renderer, x, int(intery)+1);
intery += gradient;
}
}
else
{
// do the same with vertical lines
}
}