Modelize a Matrix and a A* path in my Ionic App


#1

Hi there,

I’d like to have your advices about something I have to do!

I have an Ionic v3 application.
I have a matrix like this :
matrix = [ [0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 0, 1, 0] ]

I’d like to draw and display a div with whites squares if I have a 0 and black squares if I have a 1.

Do you know how could I make this ?

Thank you for reading !


#2

Hello,
maybe you should use structural directives to iterate over maxtrix and a second time over array inside matrix and decide what to insert with ngif

<ng-container *ngFor="let arrayinside of matrix">
   <ng-container *ngFor="let item of arrayinside ">
<div *ngIf="item == 0" style='whatever'></div>
<div *ngIf="item == 1" style='whateverelse'></div>
  </ng-container>
 </ng-container>

Or any simular structure directives, see for example https://codecraft.tv/courses/angular/built-in-directives/ngif-and-ngswitch/ or https://angular.io/guide/structural-directives

Best regards, anna-liebt


#3

Hey thank you for your reply!

I will take a look at this! :slight_smile:

I heard I could use HTML Canvas, do you think it is a good idea or what you suggest is easier/better ?


#4

Hello,
better? Depends what you wanna do?
Painting? Take canvas.
Simple and easy extendable? Take div
Learn something new with advandages? Take svg
Relax? Take a six pack.

Best regards, anna-liebt.


#5

Ah ah Thank you!

To make easily understandable;

I have implemented A* Pathfinding Algorithm in typescript.

So I have a matrix with 0 for walkable, 1 for not walkable.
I define 1 start position [Ax, Ay], and an array with multiple goals position [[Bx,By], [Cx,Cy] …].

I get the distance and the path between/from A to B, A to C and I take the smallest one.
I push the smallest path in an array called "myPath"
Then I get the path from (for exemple if A to B was the smallest one) B to C and I push the path into “myPath”

So at the end,

I have my matrix with 0 and 1
I have my path from A to C passing through B : [ [A -> B] , [B -> C] ] ==> [ [ [Ax, Ay], [0,1], [0,2], [Bx, By] ] ,
[ [Bx, By], [0,4 ], [0,5], [Cx, Cy] ]

I’d like to modelize my matrix with whites and blacks squares and to modelize my path with greens squares

I hope you have understood everything because I don’t speak english very well!

Bye! :slight_smile:


#6

Hello,

as wrote div is simple and extendable, so it would fit good to your needs. Please think about, div is a block element, but you can unblock it.
Div can used and handled in a wide range. Take a look to div tables and flexbox layouts.

Best regards, anna-liebt


#7

Okay, thank you :slight_smile:
I will take a look!

I try to use div. => Works perfectly, I can display Black squares for value 1 and whites squares for value 0.

My problem is : I have my path from A to C passing through B : [ [A -> B] , [B -> C] ] ==>
[
[ [Ax, Ay], [0,1], [0,2], [Bx, By] ] ,
[ [Bx, By], [0,4 ], [0,5], [Cx, Cy] ]
]

I’d like to display in green, the positions of my path.

Thank you very much for your help!


#8

Hello,
Sorry my english is not really good, so sorry but I do not understand what the problem is.

Best regards, anna-liebt


#9

Sorry, I have updated my previous post :slight_smile:


#10

Hello,

I personal would prefer a custom component, that have propertys and methods.

But if we stay at previous solution you could extend to a third div with your style and your matrix has 0, 1, 2 to indicate which div should used.

A different way is maybe using only one div but use ngstyle, ngclass , etc… for styling the div.

Best regards, anna-liebt


#11

It’s the solution I wanted to try! Maybe it’s not the best one, but I’m gonna try! I will tell you if I succeed or not!

Thank you for your help :smiley: