Thick Lines
We can make the line thicker by drawing an additional pixel along the minor axis of the line. This method works well with lines up to a thickness of 3. After that, the ends of the line become visually jarring, as they are alway horizontal or vertical.
void Line(Image& image, int x0, int y0, int x1, int y1, Color& val) { int dx = abs(x1 - x0); int dy = abs(y1 - y0); int xStep = x0 < x1 ? 1 : -1; int yStep = y0 < y1 ? 1 : -1; int error = 0; if (dx > dy) { int m = 2 * dy; int scale = 2 * dx; for (int x = x0, y = y0; x != x1 + xStep; x += xStep) { PutPixel(image, x, y, val); PutPixel(image, x, y + yStep, val); // 2 px thick error += m; if (error >= dx) { y += yStep; error -= scale; } } } else { int m = 2 * dx; int scale = 2 * dy; for (int y = y0, x = x0; y != y1 + yStep; y += yStep) { PutPixel(image, x, y, val); PutPixel(image, x + xStep, y, val); // 2 px thick error += m; if (error >= dy) { x += xStep; error -= scale; } } } }
Really Thick Lines
To make thicker lines that don't look jarring we need to stamp a pattern at each pixel. Any time the x or y coordinate changes, the stamp should be repeated to avoid missing pixels.
// Stamps some pattern at x / y coord void Stamp(Image& image, int x, int y, Color& col); void Line(Image& image, int x0, int y0, int x1, int y1, Color& val) { int absXDelta = abs(x1 - x0); int absYDelta = abs(y1 - y0); int steps = max(absXDelta, absYDelta); int xDir = x0 < x1 ? 1 : -1; int yDir = y0 < y1 ? 1 : -1; int x = x0, xNumerator = 0; int y = y0, yNumerator = 0; for (int i = 0; i <= steps; ++i) { Stamp(image, x, y, val) xNumerator += absXDelta; if (xNumerator >= steps) { x += xDir; xNumerator -= steps; Stamp(image, x, y, val) } yNumerator += absYDelta; if (yNumerator >= steps) { y += yDir; yNumerator -= steps; Stamp(image, x, y, val) } } }