Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

2-dimensional arrays and the x-y plane

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we are using a 2-dimensional array to model the x-y plane, is there a standard for exactly how the array indexes relate to the coordinate plane?

Specifically, if we have a point represented by myArray[c1][c2], I would assume that (c1,c2) translates directly to (x,y). That is...
  • "c1" represents a position on the x-axis, and "c2" represents a position on the y-axis.
  • Increasing "c1" moves us to the right, and increasing "c2" moves us upward.

  • myArray[][] = { {a, b, c}, {d, e, f}, {g, h, i} } would then translate graphically to...

    cfi
    beh
    adg

    However, I think a lot of people might expect...

    abc
    def
    ghi

    ...which is not only reversing x and y (so the inner arrays represent rows rather than columns), but also moving downward as the row index increases.

    I realize that everyone will likely have a preference, but is there a standard?
     
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Your idea for x & y sounds fine. Your example is 3x3 which I find a bit confusing. I might expect points along a line to look like:

    array = { {0,0}, {1,1}, {2,2}, {3,3} )

    where each pair is {x,y}. Then again, in some problems I might define a game board as {row,column} or {rank,file} which are really {y,x}. It kinda depends on the problem domain. Make your method parameters and variable names as clear as possible to help out the reader.

    Oh, and think about using the Point class you get free with Java. Then you have a one-dimensional array of a useful and standard object!
     
    Ranch Hand
    Posts: 317
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    as a newbie to OO. I want to beg the question. You are trying to create a multi-dimensional array to mimic an array of points on an x-y graph

    Would it not be better to create an array of java.awt.Dimension objects, instead of an array of arrays.
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Just to clarify: This is only hypothetical, and I do not think that a 2-d array is a good way to model this. I would definitely look for other options like using the Point class (or my own class) instead.

    However, I've seen other people try to use the 2-d array approach -- typically reversing x and y -- and I've always wondered if there was some standard for doing this.
     
    Ranch Hand
    Posts: 1608
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It's important to note (since their are traps otherwise) that Java does not have 2 dimensional arrays. What you are referring to is an array of arrays. Some prefer to conceptualise an "array of arrays" as a 2 dimensional array (I don't, since it is fraught with error), so if you must, recognise the limitations on the conceptualisation. It breaks down at some point (early on usually) because it is so far from being a true reflection of reality.

    http://jqa.tmorris.net/faq/GetQAndA.action?qids=63&showAnswers=true
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Tony Morris:
    It's important to note (since their are traps otherwise) that Java does not have 2 dimensional arrays...


    Yes, when I say "2-dimensional array" in this context, I mean Java's model of a 2-dimensional array: An array of arrays.
     
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you think of a 2-D array as a matrix from linear algebra, the standard mathematical approach is to think of array[i][j] as the i-th row and j-th colum where rows are numbered from the top down and columns are numbered from the left to the right. This may be a little backwards from the way you typically visuallize the xy-plane, as you say it. That is why I use i and j as the indices.

    As far as programming is concerned, I don't know that there is any standard that dictates this. In fact, in my opinion, it should be flexible to the given situation. In the case of computer graphics where the array represents the pixels in an image, I think it is typical for array[x][y] to signify a pixel that is x pixels from the left and y pixels from the top of the screen.

    Layne
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Layne Lund:
    ... In the case of computer graphics where the array represents the pixels in an image, I think it is typical for array[x][y] to signify a pixel that is x pixels from the left and y pixels from the top of the screen.


    Yes, that makes sense.
     
    Tony Morris
    Ranch Hand
    Posts: 1608
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    COBOL supports multi-dimensional arrays. C# supports both multi-dimensional arrays of arbitrary element type, which is evidence that a distinction does indeed exist, even ignoring historical definitions.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic