A pixel is one position in a raster image’s two-dimensional grid. It usually carries a color, but its value can also represent information such as density or a category. Unlike a vector image, a raster image displays its pixels larger when zoomed in. section 9.1.1
In a PNG maze, exact pixel colors distinguish walls, paths, the entrance, and the treasure. PNG is useful here because JPEG’s lossy compression could change a color the solver needs to recognize. section 9.1.1 section 9.3.1
Find a pixel by its color
Scan within the image’s bounds, comparing each pixel with the entrance color. Return its coordinates when found; report an error otherwise. ```go func (s *Solver) findEntrance() (image.Point, error) { bounds := s.maze.Bounds() for y := bounds.Min.Y; y < bounds.Max.Y; y++ { for x := bounds.Min.X; x < bounds.Max.X; x++ { if s.maze.RGBAAt(x, y) == s.palette.entrance { return image.Point{X: x, Y: y}, nil } } } return image.Point{}, fmt.Errorf("entrance position not found") } ``` Here, `Solver` holds an `*image.RGBA` maze and a palette of `color.RGBA` values. Using `Bounds()` avoids assuming where the image’s coordinates begin. section 9.3.1 section 9.7
Once the entrance is known, pixels become steps in the maze:
- Check the four adjacent coordinates—up, down, left, and right—and inspect their colors. A candidate coordinate may lie outside the image. section 9.3.1 section 9.3.3
- Ignore walls and the previous step; continue along a path, stop at the treasure or a dead end, and send other branches for exploration. section 9.3.3
- Use `Set(x, y, rgba)` to color pixels in the output image, highlighting the route from entrance to treasure. section 9.0 section 9.7
Listing 9.10 neighbours.go: Coordinates of a pixel
package solver
import "image"
// neighbours returns an array of the 4 neighbours of a pixel.
// Some returned positions may be outside the image.
func neighbours(p image.Point) [4]image.Point {
return [...]image.Point{
{p.X, p.Y + 1}, #1
{p.X, p.Y - 1}, #2
{p.X + 1, p.Y}, #3
{p.X - 1, p.Y}, #4
}
}
The four-neighbor function makes the move from pixel coordinates to maze exploration concrete; its returned positions still need bounds and color checks. section 9.3.1 section 9.3.3









