Walker on edge of pixel-based shapes

Viewed 36

I want to create a walker that walks around any shape. the shape can be either pixel color in a matrix or selected edges on a graph. the walker have two functions one is determine the next step and the second is move to the next step. The walker starts on some point that adjacent to the shape and has a general direction to where to go (clockwise or counter-clockwise)

enter image description here

Any tips on how should I tackle that ?

1 Answers

Tip: if you're going clockwise, keep your right hand on the shape.

Which means that the code needs to keep track of the orientation of the walker. In the example, the walker is initially facing north (assuming north at the top, and east to the right). After one step, the walker is still facing north.

The orientation of the walker determines the order in which the code should check for the next valid move as shown in the table below:

orientation directions to check
north east, north, west, south
east south, east, north, west
south west, south, east, north
west north, west, south, east

So the walker must be given a starting location, a clockwise/counter-clockwise indication, and an initial orientation.

Related