-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0e5f45
commit 1b770a7
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/cc/modlabs/kpaper/extensions/MathExtensions.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package cc.modlabs.kpaper.extensions | ||
|
||
import kotlin.math.abs | ||
|
||
|
||
private fun getNextId(x: Int, y: Int): Pair<Int, Int> { | ||
val absX: Int = abs(x) | ||
val absY: Int = abs(y) | ||
return if (absX > absY) { | ||
if (x > 0) { | ||
Pair(x, y + 1) | ||
} else { | ||
Pair(x, y - 1) | ||
} | ||
} else if (absY > absX) { | ||
if (y > 0) { | ||
Pair(x - 1, y) | ||
} else { | ||
Pair(x + 1, y) | ||
} | ||
} else { | ||
if (x == y && x > 0) { | ||
return Pair(x, y + 1) | ||
} | ||
if (x == absX) { | ||
return Pair(x, y + 1) | ||
} | ||
if (y == absY) { | ||
Pair(x, y - 1) | ||
} else Pair(x + 1, y) | ||
} | ||
} | ||
|
||
fun getPlotId(id: Int): Pair<Int, Int> { | ||
var x = 0 | ||
var y = 0 | ||
var i = 0 | ||
while (i < id) { | ||
val next = getNextId(x, y) | ||
x = next.first | ||
y = next.second | ||
i++ | ||
} | ||
return Pair(x, y) | ||
} | ||
|
||
fun getPlotIdAsHash(id: Int): String { | ||
val pair = getPlotId(id) | ||
return Base64.encodeToString("${pair.first}:${pair.second}").replace("=", "") | ||
} |