1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| auto fpart = [](float x) { return x - floorf(x); }; auto rfpart = [&fpart](float x) { return 1 - fpart(x); }; int x0 = line[0][0], y0 = line[0][1], x1 = line[1][0], y1 = line[1][1]; bool steep = abs(y1 - y0) > abs(x1 - x0); if (steep) { swap(x0, y0); swap(x1, y1); } if (x0 > x1) { swap(x0, x1); swap(y0, y1); } float dx = x1 - x0; float dy = abs(y1 - y0); float gradient = dy / dx; if (dx == 0.0) { gradient = 1.0; }
int xend = round(x0); int yend = y0 + gradient * (xend - x0);
float xgap = rfpart(x0 + 0.5f); int xpxl1 = xend; int ypxl1 = (int)yend;
if (steep) { image.set_white_alpha(ypxl1, xpxl1, rfpart(yend) * xgap); image.set_white_alpha(ypxl1 + 1, xpxl1, fpart(yend) * xgap); } else { image.set_white_alpha(xpxl1, ypxl1, rfpart(yend) * xgap); image.set_white_alpha(xpxl1, ypxl1 + 1, fpart(yend) * xgap); } float intery = yend + gradient;
xend = round(x1); yend = y1 + gradient * (xend - x1); xgap = fpart(x1 + 0.5); int xpxl2 = xend; int ypxl2 = (int)(yend); if (steep) { image.set_white_alpha(ypxl2, xpxl2, rfpart(yend) * xgap); image.set_white_alpha(ypxl2 + 1, xpxl2, fpart(yend) * xgap); } else { image.set_white_alpha(xpxl2, ypxl2, rfpart(yend) * xgap); image.set_white_alpha(xpxl2, ypxl2 + 1, fpart(yend) * xgap); }
if (steep) { for (int x = xpxl1 + 1; x <= xpxl2 - 1; x++) { image.set_white_alpha((int)(intery), x, rfpart(intery)); image.set_white_alpha((int)(intery) + 1, x, fpart(intery)); intery = intery + gradient; } } else { for (int x = xpxl1 + 1; x <= xpxl2 - 1; x++) { image.set_white_alpha(x, (int)(intery), rfpart(intery)); image.set_white_alpha(x, (int)(intery) + 1, fpart(intery)); intery = intery + gradient; } }
|