Fix for silly bug while calculating mean direction

This avoids the nodes from moving out of the boundary.