Этап 7
This commit is contained in:
43
Minint.Core/Services/Impl/DrawingService.cs
Normal file
43
Minint.Core/Services/Impl/DrawingService.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Minint.Core.Models;
|
||||
|
||||
namespace Minint.Core.Services.Impl;
|
||||
|
||||
public sealed class DrawingService : IDrawingService
|
||||
{
|
||||
public void ApplyBrush(MinintLayer layer, int cx, int cy, int radius, int colorIndex, int width, int height)
|
||||
{
|
||||
foreach (var (x, y) in GetBrushMask(cx, cy, radius, width, height))
|
||||
layer.Pixels[y * width + x] = colorIndex;
|
||||
}
|
||||
|
||||
public void ApplyEraser(MinintLayer layer, int cx, int cy, int radius, int width, int height)
|
||||
{
|
||||
foreach (var (x, y) in GetBrushMask(cx, cy, radius, width, height))
|
||||
layer.Pixels[y * width + x] = 0;
|
||||
}
|
||||
|
||||
public List<(int X, int Y)> GetBrushMask(int cx, int cy, int radius, int width, int height)
|
||||
{
|
||||
var mask = new List<(int, int)>();
|
||||
int r = Math.Max(radius, 0);
|
||||
int r2 = r * r;
|
||||
|
||||
int xMin = Math.Max(0, cx - r);
|
||||
int xMax = Math.Min(width - 1, cx + r);
|
||||
int yMin = Math.Max(0, cy - r);
|
||||
int yMax = Math.Min(height - 1, cy + r);
|
||||
|
||||
for (int py = yMin; py <= yMax; py++)
|
||||
{
|
||||
int dy = py - cy;
|
||||
for (int px = xMin; px <= xMax; px++)
|
||||
{
|
||||
int dx = px - cx;
|
||||
if (dx * dx + dy * dy <= r2)
|
||||
mask.Add((px, py));
|
||||
}
|
||||
}
|
||||
|
||||
return mask;
|
||||
}
|
||||
}
|
||||
47
Minint.Core/Services/Impl/FloodFillService.cs
Normal file
47
Minint.Core/Services/Impl/FloodFillService.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Minint.Core.Models;
|
||||
|
||||
namespace Minint.Core.Services.Impl;
|
||||
|
||||
public sealed class FloodFillService : IFloodFillService
|
||||
{
|
||||
public void Fill(MinintLayer layer, int x, int y, int newColorIndex, int width, int height)
|
||||
{
|
||||
if (x < 0 || x >= width || y < 0 || y >= height)
|
||||
return;
|
||||
|
||||
var pixels = layer.Pixels;
|
||||
int targetIndex = pixels[y * width + x];
|
||||
|
||||
if (targetIndex == newColorIndex)
|
||||
return;
|
||||
|
||||
var queue = new Queue<(int X, int Y)>();
|
||||
var visited = new bool[width * height];
|
||||
|
||||
queue.Enqueue((x, y));
|
||||
visited[y * width + x] = true;
|
||||
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var (cx, cy) = queue.Dequeue();
|
||||
pixels[cy * width + cx] = newColorIndex;
|
||||
|
||||
Span<(int, int)> neighbors =
|
||||
[
|
||||
(cx - 1, cy), (cx + 1, cy),
|
||||
(cx, cy - 1), (cx, cy + 1)
|
||||
];
|
||||
|
||||
foreach (var (nx, ny) in neighbors)
|
||||
{
|
||||
if (nx < 0 || nx >= width || ny < 0 || ny >= height)
|
||||
continue;
|
||||
int ni = ny * width + nx;
|
||||
if (visited[ni] || pixels[ni] != targetIndex)
|
||||
continue;
|
||||
visited[ni] = true;
|
||||
queue.Enqueue((nx, ny));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user