//Draws a circle on the bitmap using Bresenham public static PixelMatrix BresenhamCircle(PixelMatrix bitmap, int centerX, int centerY, int radius, Color color) { int d = (5 - radius * 4) / 4; int x = 0; int y = radius; do { bitmap.SetPixel(centerX + x, centerY + y, color); bitmap.SetPixel(centerX + x, centerY - y, color); bitmap.SetPixel(centerX - x, centerY + y, color); bitmap.SetPixel(centerX - x, centerY - y, color); bitmap.SetPixel(centerX + y, centerY + x, color); bitmap.SetPixel(centerX + y, centerY - x, color); bitmap.SetPixel(centerX - y, centerY + x, color); bitmap.SetPixel(centerX - y, centerY - x, color); if (d < 0) { d += 2 * x + 1; } else { d += 2 * (x - y) + 1; y--; } x++; } while (x <= y); return(bitmap); }
//Draws a line on the bitmap using Bresenham public static PixelMatrix BresenhamLine(PixelMatrix bitmap, int x0, int y0, int x1, int y1, Color color) { int dx = Mathf.Abs(x1 - x0), sx = x0 < x1 ? 1 : -1; int dy = Mathf.Abs(y1 - y0), sy = y0 < y1 ? 1 : -1; int err = (dx > dy ? dx : -dy) / 2, e2; do { bitmap.SetPixel(x0, y0, color); if (x0 == x1 && y0 == y1) { break; } e2 = err; if (e2 > -dx) { err -= dy; x0 += sx; } if (e2 < dy) { err += dx; y0 += sy; } } while (true); return(bitmap); }
//Add a decal to an image public static PixelMatrix Decal(PixelMatrix original, PixelMatrix decal, int x, int y) { //Offsets to apply int widthOffset = -decal.width / 2 + x; int heightOffset = -decal.height / 2 + y; for (int i = 0; i < decal.width; i++) { //X offset int finalX = i + widthOffset; //Skip column if out of original if (finalX < 0 || finalX > original.width - 1) { continue; } for (int j = 0; j < decal.height; j++) { //Get color Color cl = decal.GetPixelSafe(i, j); //Skip transparent pixels //TODO Blend with transparency? if (cl.a < 0.7) { continue; } //Y offset int finalY = j + heightOffset; //Apply color original.SetPixel(finalX, finalY, cl); } } return(original); }