Skip to content

Instantly share code, notes, and snippets.

@KrofDrakula
Last active December 10, 2015 13:48
Show Gist options
  • Save KrofDrakula/4443227 to your computer and use it in GitHub Desktop.
Save KrofDrakula/4443227 to your computer and use it in GitHub Desktop.
Problem: visit every pixel of a 2-dimensional texture using a single sequence generator

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.

Trivial example

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.

Constraints

  • 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 run w×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.

Solutions

Fork and add a JS file to submit a solution. Happy hunting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment