# Thread: sonar like 2d array

1. ## sonar like 2d array

hi I'm trying to do trying to find the distance of a given point within a 2d array.

e.g

4 3 2 2 2 2
4 3 2 1 1 1
4 3 2 1 P 1
4 3 2 1 1 1
4 3 2 2 2 2

where P is a point. In order to do this. I'm planning on splitting the area up in 4 triangles, hence the sonar.

\ 0 0 0 /
0 \ 0 / 0
0 0 p 0 0
0 / 0 \ 0
/ 0 0 0 \

Of course to do this example would be easy because it's just j>i && j<max-1

However if I were to move the P to the left it would be different.

I have one couple of for loops to find the P and place the coordinates under m and n integers.

Code:
```for(i=0;i<MAX;i++){
for(j=0;j<MAX;j++){
if(j<n && i<m ){
arr[i][j]=m-j;
}
}
}```
This comes out as

5 4 3 2 1 0
5 4 3 2 1 0
5 4 3 2 1 0
5 4 3 2 1 0
5 4 3 2 1 0
0 0 0 0 0 P

So it's half right. The numbers that are wrong are depended on the i location. So it's basically what I did with the numbers on the left...
arr[i][j]=n-i;

Any suggestions on how I can get the point to be split up in triangles??

update: j-i<=m-n && i+j<=n+m works for the left hand side. Would this be how you all would do it?

update Doesn't work when n and m are 1,1

2. Given that the thing you are to trying to calculate is a trivial computation (i.e. subtract coordinates and take an absolute value), is there a reason to split the thing up into triangles in the first place?

3. Probably me trying to make this more complicated than it actually is.

I'm split it up because the top triangle will need to be subtracted by i compared to the bottom triangle that will need to be increased by i-MAX

The left triangle will be depended on where Pj is located. So that it will be subtracted by j compared to the right triangle.

Thought by splitting it up into triangles. I can use these if statements to use the correct formula.

4. It looks to me like you want the number of each cell to be equal to:
Code:
`abs(P.x - col) + abs(P.y - row)`
This would give the following for P = (4, 2):
Code:
``` 4 3 2 2 2 2
4 3 2 1 1 1
4 3 2 1 0 1
4 3 2 1 1 1
4 3 2 2 2 2```