-
Notifications
You must be signed in to change notification settings - Fork 0
/
parking.js
1 lines (1 loc) · 4.48 KB
/
parking.js
1
(()=>{var t=window.innerWidth-2,e=window.innerHeight-100,n=20,l=[],o=-1,i=-1,c=999999,s="black",d=!0,a=!1,r=!1,u=[],b=document.createElement("canvas");b.setAttribute("id","myCanvas"),b.setAttribute("width",t),b.setAttribute("height",e),b.setAttribute("style","border:1px solid red;"),document.getElementById("body").appendChild(b);var g=b.getContext("2d"),m=0;function f(){for(var t="| |",e=0;e<u.length;e++)t+=" V"+e+"|";for(console.log(t),e=0;e<u.length;e++){for(var n="| V"+e+"|",l=0;l<u[e].length;l++)u[e][l]==c?n+=" ∞ |":n+=" "+u[e][l]+" |";console.log(n)}console.log("")}function v(t,e,o,i){var c=l[t][0],d=l[t][1],a=l[e][0],r=l[e][1],u=Math.sqrt((c-a)*(c-a)+(d-r)*(d-r));!function(t,e,n,l,o){g.strokeStyle=o||s,g.beginPath(),g.moveTo(t,e),g.lineTo(n,l),g.stroke(),g.closePath()}((a-c)*n/u+c,(r-d)*n/u+d,a-(a-c)*n/u,r-(r-d)*n/u,o);var b=(c+a)/2,m=(d+r)/2;i||(i=Math.ceil(10*Math.random())),g.fillStyle=o||s,g.fillText(i,b,m)}function h(t,e,n,l,o){return Math.sqrt((t-n)*(t-n)+(e-l)*(e-l))<=o}b.onclick=function(t){d?function(t){var e=t.clientX-b.getBoundingClientRect().left,n=t.clientY-b.getBoundingClientRect().top;(function(t,e,n,l,o,i){i?(g.strokeStyle=i,g.fillStyle=i):(g.strokeStyle=s,g.fillStyle=s),g.beginPath(),g.arc(t,e,20,0,2*Math.PI),g.stroke(),g.closePath(),g.font="20px Microsoft JhengHei",g.fillText(l,t-10,e+10)})(e,n,0,"V"+m,0,s),l[m]=[e,n],u[m]=[];for(var o=0;o<=m;o++)o==m?u[m][o]=0:(u[m][o]=c,u[o][m]=c);m++,f()}(t):a?function(t){for(var e=t.clientX-b.getBoundingClientRect().left,c=t.clientY-b.getBoundingClientRect().top,d=-1,a=0;a<l.length;a++)if(h(e,c,l[a][0],l[a][1],n)){d=a;break}if(-1!=d||-1!=o)if(-1!=d||-1!=i){if(-1==o)return o=d,void console.log("点击的开始节点是 V"+o);if(-1==i){i=d,console.log("点击的结束节点是 V"+i),console.log("");v(o,i,s,1),u[o][i]=1,u[i][o]=1,f(),o=-1,i=-1}}else alert("请选择连线结束节点!");else alert("请选择连线开始节点!")}(t):r&&function(t){for(var e=t.clientX-b.getBoundingClientRect().left,o=t.clientY-b.getBoundingClientRect().top,i=-1,s=0;s<l.length;s++)if(h(e,o,l[s][0],l[s][1],n)){i=s;break}if(-1!=i||-1!=p)if(-1!=i||-1!=y){if(-1==p)return p=i,void console.log("选择的出发节点是 V"+p);if(-1==y){y=i,console.log("选择的到达节点是 V"+y),console.log("");var d=[],a=-1,r=c;for(s=0;s<u.length;s++)if(s!=p){var g=u[p][s];g<r&&(r=g,a=s),d[s]={checked:!1,path:p+","+s+",",distance:g}}for(B(d);-1!=a;){r=c;var m=-1;for(s=0;s<d.length;s++)if(s!=p&&!d[s].checked&&s!=a){var f=d[a].distance+u[a][s];f<d[s].distance&&(d[s].distance=f,d[s].path=d[a].path+s+","),d[s].distance<r&&(r=d[s].distance,m=s)}d[a].checked=!0,a=m,B(d)}console.log("V"+p+" 到 v"+y+" 的最短距离是"+d[y].distance+",路线是"+d[y].path);var _=d[y].path.split(","),I=-1,E=-1;for(s=0;s<_.length;s++){var A=_[s];A&&0!=A.length&&(A=parseInt(A),-1!=I?(v(I,E=A,"red",u[I][E]),I=E,E=-1):I=A)}}p=-1,y=-1}else alert("请选择到达节点!");else alert("请选择出发节点!")}(t)},document.getElementById("btn_step1").onclick=function(){d=!0,a=!1,r=!1,document.getElementById("btn_step1").setAttribute("disabled","disabled"),document.getElementById("btn_step1").setAttribute("class","active"),document.getElementById("btn_step2").removeAttribute("disabled"),document.getElementById("btn_step2").setAttribute("class","no_active"),document.getElementById("btn_step3").removeAttribute("disabled"),document.getElementById("btn_step3").setAttribute("class","no_active")},document.getElementById("btn_step2").onclick=function(){a=!0,d=!1,r=!1,document.getElementById("btn_step2").setAttribute("disabled","disabled"),document.getElementById("btn_step2").setAttribute("class","active"),document.getElementById("btn_step1").removeAttribute("disabled"),document.getElementById("btn_step1").setAttribute("class","no_active"),document.getElementById("btn_step3").removeAttribute("disabled"),document.getElementById("btn_step3").setAttribute("class","no_active")},document.getElementById("btn_step3").onclick=function(){r=!0,d=!1,a=!1,document.getElementById("btn_step3").setAttribute("disabled","disabled"),document.getElementById("btn_step3").setAttribute("class","active"),document.getElementById("btn_step1").removeAttribute("disabled"),document.getElementById("btn_step1").setAttribute("class","no_active"),document.getElementById("btn_step2").removeAttribute("disabled"),document.getElementById("btn_step2").setAttribute("class","no_active")};var p=-1,y=-1;function B(t){for(var e=0;e<t.length;e++)t[e]&&console.log("checked:"+t[e].checked+", path:"+t[e].path+", distance:"+t[e].distance);console.log("")}})();