Declare a 2D texture with integer coordinates:
______w_______
|(0,0) |
| |
| |h
| |
|_____(w-1,h-1)|
Define a function generator that takes the width and height of the texture and generates a function that returns a pair of (x,y)
or an index, such that:
(x,y) = (index % w, floor(index / w))
To generate the next pair or index, you pass the previous pair or index to the function.
function next(n, w, h) {
return (n + 1) % (w * h);
}
// calculating the first element
var idx = next(0, w, h); // 1
// next element
idx = next(idx, w, h); // 2
// ... etc.
- The function must not store any state about previously generated pairs or indexes.
- The function must output
w×h
unique(x,y)
pairs or indexes when runw×h
times. - Bonus points: the function generates a sequence that appears evenly distributed – admittedly subjective, but dithering and random distributions are preferred. The example provided is a bad one, which simply samples sequentially left-to-right, top-to-bottom.
Fork and add a JS file to submit a solution. Happy hunting!