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.
This comes out asCode:for(i=0;i<MAX;i++){ for(j=0;j<MAX;j++){ if(j<n && i<m ){ arr[i][j]=m-j; } } }
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