How do I get output with this distribution from a x,y coordinate

I have this grid and I want to input a xy coordinate into a method that returns the value on the cell

the grid looks like this:

as you see there is a clear pattern of repetition, i was wondering what is the formula that will output the values for this particular distribution.

Basically I’m looking for a formula in which I input ( x = 2, y =2 ) and it returns 9
x=3. y=3 returns 1 etc

I dont want to fill an array or a grid with these values, just the formula that returns the values for this particular distribution

Thanks!

1 + (x % 3) + 3*(y % 3)

5 Likes

it’s actually quite simple, on the X axis you need to do a modulo operation and just add 3 for every Y.

public int GetValueForCoord(int x, int y){
    return ((x % 3) + 1) + (y * 3);
}

I think, i might be off by 1 in the modulo, i always get it confused, hehe

edit: whoops, didn’t see the repition on the y, use what @Antistone posted

1 Like

Works perfectly, thanks a lot