A*尋路演算法(JavaScript實現)

來源:互聯網
上載者:User

標籤:top   for   char   his   val   排除   att   head   []   

參考資料:http://www.cnblogs.com/zhoug2020/p/3468167.html              http://www.cnblogs.com/lipan/archive/2010/07/01/1769420.html代碼:  
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title>    <style>        div{            position: absolute;            width: 50px;            height: 50px;            margin: 1px 1px;            background-color: gray;            border: 1px dashed #cccccc;        }        body{            position: relative;            font-family: "微軟雅黑";        }        header,        section        {            position: relative;            margin: 0 auto;            width: 998px;        }    </style></head><body>    <header>        <h1>A*尋路demo示範</h1>        <table>            <tr>                <td>輸入地圖列數(最大15列):</td>                <td><input id="rowInput" type="text"/></td>            </tr>            <tr>                <td>請輸入地圖行數(最大15行):</td>                <td><input id="colInput" type="text"></td>            </tr>            <tr>                <td></td>                <td><input id="createMapBtn" type="button" value="點擊產生地圖"/></td>            </tr>            <tr>                <td id="prompt">test...</td>                <td><input id="findPoint" type="button" value="開始尋路" style="display: none"/></td>            </tr>        </table>    </header>    <section id="main"></section>    <script>       function Point(){           this.x=0;           this.y=0;           this.G=0;//G值 開始點 到當前點的移動量           this.H=0;//H值 當前點移動目的地的移動量估算值           this.father=null;       };       Point.prototype={           Console:function(){               console.log("x:"+this.x+" and y:"+this.y);           },           Init:function(x,y,father){               this.x=x;               this.y=y;               this.father=father;           }       };       function AStar(){           //地圖存放二維數組           this.map=[];           //行數           this.rowCount=0;           //列數           this.colCount=0;           //出發點           this.startPoint=new Point();           //終點           this.endPoint=new Point();           //存放Opint類的open數組           this.openList=[];           //存在Opint類的close數組           this.closeList=[];       };       AStar.prototype={           //是否為障礙物           IsBar:function(x,y){               if(this.map[x][y]==3){                   console.log("bar...");                   return true;               }               else{                   console.log("no bar...")                   return false;               }           },           //當前座標是否在OpenList           IsInOpenList:function(x,y){                for(var i=0;i<this.openList.length;i++){                    if(this.openList[i].x==x&&this.openList[i].y==y){                        return true;                    }                }               return false;           },           //當前座標是否在CloseList           IsInCloseList:function(x,y){               for(var i=0;i<this.closeList.length;i++){                   if(this.closeList[i].x==x&&this.closeList[i].y==y){                       return true;                   }               }               return false;           },           //計算G值;(p是Point類)           GetG:function(p){               if(p.father==null){                   return 0;               }               return p.father.G+1;           },           //計算H值           GetH:function(p,pb){               return Math.abs(p.x-pb.x)+Math.abs(p.y-pb.y);           },           //添加當前點的上下左右相鄰的方格到Open列表中           AddNeiToOpenList:function(curPoint){               for(var x=curPoint.x-1;x<=curPoint.x+1;x++){                    for(var y=curPoint.y-1;y<=curPoint.y+1;y++){                        //排除自身以及超出下標的點                        if((x>=0&&x<this.colCount&&y>=0&&y<this.rowCount)&&!(curPoint.x==x&&curPoint.y==y)){                            //排除斜對角                            if(Math.abs(x-curPoint.x)+Math.abs(y-curPoint.y)==1){                                //不是障礙物且不在關閉列表中                                if(this.IsBar(x,y)==false&&this.IsInCloseList(x,y)==false){                                    //不存在Open列表                                    if(this.IsInOpenList(x,y)==false){                                        var point=new Point();                                        point.x=x;                                        point.y=y;                                        point.father=curPoint;                                        point.G=this.GetG(point);                                        point.H=this.GetH(point,this.endPoint);                                        this.openList.push(point);                                    }                                }                            }                        }                    }               }           },           //在openlist集合中擷取G+H為最小的Point點           GetMinFFromOpenList:function(){               var minPoint=null;               var index=0;               for(var i=0;i<this.openList.length;i++){                   if(minPoint==null||minPoint.G+minPoint.H>=this.openList[i].G+this.openList[i].H){                       minPoint=this.openList[i];                       index=i;                   }               }               return{                   minPoint:minPoint,                   index:index               }           },           GetPointFromOpenList:function(x,y){               for(var i=0;i<this.openList.length;i++){                   if(this.openList[i].x==x&&this.openList[i].y==y){                       return this.openList[i];                   }               }               return null;           },           //開始尋找節點           FindPoint:function(){               console.log(this);               this.openList.push(this.startPoint);               while(this.IsInOpenList(this.endPoint.x,this.endPoint.y)==false||this.openList.length==0){                   var curPoint=this.GetMinFFromOpenList().minPoint;                   console.log("curPoint:"+curPoint);                   var index=this.GetMinFFromOpenList().index;                   if(curPoint==null){                       console.log("沒有路");                       return;                   }                   this.openList.splice(index,1);                   this.closeList.push(curPoint);                   this.AddNeiToOpenList(curPoint);               }               var p=this.GetPointFromOpenList(this.endPoint.x,this.endPoint.y);               console.log(p+".....");               while(p.father!=null){                   p= p.father;                   console.log("father..");                   this.map[p.x][p.y]=4;               }               //把終結點也設定成4               this.map[this.endPoint.x][this.endPoint.y]=4;           },           PrintMap:function(){           }       };       //地圖類    map數組儲存數位識別碼 :0 預設 1 開始點 2 結束點 3 障礙物       function Map(id){           this.map=[];           this.container=document.getElementById(id);           this.colCount=0;           this.rowCount=0;       }       Map.prototype={           init:function(colCount,rowCount){               this.colCount=colCount;               this.rowCount=rowCount;               for(var colIndex=0;colIndex<this.colCount;colIndex++){                   this.map.push([]);                   for(var rowIndex=0;rowIndex<this.rowCount;rowIndex++){                       this.map[colIndex].push(0);                   }               }           },           drawMap:function(callback){               for(var colIndex=0;colIndex<this.map.length;colIndex++){                   for(var rowIndex=0;rowIndex<this.map[colIndex].length;rowIndex++){                       var div=document.createElement("div");                       div.style.top=colIndex*50+5+"px";                       div.style.left=rowIndex*50+5+"px";                       div.setAttribute("colIndex",colIndex);                       div.setAttribute("rowIndex",rowIndex);                       div.onclick=callback;                       this.container.appendChild(div);                   }               }           },           drawPoints:function(){               var divs=this.container.children;               console.log(divs.length);               console.log(this.map);               var timer=1000;               for(var i=0;i<divs.length;i++){                   var colIndex=divs[i].getAttribute("colIndex");                   var rowIndex=divs[i].getAttribute("rowIndex");                   if(this.map[colIndex][rowIndex]==4){                       divs[i].style.backgroundColor="red";                   }               }           },           getPoint:function(colIndex,rowIndex){               return this.map[colIndex][rowIndex];           },           setStartPoint:function(colIndex,rowIndex){               this.map[colIndex][rowIndex]=1;           },           setEndPoint:function(colIndex,rowIndex){               this.map[colIndex][rowIndex]=2;           },           setBarPoint:function(colIndex,rowIndex){               this.map[colIndex][rowIndex]=3;           },           clearMap:function(){               this.map.splice(0,this.map.length);               this.container.innerHTML="";           }       }       var rowInput=document.querySelector("#rowInput");       var colInput=document.querySelector("#colInput");       var createMapBtn=document.querySelector("#createMapBtn");       var prompt=document.querySelector("#prompt");       var findPoint=document.querySelector("#findPoint");       var curMap=new Map("main");       var state=0;//狀態標識 0選擇開始節點標識 1選擇結束節點標識 2選擇障礙物標識       var aStar=new AStar();//尋路類       createMapBtn.onclick=function(){           curMap.clearMap();           curMap.init(colInput.value,rowInput.value);           aStar.map=curMap.map;           aStar.colCount=colInput.value;           aStar.rowCount=rowInput.value;           curMap.drawMap(function(){                var colIndex=this.getAttribute("colIndex");                var rowIndex=this.getAttribute("rowIndex");                if(curMap.getPoint(colIndex,rowIndex)!=0){                    return;                }                if(state==0){                    this.style.backgroundColor="blue";                    curMap.setStartPoint(colIndex,rowIndex);                    aStar.startPoint.x=colIndex;                    aStar.startPoint.y=rowIndex;                    state=1;                    prompt.innerHTML="請點擊格子,設定終點";                }else if(state==1){                    this.style.backgroundColor="yellow";                    curMap.setEndPoint(colIndex,rowIndex);                    aStar.endPoint.x=colIndex;                    aStar.endPoint.y=rowIndex;                    prompt.innerHTML="請點擊格子,設定障礙物";                    console.log(findPoint);                    findPoint.style.display="block";                    state=2;                }else if(state==2){                    curMap.setBarPoint(colIndex,rowIndex);                    this.style.backgroundColor="black";                }           });           prompt.innerHTML="請點擊格子,設定起始點";       }       findPoint.onclick=function(){           console.log(aStar.map);           aStar.FindPoint();           curMap.drawPoints();       }    </script></body></html>

  

A*尋路演算法(JavaScript實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.