mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-11-26 07:06:44 +01:00
inverse pairing function
This commit is contained in:
parent
abe9d9f5ac
commit
e039bbd56a
@ -82,6 +82,47 @@ public class PlotId {
|
||||
public String toString() {
|
||||
return this.x + ";" + this.y;
|
||||
}
|
||||
|
||||
public PlotId unpair(int hash) {
|
||||
if (hash >= 0) {
|
||||
if (hash % 2 == 0) {
|
||||
// + +
|
||||
hash /= 2;
|
||||
int i = (int) (Math.abs(-1 + Math.sqrt(1 + 8 * hash)) / 2);
|
||||
int idx = hash - ((i * (1 + i)) / 2);
|
||||
int idy = ((i * (3 + i)) / 2) - hash;
|
||||
return new PlotId(idx, idy);
|
||||
}
|
||||
else {
|
||||
// + -
|
||||
hash -= 1;
|
||||
hash /= 2;
|
||||
int i = (int) (Math.abs(-1 + Math.sqrt(1 + 8 * hash)) / 2);
|
||||
int idx = hash - ((i * (1 + i)) / 2);
|
||||
int idy = ((i * (3 + i)) / 2) - hash;
|
||||
return new PlotId(idx, -idy);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (hash % 2 == 0) {
|
||||
// - +
|
||||
hash /= -2;
|
||||
int i = (int) (Math.abs(-1 + Math.sqrt(1 + 8 * hash)) / 2);
|
||||
int idx = hash - ((i * (1 + i)) / 2);
|
||||
int idy = ((i * (3 + i)) / 2) - hash;
|
||||
return new PlotId(-idx, idy);
|
||||
}
|
||||
else {
|
||||
// - -
|
||||
hash += 1;
|
||||
hash /= -2;
|
||||
int i = (int) (Math.abs(-1 + Math.sqrt(1 + 8 * hash)) / 2);
|
||||
int idx = hash - ((i * (1 + i)) / 2);
|
||||
int idy = ((i * (3 + i)) / 2) - hash;
|
||||
return new PlotId(-idx, -idy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
Loading…
Reference in New Issue
Block a user