How to find coordinate of node in triangle?

• Mar 2nd 2013, 05:26 PM
Sneaky
How to find coordinate of node in triangle?
Hi,

If I have a triangle like this:

1 2 4 7 11 16
3 5 8 12 17
6 9 13
10 14
15
... which goes on to contain $n$ numbers (although I don't think the size is relavent).

Notice the way it's ordered. The numbers can be considered as the ordinal index of a node of the triangle. The numbers are ordered going diagonally from top right to bottom left, and creating a new diagonal line which is longer as you go right.

The problem is, given a node with ordinal index $q$, how can I find what the $x$ and $y$ coordinate is of $q$? Is there a formula?

For example:
get_x_index(13) = 2
get_y_index(13) = 2

get_x_index(17) = 4
get_y_index(17) = 1

Does anyone know how to do this?

Thanks.
• Mar 2nd 2013, 05:43 PM
emakarov
Re: How to find coordinate of node in triangle?
The function that maps two coordinates into a node index is similar to Cantor pairing function (in the linked page, indices start from 0). See section "Inverting the Cantor pairing function".
• Mar 2nd 2013, 06:14 PM
Sneaky
Re: How to find coordinate of node in triangle?
ok thanks, that worked, but now given the triangle has n numbers where n is the amount of numbers in the triangle, how can I find the width and height of the triangle?
In the example in the beginning post, the width would be 6 and the height would be 5.
• Mar 2nd 2013, 06:28 PM
emakarov
Re: How to find coordinate of node in triangle?
Quote:

Originally Posted by Sneaky
ok thanks, that worked, but now given the triangle has n numbers where n is the amount of numbers in the triangle, how can I find the width and height of the triangle.

If you find the x and y coordinates of n, then I think that one of the dimensions of the triangle is x + y and the other one is either x + y or x + y - 1 depending on whether x (or maybe y) is 0.