Skip to content

Instantly share code, notes, and snippets.

@arissa34
Last active June 17, 2021 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arissa34/f31efdee6ab974c7a091f49fb2276d0e to your computer and use it in GitHub Desktop.
Save arissa34/f31efdee6ab974c7a091f49fb2276d0e to your computer and use it in GitHub Desktop.
[LIBGDX] Cantor pairing
package rma.ox.engine.core.math;
import com.badlogic.gdx.math.Vector2;
/****
* When I need to flat 2D coord to an ID
* https://en.wikipedia.org/wiki/Pairing_function
*/
public class CantorPairing {
//πœ‹(π‘Ž,𝑏)=(1/2)(π‘Ž+𝑏)(π‘Ž+𝑏+1)+𝑏 .
public static double pair(double x, double y){
return (0.5f * (x+y) * (x+y+1) + y);
}
public static Vector2 unpair(double z){
Vector2 result = new Vector2();
double t = Math.floor((Math.sqrt((8 * z + 1)) - 1) / 2);
double x = t * (t + 3) / 2 - z;
double y = z - t * (t + 1) / 2;
result.set((float) x, (float) y);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment