The essential property of course is to observe the edge directions. So work out the pseudo-code with this in mind.