Untitled
unknown
plain_text
a year ago
1.9 kB
8
Indexable
public void Execute()
{
Unity.Mathematics.Random random = new Unity.Mathematics.Random((uint)Environment.TickCount);
_pixelsCount = Width * Height;
_pixelsPerColor = _pixelsCount / Gradations;
_filledCount = 0;
for (_colorId = 0; _colorId < Gradations; _colorId++)
{
int currentX = random.NextInt(0, Width);
int currentY = random.NextInt(0, Height);
// Запускаємо заповнення з нової позиції
int pixelsFilled = Fill(currentX, currentY, _colorId);
_filledCount += pixelsFilled;
// Якщо заповнили достатньо пікселів, переходимо до наступного кольору
if (_filledCount >= _pixelsPerColor * (_colorId + 1))
{
break; // Виходимо, якщо заповнили всі кольори
}
}
}
private int Fill(int startX, int startY, int colorId)
{
int pixelsFilled = 0;
Queue<Vector2Int> pixels = new Queue<Vector2Int>();
pixels.Enqueue(new Vector2Int(startX, startY));
while (pixels.Count > 0)
{
Vector2Int pixel = pixels.Dequeue();
int x = pixel.x;
int y = pixel.y;
// Перевірка, чи піксель у межах кордонів і ще не заповнений
if (x < 0 || x >= Width || y < 0 || y >= Height || GetFilledValue(x, y))
continue;
// Заповнюємо піксель
SetFilledValue(x, y, true);
GradationMap[y * Width + x] = colorId;
pixelsFilled++;
// Додаємо сусідів для наступної обробки
pixels.Enqueue(new Vector2Int(x + 1, y));
pixels.Enqueue(new Vector2Int(x - 1, y));
pixels.Enqueue(new Vector2Int(x, y + 1));
pixels.Enqueue(new Vector2Int(x, y - 1));
}
return pixelsFilled;
}
Editor is loading...
Leave a Comment