Facebook
From Scribby Crane, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 239
  1.         public class OpenL
  2.     {
  3.         public int Current { get; set; }
  4.         public int Parent { get; set; }
  5.         public float F { get; set; }
  6.         public float G { get; set; }
  7.     }
  8.    
  9.    
  10.    
  11.     //Algorytm A*
  12.             Punkt poczatek = punkty.punkty[0];
  13.             Punkt koniec = punkty.punkty[1];
  14.             Punkt sasiad = punkty.punkty[0];
  15.             int index = 0;
  16.             int koszt = 1000;
  17.             int koszt_save = 1000;
  18.             bool bylo = false;
  19.             int index_save = 0;
  20.             var xd = new OpenL { };
  21.  
  22.             //Heuristic H             odleglosc(Node, End);
  23.             //Movement cost G         odleglosc(currentNode, neighbour);
  24.             //Total cost F            F = H(neighbourToEnd) + G;
  25.  
  26.             List<OpenL> OpenList = new List<OpenL>(); // a priority queue of nodes to be transversed (Parent, cost F, cumulative G).
  27.             List<OpenL> OpenList2 = new List<OpenL>(); // a priority queue of nodes to be transversed (Parent, cost F, cumulative G).
  28.             List<int> ClosedList = new List<int>(); // a list of nodes already transversed.
  29.  
  30.             OpenList.Add(new OpenL { Current = 0, Parent = 0, F = odleglosc(poczatek, koniec) + 0, G = 0 });
  31.  
  32.             do
  33.             {
  34.                 foreach (var item in OpenList)
  35.                 {
  36.                     for (int i = 0; i < punkty.rozmiar - 1; ++i)
  37.                     {
  38.                         xd = new OpenL { Current = item.Current, Parent = item.Parent, F = item.F, G = item.G };
  39.                         jest_droga = false;
  40.                         bylo = false;
  41.                         index = item.Current;
  42.                         Punkt currentNode = punkty.punkty[index];
  43.                         Punkt neighbour = punkty.punkty[i];
  44.                         int odl = odleglosc(currentNode, neighbour);
  45.                         if (odl != 0 && odl < odl_max)
  46.                         {
  47.                             jest_droga = true;
  48.                             line(currentNode, neighbour);
  49.                             if (jest_droga)
  50.                             {
  51.                                 foreach (var item2 in OpenList2)
  52.                                 {
  53.                                     if (item2.Current == i) bylo = true;
  54.                                 }
  55.                                 if (!bylo)
  56.                                 {
  57.                                     OpenList2.Add(new OpenL { Current = i, Parent = index, F = odleglosc(neighbour, koniec) + odl, G = odl });
  58.                                 }
  59.                                 koszt = odleglosc(neighbour, koniec) + odl;
  60.                                 if (koszt < koszt_save)
  61.                                 {
  62.                                     index_save = index;
  63.                                     koszt_save = koszt;
  64.                                 }
  65.                             }
  66.                         }
  67.                         sasiad = punkty.punkty[index_save];
  68.                         using (var graphics = Graphics.FromImage(image1))
  69.                         {
  70.                             graphics.DrawLine(bluePen, currentNode.x, currentNode.y, sasiad.x, sasiad.y);
  71.                             pictureBox1.Image = image1;
  72.                             pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
  73.                         }
  74.                     }
  75.                 }
  76.                 OpenList2 = OpenList;
  77.                 OpenList.Remove(xd);
  78.                 OpenList2.Remove(xd);
  79.             }
  80.             while ((sasiad.x != koniec.x) && (sasiad.y != koniec.y));