Simple a* routing algorithm

From , 2 Years ago, written in JavaScript, viewed 148 times.
URL https://pastebin.vip/view/ddb30680
  1. <html><head><title>use A* to find path...</title></head>
  2. <body style="margin:0px">
  3. <script>
  4. /*
  5. written by hjjboy
  6. email:tianmashuangyi@163.com
  7. qq:156809986
  8. */
  9. var closelist=new Array(),openlist=new Array();
  10. var gw=10,gh=10,gwh=14;
  11. var p_start=new Array(2),p_end=new Array(2);
  12. var s_path,n_path="";
  13. var num,bg,flag=0;
  14. var w=30,h=20;
  15. function GetRound(pos){
  16.   var a=new Array();
  17.   a[0]=(pos[0]+1)+","+(pos[1]-1);
  18.   a[1]=(pos[0]+1)+","+pos[1];
  19.   a[2]=(pos[0]+1)+","+(pos[1]+1);
  20.   a[3]=pos[0]+","+(pos[1]+1);
  21.   a[4]=(pos[0]-1)+","+(pos[1]+1);
  22.   a[5]=(pos[0]-1)+","+pos[1];
  23.   a[6]=(pos[0]-1)+","+(pos[1]-1);
  24.   a[7]=pos[0]+","+(pos[1]-1);
  25.   return a;
  26. }
  27. function GetF(arr){
  28.   var t,G,H,F;
  29.   for(var i=0;i<arr.length;i++){
  30.     t=arr[i].split(",");
  31.     t[0]=parseInt(t[0]);t[1]=parseInt(t[1]);
  32.     if(IsOutScreen([t[0],t[1]])||IsPass(arr[i])||InClose([t[0],t[1]])||IsStart([t[0],t[1]])||!IsInTurn([t[0],t[1]]))
  33.         continue;
  34.     if((t[0]-s_path[3][0])*(t[1]-s_path[3][1])!=0)
  35.         G=s_path[1]+gwh;
  36.     else
  37.         G=s_path[1]+gw;
  38.     if(InOpen([t[0],t[1]])){
  39.         if(G<openlist[num][1]){
  40.           openlist[num][0]=(G+openlist[num][2]);
  41.           openlist[num][1]=G;
  42.           openlist[num][4]=s_path[3];
  43.         }
  44.         else{G=openlist[num][1];}
  45.     }
  46.     else{
  47.         H=(Math.abs(p_end[0]-t[0])+Math.abs(p_end[1]-t[1]))*gw;
  48.         F=G+H;
  49.         arr[i]=new Array();
  50.         arr[i][0]=F;arr[i][1]=G;arr[i][2]=H;arr[i][3]=[t[0],t[1]];arr[i][4]=s_path[3];
  51.         openlist[openlist.length]=arr[i];
  52.     }
  53.     if(maptt.rows[t[1]].cells[t[0]].style.backgroundColor!="#cccccc"&&maptt.rows[t[1]].cells[t[0]].style.backgroundColor!="#0000ff"&&maptt.rows[t[1]].cells[t[0]].style.backgroundColor!="#ff0000"&&maptt.rows[t[1]].cells[t[0]].style.backgroundColor!="#00ff00")
  54.     {
  55.         maptt.rows[t[1]].cells[t[0]].style.backgroundColor="#FF00FF";
  56.         //maptt.rows[t[1]].cells[t[0]].innerHTML="<font color=white>"+G+"</font>";
  57.     }
  58.   }
  59. }
  60. function IsStart(arr){
  61.   if(arr[0]==p_start[0]&&arr[1]==p_start[1])
  62.     return true;
  63.   return false;
  64. }
  65. function IsInTurn(arr){
  66.   if(arr[0]>s_path[3][0]){
  67.     if(arr[1]>s_path[3][1]){
  68.         if(IsPass((arr[0]-1)+","+arr[1])||IsPass(arr[0]+","+(arr[1]-1)))
  69.           return false;
  70.     }
  71.     else if(arr[1]<s_path[3][1]){
  72.         if(IsPass((arr[0]-1)+","+arr[1])||IsPass(arr[0]+","+(arr[1]+1)))
  73.           return false;
  74.     }
  75.   }
  76.   else if(arr[0]<s_path[3][0]){
  77.     if(arr[1]>s_path[3][1]){
  78.         if(IsPass((arr[0]+1)+","+arr[1])||IsPass(arr[0]+","+(arr[1]-1)))
  79.           return false;
  80.     }
  81.     else if(arr[1]<s_path[3][1]){
  82.         if(IsPass((arr[0]+1)+","+arr[1])||IsPass(arr[0]+","+(arr[1]+1)))
  83.           return false;
  84.     }
  85.   }
  86.   return true;
  87. }
  88. function IsOutScreen(arr){
  89.   if(arr[0]<0||arr[1]<0||arr[0]>(w-1)||arr[1]>(h-1))
  90.     return true;
  91.   return false;
  92. }
  93. function InOpen(arr){
  94.   var bool=false;
  95.   for(var i=0;i<openlist.length;i++){
  96.     if(arr[0]==openlist[i][3][0]&&arr[1]==openlist[i][3][1]){
  97.         bool=true;num=i;break;}
  98.   }
  99.   return bool;
  100. }
  101. function InClose(arr){
  102.   var bool=false;
  103.   for(var i=0;i<closelist.length;i++){
  104.     if((arr[0]==closelist[i][3][0])&&(arr[1]==closelist[i][3][1])){
  105.         bool=true;break;}
  106.   }
  107.   return bool;
  108. }
  109. function IsPass(pos){
  110.   if((";"+n_path+";").indexOf(";"+pos+";")!=-1)
  111.     return true;
  112.   return false;
  113. }
  114. function Sort(arr){
  115.   var temp;
  116.   for(var i=0;i<arr.length;i++){
  117.     if(arr.length==1)break;
  118.     if(arr[i][0]<=arr[i+1][0]){
  119.         temp=arr[i];
  120.         arr[i]=arr[i+1];
  121.         arr[i+1]=temp;
  122.     }
  123.     if((i+1)==(arr.length-1))
  124.         break;
  125.   }
  126. }
  127. function main(){
  128.     GetF(GetRound(s_path[3]));
  129.     Sort(openlist);
  130.     s_path=openlist[openlist.length-1];
  131.     closelist[closelist.length]=s_path;
  132.     openlist[openlist.length-1]=null;
  133.     if(openlist.length==0){alert("找不到路径");return;}
  134.     openlist.length=openlist.length-1;
  135.     if((s_path[3][0]==p_end[0])&&(s_path[3][1]==p_end[1])){
  136.         getPath();
  137.     }
  138.     else{maptt.rows[s_path[3][1]].cells[s_path[3][0]].style.backgroundColor="#00ff00";setTimeout("main()",100);}
  139. }
  140. function getPath(){
  141.   var str="";
  142.   var t=closelist[closelist.length-1][4];
  143.   while(1){
  144.     str+=t.join(",")+";";
  145.     maptt.rows[t[1]].cells[t[0]].style.backgroundColor="#ffff00";
  146.     for(var i=0;i<closelist.length;i++){
  147.         if(closelist[i][3][0]==t[0]&&closelist[i][3][1]==t[1])
  148.           t=closelist[i][4];
  149.     }
  150.     if(t[0]==p_start[0]&&t[1]==p_start[1])
  151.         break;
  152.   }
  153.   alert(str);
  154. }
  155. function setPos(){
  156.   var h=(Math.abs(p_end[0]-p_start[0])+Math.abs(p_end[1]-p_start[1]))*gw;
  157.   s_path=[h,0,h,p_start,p_start];
  158. }
  159. function set(id,arr){
  160.   switch(id){
  161.     case 1:
  162.         p_start=arr;
  163.         maptt.rows[arr[1]].cells[arr[0]].style.backgroundColor="#ff0000";break;
  164.     case 2:
  165.         p_end=arr;maptt.rows[arr[1]].cells[arr[0]].style.backgroundColor="#0000ff";break;
  166.     case 3:
  167.         n_path+=arr.join(",")+";";maptt.rows[arr[1]].cells[arr[0]].style.backgroundColor="#cccccc";break;
  168.     default:
  169.         break;
  170.   }
  171. }
  172. function setflag(id){flag=id;}
  173. </script>
  174. <table id="maptt" cellspacing="1" cellpadding="0" border="0" bgcolor="#000000">
  175. <script>
  176. for(var i=0;i<h;i++){
  177.   document.write("<tr>");
  178.   for(var j=0;j<w;j++){
  179.     document.write('<td onclick="set(flag,['+j+','+i+']);" bgcolor="#ffffff" width="20" height="20"></td>');
  180.   }
  181.   document.write("</tr>");
  182. }
  183. </script>
  184. </table>
  185. <a href="javascript:setflag(1);">设置起点</a><br>
  186. <a href='javascript:setflag(2);'>设置终点</a><br>
  187. <a href='javascript:setflag(3);'>设置障碍点</a><br>
  188. <input type="button" onclick="setPos();main();this.disabled=true;" value="find">
  189. </body>
  190. </html>

Reply to "Simple a* routing algorithm"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website