Given the following requirements, devise a formula to find x and y as given below
given: length1 length2, assume length1 >= length2
total = length1 + length2
i = 0 : x = 0, y = 0
i = 1 : x = 0, y = 1
...
i = length2 -1 : x = 0, y = length2 -1
i = total-length1 : x = 0, y = 0
i = total-length1 +1 : x = 1, y = 0
...
i = length1 + length2: x = length1 -1, y = 0
So in code, it would look something like:
int length1 = //given
int length2 = //given
int total = length1 + length2;
for (int i = 0; i < total; i++) {
x = ? //answer here
y = ? //answer here
}
Here is an example when length1 = 5; length2 =4
i x,y
---------
i=0 0,0
i=1 0,1
i=2 0,2
i=3 0,3
i=4 0,0
i=5 1,0
i=6 2,0
i=7 3,0
i=8 4,0
edit:
I’m looking for a 1-liner for finding x and y.
Something that divides x out to 0 when i is less than length2 and y to 0 when i is > length1.
1 Answer