-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.cs
More file actions
47 lines (38 loc) · 1.27 KB
/
canvas.cs
File metadata and controls
47 lines (38 loc) · 1.27 KB
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
using Cairo;
using System;
class Canvas {
ImageSurface surface;
Maze maze;
Context ctx;
public Canvas(Maze maze) {
this.maze = maze;
surface = new ImageSurface(Format.RGB24, this.maze.cols, this.maze.rows);
ctx = new Context(surface);
}
public void draw_the_maze(Solver solver = null) {
var grid = maze.grid;
int width = grid.GetLength(0), height = grid.GetLength(1);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
(int r, int g, int b) color = !grid[x,y] ? (0,0,0) : (255,255,255);
ctx.SetSourceRGB(color.r, color.g, color.b);
ctx.Rectangle(x,y,1,1);
ctx.Fill();
}
}
if (solver is Solver) {
var end = maze.end;
var result = solver.result;
var current = end;
while (current != (0,0)) {
ctx.SetSourceRGB(255, 0, 0);
ctx.Rectangle(current.x,current.y,1,1);
ctx.Fill();
current = result[current.x + current.y * width];
}
}
}
public void save_image(string filename) {
surface.WriteToPng(filename);
}
}