1

I have an A* algorithm implemented, to find a way between two coordinates, and it works good (pretty much).

After that, I need to show a circle, going step by step through the path

Problem is, that the order the coordinates are added to a list, to (then) show them, is based on x, then y; and, sometimes, it has to change directions, like: left, left, left, up, up, up, left, left, down, down, left, up, up, up.

I have tried to order them by x, then y, ascending and descending and vice versa, but, because of the direction changes, it does not work very good for the circle to go step by step.

So, I had an idea, but I am not sure, on how to do it:

First: Create another list, and set ids for each point. Second: Make the initial point from where the path is made, as the id 0 Third: Add the other coords:

private async void showAnimation(int size)
    {
        int idOfMap = 0;
        int xplus = 1;
        int yplus = 1;
        Mapa a = new Mapa();
        List<Mapa> resultMap = new List<Mapa>();

        foreach (Mapa camino in Mapa.mapaCamino)
        {
            if (camino.x == initialStore_x && camino.y == initialStore_y)
            {
                a.x = camino.x;
                a.y = camino.y;
                a.id = idOfMap.ToString();
                resultMap.Add(a);
                idOfMap++;
            }
            for (var i = 0; i < Mapa.mapaCamino.Count; i++)
            {
                if (camino.x == a.x)
                {
                    if (camino.y == (a.y + yplus) || camino.y == (a.y - yplus))
                    {

                    }
                }
            }

        }

(that is what I have so far)

now my problem comes when it has to go up, for instance; after it was going down:

yplus = 5, after going down ... and next time I had to check y, it will need yplus to be 4. And the same thing may happen to x as well (besides I have to check when camino.x == a.x + xplus, etc)

So, what would be a way to have this items, in the list, ordered from point a to point b?

This is the code snipet, in case it helps:

private void ShowRoute(IEnumerable<Point> path, int size)
    {
        Canvas myCanvas1 = new Canvas();
        Canvas myCanvas2 = new Canvas();

        for (int y = 0; y <= this.map.GetLength(1)-1; y++)
        {
            for (int x = 0; x < this.map.GetLength(0); x++)
            {
                //set the initial point
                if (this.searchParameters.StartLocation.Equals(new Point(x, y)))
                {
                    myCanvas1.Background = new SolidColorBrush(Colors.Red);
                    myCanvas1.Height = size;
                    myCanvas1.Width = size;
                    Canvas.SetTop(myCanvas1, (y) * size);
                    Canvas.SetLeft(myCanvas1, (x) * size);
                    myParentCanvas.Children.Add(myCanvas1);
                }
                    //set the end point
                else if (this.searchParameters.EndLocation.Equals(new Point(x, y)))
                {
                    myCanvas2.Background = new SolidColorBrush(Colors.Blue);
                    myCanvas2.Height = size;
                    myCanvas2.Width = size;
                    Canvas.SetTop(myCanvas2, (y) * size);
                    Canvas.SetLeft(myCanvas2, (x) * size);
                    myParentCanvas.Children.Add(myCanvas2);
                }
                    //add the whole path to a list
                else if (this.map[x, y] == true)
                {

                    if (path.Where(p => p.X == x && p.Y == y).Any())
                    {
                        Mapa pathToGo = new Mapa();
                        pathToGo.x = x;
                        pathToGo.y = y;
                        Mapa.mapaCamino.Add(pathToGo);
                    }
                }
            }
        }
        //show the path in the map
        foreach (var camino in Mapa.mapaCamino)
        {
            ImageBrush yellowDot = new ImageBrush();
            yellowDot.ImageSource = new BitmapImage(new Uri("ms-appx:/Imagenes/pathToOz.png", UriKind.Absolute));

            Canvas myCanvas3 = new Canvas();
            myCanvas3.Background = yellowDot;
            myCanvas3.Height = (size + 2);
            myCanvas3.Width = (size + 2);
            Canvas.SetTop(myCanvas3, (camino.y) * size);
            Canvas.SetLeft(myCanvas3, (camino.x) * size);

            myParentCanvas.Children.Add(myCanvas3);
        }
        showAnimation(size);
    }
Gustavo
  • 33
  • 6

0 Answers0