Thread: Return X and Y from count value

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    Return X and Y from count value

    does anybody have a formula that will allow me to get the Y and X of a location in a 2d array based on its number counting along through the elements, so in a 5x5 array count at 3, 4 is 19, at 2, 2 count is 12 etc.

    i made up the below one but i found it only works with square areas, rectangles give lots of - X coordinate numbers and incorrect Y coordinate ones, strangely the chart output still comes out ok..?

    i am rubbish at maths as you can see

    Code:
     Ycoord = element_number / Y_Range;
     Xcoord  = element_number - X_Range * Ycoord;
    i have done a formula to give me the element number from the co-ordinates but i cant reverse it properly
    Last edited by rogster001; 09-15-2009 at 04:30 AM. Reason: incorrect code posted

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    All you need is the number of columns in the array.
    Code:
    int array[ ROWS ][ COLS ];
    ...
    
    number = 23;
    row = number / COLS;
    col = number % COLS;
    Say COLS is 4.
    row = 23 / 4 = 5
    col = 23 % 4 = 3



    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    superb, thanks for the help!

Popular pages Recent additions subscribe to a feed