算法问题 - 如何建模A *寻路和&打字稿中的矩阵

时间:2018-02-16 09:47:09

标签: angular typescript matrix multidimensional-array a-star

我不会说英语,所以我会试着说清楚!

  1. 我尝试制作Ionic 3应用程序,因此我使用HTML / CSS,Angular 4和Typescript。 (上下文)

  2. 我在此链接后实现了一个星形寻路算法:https://github.com/digitsensitive/astar-typescript。的(成功)

  3. 我使用并改造它,找到从一个点到另一个点的最短路径,通过多个目标。的(成功)

  4. 这就是我得到的:(成功) My result

  5. 现在,我想像我这样建模我的矩阵和路径:(待办事项) What I'd like

  6. 我用div,ngIf,CSS意识到这一点:(完成) My CSS, Div & ngIf

  7. 现在..我不知道如何把绿色路径放进去。 (比如在" 5。")中(待办事项)

    • 我想访问矩阵中每个元素的[x][y]
    • 与myPath的[value, value]比较
    • 如果它是相同的,颜色为绿色或类似的东西..
  8. 你能帮帮我吗?

    非常感谢你阅读这篇(长篇)帖子,Best!

    更新:这是我的代码,找到有多个目标的最短路径(AStar的完整代码在github上)

    import { Component } from '@angular/core';
    import { IonicPage, NavController, NavParams } from 'ionic-angular';
    import { MycartProvider } from '../../providers/mycart/mycart';
    import * as AStar from '../../pathfinding/aStar/main';
    
    @IonicPage()
    @Component({
      selector: 'page-itinerary',
      templateUrl: 'itinerary.html',
    })
    export class ItineraryPage {
    
    private goals=[[2, 2], [4, 4]];
    private newgoals=[];
    private chemintmp=[];
    private chemin=[];
    private noeud1 = {
        'x':0,
        'y':0,
        'dist':0
    };
    private noeudproche = {
        'x':0,
        'y':0,
        'dist':0
    };
    
    private startPos=[0, 0];
    private goalPos=[];
    
    private m_aStarInstance: AStar.AStarFinder;
    private m_grid: AStar.Grid;
    private m_pathway;
    
    private matrice=[
            [0, 0, 0, 0, 0, 0],
            [0, 1, 1, 1, 1, 0],
            [0, 0, 0, 0, 0, 0],
            [0, 1, 1, 1, 1, 0],
            [0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0],
            ];
    
      constructor(public navCtrl: NavController, public navParams: NavParams,
              public cartProvider: MycartProvider) {
      }
    
      ionViewDidLoad() {
          this.defineGoalPos();
      }
    
        defineGoalPos(){
            while(this.goals.length != 0){
                for (let i of this.goals) {
                    this.goalPos=i;
                    let matrix = this.matrice;
                    this.m_grid = new AStar.Grid(matrix);
                    let diagonalMovement = true;
                    this.m_aStarInstance = new AStar.AStarFinder(this.m_grid, diagonalMovement);
                    this.m_pathway = this.m_aStarInstance.findPath(this.startPos, this.goalPos);
    
                    if(this.noeudproche.dist != 0){
    
                        let x1= this.goalPos[0];
                        let y1= this.goalPos[1];
    
                        this.noeud1.x = x1;
                        this.noeud1.y = y1;
                        this.noeud1.dist = this.m_pathway.length;
    
                        if(this.noeud1.dist < this.noeudproche.dist){
                            this.newgoals.push([this.noeudproche.x, this.noeudproche.y]);
                            this.noeudproche.x = this.noeud1.x;
                            this.noeudproche.y = this.noeud1.y;
                            this.noeudproche.dist = this.noeud1.dist;
                            this.chemintmp.splice(0,1);
                            this.chemintmp.push(this.m_pathway);
                        }
                        else{
                            this.newgoals.push([this.noeud1.x, this.noeud1.y]);
                        }
    
                    }
                    else{
                        let x_proche= this.goalPos[0];
                        let y_proche= this.goalPos[1];
    
                        this.noeudproche.x = x_proche;
                        this.noeudproche.y = y_proche;
                        this.noeudproche.dist = this.m_pathway.length;
                        this.chemintmp.splice(0,1);
                        this.chemintmp.push(this.m_pathway);
                    }
                }
                this.chemin.push(this.chemintmp);
                this.goals=this.newgoals;
                this.newgoals=[];
                this.startPos=[this.noeudproche.x, this.noeudproche.y];
                this.noeud1.x = 0;
                this.noeud1.y = 0;
                this.noeud1.dist = 0;
                this.noeudproche.x = 0;
                this.noeudproche.y = 0;
                this.noeudproche.dist = 0;
                this.chemintmp=[];
                //console.log(this.chemin);
            }
        }
    
    }
    

0 个答案:

没有答案