-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
100 lines (78 loc) · 2.96 KB
/
Program.cs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using Raylib_cs;
public class Program
{
const int GRID_WIDTH = 32;
const int GRID_HEIGHT = 32;
const int Size = 32;
const int RectSize = 512;
const int BlockSize = RectSize / Size;
static Color[,] Grid = new Color[GRID_WIDTH, GRID_HEIGHT];
public static void Main(string[] args)
{
Raylib.SetConfigFlags(ConfigFlags.FLAG_VSYNC_HINT);
Raylib.InitWindow(1366, 768, "Flood Fill Test");
ClearArray(Color.DARKGRAY);
while (!Raylib.WindowShouldClose())
{
var mx = Raylib.GetMouseX() / BlockSize;
var my = Raylib.GetMouseY() / BlockSize;
if ((mx >= 0 && mx < Grid.GetLength(0)) && my >= 0 && my < Grid.GetLength(1))
{
if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT))
{
FloodFill(mx, my, new Color(Raylib.GetRandomValue(0, 255), Raylib.GetRandomValue(0, 255), Raylib.GetRandomValue(0, 255), 255), Grid[mx, my], mx, my, 10);
}
if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_RIGHT))
{
ClearArray(Color.DARKGRAY);
}
}
Raylib.ClearBackground(Color.BLACK);
Raylib.BeginDrawing();
for (int y = 0; y < GRID_HEIGHT; y++)
{
for (int x = 0; x < GRID_WIDTH; x++)
{
Raylib.DrawRectangle(x * BlockSize, y * BlockSize, BlockSize, BlockSize, Grid[x, y]);
Raylib.DrawRectangleLines(x * BlockSize, y * BlockSize, BlockSize, BlockSize, Color.BLACK);
}
}
Raylib.DrawFPS(2, 2);
Raylib.EndDrawing();
}
Raylib.CloseWindow();
}
static async void FloodFill(int x, int y, Color color, Color old, int originX, int originY, int maxDistance)
{
if (x < 0 || x >= GRID_WIDTH) return;
if (y < 0 || y >= GRID_HEIGHT) return;
var d = GetManhattanDistance(originX, originY, x, y);
if (d > maxDistance)
return;
var c = Grid[x, y];
if (c.R != old.R || c.G != old.G || c.B != old.B)
return;
if (c.R == color.R && c.G == color.G && c.B == color.B)
return;
Grid[x, y] = color;
await Task.Delay(1);
FloodFill(x - 1, y, color, old, originX, originY, maxDistance);
FloodFill(x + 1, y, color, old, originX, originY, maxDistance);
FloodFill(x, y - 1, color, old, originX, originY, maxDistance);
FloodFill(x, y + 1, color, old, originX, originY, maxDistance);
}
static int GetManhattanDistance(int x1, int y1, int x2, int y2)
{
return Math.Abs(x2 - x1) + Math.Abs(y2 - y1);
}
static void ClearArray(Color color)
{
for (int x = 0; x < GRID_WIDTH; x++)
{
for (int y = 0; y < GRID_HEIGHT; y++)
{
Grid[x, y] = color;
}
}
}
}