Imagine a grid like the one in the image. I have to model an API which identifies a single segment inside a grid (like the one painted in red).

I thought the segment should be represented with something like this:

int rowIndex -> specifies the row in which the segment is placed
int columnIndex -> specifies the column in which the segment is placed
enum (vertical|horizontal) orientation -> specifies the orientation of the segment

The point at 0,0 is the upper left corner.

For my example, the red line would be rowIndex: 1, columnIndex: 0, orientation: horizontal.

Do you think it's a valid and understandable solution to represent a segment inside a grid or perhaps is there a clearer solution? Is the naming clear?

Grid image

  • 2
    I would use Forward and Down instead of Horizontal and Vertical. This makes it unambiguous. – Martin Maat Apr 4 at 12:09
  • @MartinMaat this is a nice tip. The concepts "forward" and "down" are clear also without reading the documentation of the API. Thanks. – Giorgio Antonioli Apr 4 at 12:40

With your example diagram, it's fairly clear, but 'is placed' is a bit ambiguous as the lines are placed between the rows and columns not in the grid. Make it clear in your documentation that the segments originate from the top left of the cell with the given zero-based indices.

| improve this answer | |

Yes you can do that. It perfectly acceptable.

You are essentially addressing the point in the top left corner of each square. and selecting the right or lower edge. Alternately you are addressing the square and its left, or upper edge.

If the association of the edge to this point/square is irrelevant you could address the edges directly.

h| (0,0), (0,1), (0,2)
v| (1,1), (1,1), (1,2), (1,3)
h| (2,0), (2,1), (2,2)
v| (3,1), (3,1), (3,2), (3,3)
h| (4,0), (4,1), (4,2)
v| (5,1), (5,1), (5,2), (5,3)
h| (6,0), (6,1), (6,2)
v| (7,1), (7,1), (7,2), (7,3)
h| (8,0), (8,1), (8,2)

In this way the column essential identifies if the edge is horizontal or vertical.

Even better there are two enhancements you can go from here with:

1) interleave the face and the corner points into the addressing scheme. These would essentially be additional rows with a 3-4 columns.

2) use a single number to identify the exact edge/point/face in the grid. The first row/column is zero and increment through each column then the next row. This drastically simplifies your interface to a single number. And you can still tell if you have a vertical or horizontal edge (or point or face) by some simple arithmetic.

eg: id mod 2n+1 < n where n is the number of faces wide the grid is, if true means this is a horizontal edge, otherwise its vertical.

If its a horizontal edge adding n or n+1 gives you the vertical edges below it on the left or right. Subtracting it gives you the top left and right vertical edges.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.