Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #191 from Blaisorblade/simplify-arc-formula
Browse files Browse the repository at this point in the history
Simplify arc formula
  • Loading branch information
Blaisorblade authored Aug 27, 2016
2 parents 91c2638 + d1b3443 commit 24adcfd
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
39 changes: 37 additions & 2 deletions app/src/main/java/com/kamron/pogoiv/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,43 @@ public class Data {
public static int[] arcX;
public static int[] arcY;

public static int convertLevelToIndex(double pokemonLevel){
return (int)(pokemonLevel * 2 -2);
/**
* Convert a pokemon/trainer level to a <em>level index</em> (<code>levelIdx</code> in code).
* The mapping is invertible, but level indexes can be used to index an array (like Data.CpM).
*
* Pokemon levels go from 1 to trainerLevelToMaxPokeLevel(trainerLevel), in increments of 0.5.
* Level indexes go from 0 to trainerLevelToMaxPokeLevelIdx(trainerLevel) in increments of 1.
* This method adjusts a level to a <em>level index</em> (<code>levelIdx</code>), by doubling it
* and subtracting 2.
*/
public static int levelToLevelIdx(double level) {
return (int) (level * 2) - 2;
}

/**
* Convert a <em>level index</em> back to a level. Inverse of levelToLevelIdx, see explanations
* there for rationale.
*/
public static double levelIdxToLevel(int levelIdx) {
return (levelIdx + 2) / 2.0;
}

/**
* Maximum pokemon level for a trainer, from the trainer level. That's usually trainerLevel + 1.5, but
* the maximum is 40 (http://pokemongo.gamepress.gg/power-up-costs).
*/
public static double trainerLevelToMaxPokeLevel(int trainerLevel) {
return Math.min(trainerLevel + 1.5, 40);
}

/*
* Pokemon levels go from 1 to trainerLevel + 1.5, in increments of 0.5.
* Here we use levelIdx for levels that are doubled and shifted by - 2; after this adjustment,
* the level can be used to index CpM, arcX and arcY.
*/
public static int trainerLevelToMaxPokeLevelIdx(int trainerLevel) {
// This is Math.min(2 * trainerLevel + 1, 78).
return levelToLevelIdx(trainerLevelToMaxPokeLevel(trainerLevel));
}

// should be pretty fast https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Java
Expand Down
35 changes: 20 additions & 15 deletions app/src/main/java/com/kamron/pogoiv/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,21 +365,26 @@ public void onAppUpdateEvent(AppUpdateEvent event) {
* Sets up the x,y coordinates of the arc using the trainer level, stores it in Data.arcX/arcY
*/
private void setupArcPoints() {
final int indices = Math.min((int) ((trainerLevel + 1.5) * 2) - 1, 79);
Data.arcX = new int[indices];
Data.arcY = new int[indices];

double maxAngle = 178.4;
double levelCoeff = maxAngle * Data.CpM[trainerLevel * 2 - 2] / (Data.CpM[(int) ((trainerLevel + 1.5) * 2 - 2)] - Data.CpM[0]);

for (double pokeLevel = 1.0; pokeLevel <= trainerLevel + 1.5; pokeLevel += 0.5) {
double angleInDegrees = (Data.CpM[(int) (pokeLevel * 2 - 2)] - Data.CpM[0]) * levelCoeff / Data.CpM[trainerLevel * 2 - 2];

double angleInRadians = (angleInDegrees + 180) * Math.PI / 180.0;

int index = Data.convertLevelToIndex(pokeLevel);
Data.arcX[index] = (int) (arcCenter + (radius * Math.cos(angleInRadians)));
Data.arcY[index] = (int) (arcInitialY + (radius * Math.sin(angleInRadians)));
/*
* Pokemon levels go from 1 to trainerLevel + 1.5, in increments of 0.5.
* Here we use levelIdx for levels that are doubled and shifted by - 2; after this adjustment,
* the level can be used to index CpM, arcX and arcY.
*/
int maxPokeLevelIdx = Data.trainerLevelToMaxPokeLevelIdx(trainerLevel);
Data.arcX = new int[maxPokeLevelIdx + 1]; //We access entries [0..maxPokeLevelIdx], hence + 1.
Data.arcY = new int[maxPokeLevelIdx + 1];

double baseCpM = Data.CpM[0];
double maxPokeCpMDelta = Data.CpM[Math.min(maxPokeLevelIdx + 1, Data.CpM.length)] - baseCpM;

//pokeLevelIdx <= maxPokeLevelIdx ensures we never overflow CpM/arc/arcY.
for (int pokeLevelIdx = 0; pokeLevelIdx <= maxPokeLevelIdx; pokeLevelIdx++) {
double pokeCurrCpMDelta = (Data.CpM[pokeLevelIdx] - baseCpM);
double arcRatio = pokeCurrCpMDelta / maxPokeCpMDelta;
double angleInRadians = (arcRatio + 1) * Math.PI;

Data.arcX[pokeLevelIdx] = (int) (arcCenter + (radius * Math.cos(angleInRadians)));
Data.arcY[pokeLevelIdx] = (int) (arcInitialY + (radius * Math.sin(angleInRadians)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/kamron/pogoiv/OCRHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private Bitmap replaceColors(Bitmap myBitmap, int keepCr, int keepCg, int keepCb
private double getPokemonLevelFromImg(Bitmap pokemonImage, double trainerLevel) {
double estimatedPokemonLevel = trainerLevel + 1.5;
for (double estPokemonLevel = estimatedPokemonLevel; estPokemonLevel >= 1.0; estPokemonLevel -= 0.5) {
int index = Data.convertLevelToIndex(estPokemonLevel);
int index = Data.levelToLevelIdx(estPokemonLevel);
int x = Data.arcX[index];
int y = Data.arcY[index];
if (pokemonImage.getPixel(x, y) == Color.rgb(255, 255, 255)) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/kamron/pogoiv/Pokefly.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ private void setArcPointer(double pokeLevel) {
// }
//
// double angleInRadians = (angleInDegrees + 180) * Math.PI / 180.0;
int index = Data.convertLevelToIndex(pokeLevel);
int index = Data.levelToLevelIdx(pokeLevel);
arcParams.x = Data.arcX[index] - pointerWidth; //(int) (arcCenter + (radius * Math.cos(angleInRadians)));
arcParams.y = Data.arcY[index] - pointerHeight - statusBarHeight; //(int) (arcInitialY + (radius * Math.sin(angleInRadians)));
//System.out.println("Pointer X: " + arcParams.x);
Expand Down

0 comments on commit 24adcfd

Please sign in to comment.