Calculate the shortest continuous path between shapes without passing thru other shapes in a specific order?

74 Views Asked by At

I have the following points, shapes and paths I would like to calculate how to go thru:

Example

We do not have to move in a diagonal direction if that poses a problem. Here would be the movement with just up, down, left, and right movements between shapes:

enter image description here

As you can see, I first have a circle. Going to that circle from the bottom left and around it, hitting each point is simple. BUT...the second shape is in the top right corner which is a square so I have to go around the shape in between them (so I dont cross it) and hit each of the points all around that square. Once there, I go to the middle, hit each of the points of the star and done. Once that is done, going backwards is pretty easy.

This is a easy example but of course, there can be 100s of shapes inbetween each of them and I have to go to each in the order they are stated.

The main thing I am looking for is how do I calculate a path AROUND a shape that is between another shape which I have to go to first? Im not sure what algorithm to look for. Is it a Travelling salesman problem? A*?

EDIT: The shapes are defined on a grid. This is what I mean:

Grid representation

"1" means it must go thru here and "0" is empty space which can be used to get to from one path to another.