Skip to content

Instantly share code, notes, and snippets.

@tommyettinger
Created November 25, 2016 23:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommyettinger/878348cff32e04cf7b9bffa643a58994 to your computer and use it in GitHub Desktop.
Save tommyettinger/878348cff32e04cf7b9bffa643a58994 to your computer and use it in GitHub Desktop.
Quick and easy Halton sequence point generator for libGDX
/**
* Gets a quasi-random Vector2 between (0f,0f) inclusive and (1f,1f) exclusive, assigning into {@code into} the
* {@code index}-th point in the (2, 3) Halton sequence. If index is unique, the Vector2 should be as well for all
* but the largest values of index. You might find an advantage in using values for index that start higher than
* 20 or so, but you can pass sequential values for index and generally get Vector2s that won't be near each other;
* this is not true for all parameters to Halton sequences, but it is true for this one.
* @param into the Vector2 to place the result into and return
* @param index an int that, if unique, positive, and not too large, will usually result in unique Vector2 values
* @return into, modified; usually will have a comfortable distance from Vector2s produced with close index values
*/
public static Vector2 halton(Vector2 into, int index)
{
int s = (index+1 & 0x7fffffff),
numX = s % 2, numY = s % 3, denX = 2, denY = 3;
while (denX <= s) {
numX *= 2;
numX += (s % (denX * 2)) / denX;
denX *= 2;
}
while (denY <= s) {
numY *= 3;
numY += (s % (denY * 3)) / denY;
denY *= 3;
}
if(into == null)
into = new Vector2((float)numX / denX, (float)numY / denY);
else
into.set((float)numX / denX, (float)numY / denY);
return into;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment