1. A method for controlling a machine according to a pattern of disconnected contours, comprising: determining a set of costs representing operations of the machine along a set of trajectories connecting a set of exit and entry points on the contours of the pattern, wherein each trajectory represents an operation of the machine proceeding from an exit point with an exit velocity to an entry point with an entry velocity according to dynamics of the machine, wherein the set of trajectories includes at least one trajectory representing the operation along a contour with non-zero velocities at corresponding exit and entry points, and at least one trajectory representing the operation between different contours with non-zero velocities at the corresponding exit and entry points; determining, based on the costs, a sequence of the trajectories optimizing a total cost of operation of the machine tracking the pattern; and determining a set of instructions for controlling the machine according to the sequence, wherein steps of the method are performed by a processor.