//Adicionando o evento onclick no botão, para chamar a função calcular
document.getElementById("btnCalcular").onclick = calcular;
//Função calcular()
//Pega os valores dos campos de texto da tela e cria a instância da
//Classe principal "AStar". Essa classe é instanciada e em seguida chamamos a função getPath, que é quem calcula o caminho.
function calcular() {
let txtConsole = document.getElementById("txtConsole");
txtConsole.innerHTML = "Calculando...";
let _x1 = document.getElementById("x1") as HTMLInputElement;
let x1 = parseInt(_x1.value);
let _y1 = document.getElementById("y1") as HTMLInputElement;
let y1 = parseInt(_y1.value);
let _x2 = document.getElementById("x2") as HTMLInputElement;
let x2 = parseInt(_x2.value);
let _y2 = document.getElementById("y2") as HTMLInputElement;
let y2 = parseInt(_y2.value);
let n1 = new Node(new Position(x1, y1), 10, 0, 0, 0, null);
let n2 = new Node(new Position(x2, y2), 10, 0, 0, 0, null);
let aStar = new AStar();
let path = aStar.getPath(n1, n2);
txtConsole.innerHTML = "";
for (let i = 0; i < path.length; i++) {
txtConsole.innerHTML += "Passo " + i + ": x = " + path[i].position.x + ", y = " + path[i].position.y + "<br/>";
}
}
/**** Classe Position ****/
// Classe auxiliar, que armazena a posição de um determinado nó */
class Position {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
/**** Classe Node ****/
// Classe auxiliar que representa um nó */
class Node {
//Posição do nó
public position: Position;
//Custo para ir para aquele nó
public cost: number;
//Custo agregado desde o início do caminho até o nó em questão
public g: number;
//Valor estimado para chegar ao nó destino
public h: number;
//Somatória do quanto já andamos e o quanto falta andar (G + H)
public f: number;
//Nó pai do nó atual. Se este nó é o B e viemos de A então A é parent de B.
public parentNode: Node;
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.