sonar like 2d array

This is a discussion on sonar like 2d array within the C++ Programming forums, part of the General Programming Boards category; hi I'm trying to do trying to find the distance of a given point within a 2d array. e.g 4 ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    8

    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
    Last edited by nimbus2506; 06-29-2011 at 07:20 PM. Reason: update

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    Registered User
    Join Date
    May 2011
    Posts
    8
    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. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    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
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  2. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21