Created
January 29, 2020 14:59
-
-
Save Eisenwave/34580de5b81535f1621561c5c7c9439d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constexpr size_t indexOf(int x, int y, int stride) | |
{ | |
return static_cast<size_t>(x + y * stride); | |
} | |
template <typename RGB> | |
void bresenhamDrawLineUnsafe(RGB *buffer, int w, int h, int x0, int y0, int x1, int y1, RGB rgb) | |
{ | |
const auto dx = x1 - x0, dy = y1 - y0; | |
const auto dxSign = mve::math::sgn(dx), dySign = mve::math::sgn(dy); | |
const auto dxAbs = std::abs(dx), dyAbs = std::abs(dy); | |
const auto dmax = std::max(dx, dy, [](auto lhs, auto rhs) { | |
return std::abs(lhs) < std::abs(rhs); | |
}); | |
auto err = std::abs(dmax / 2); | |
#define DRAW(x, y) buffer[indexOf(x, y, w)] = rgb; | |
if (dmax == dx) { | |
for (int x = x0, y = y0; x != x1; x += dxSign) { | |
DRAW(x, y); | |
err -= dyAbs; | |
if (err < 0) { | |
err += dxAbs; | |
y += dySign; | |
} | |
} | |
} | |
else if (dmax == dy) { | |
for (int x = x0, y = y0; y != y1; y += dySign) { | |
DRAW(x, y); | |
err -= dxAbs; | |
if (err < 0) { | |
err += dyAbs; | |
x += dxSign; | |
} | |
} | |
} | |
#undef DRAW | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment