Last active
June 17, 2021 21:18
-
-
Save arissa34/f31efdee6ab974c7a091f49fb2276d0e to your computer and use it in GitHub Desktop.
[LIBGDX] Cantor pairing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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