Apologies for the horrible title. I spent 10 minutes trying to explain this in one sentence and failed.
Although the application prompting this question is in Java (And
Your "levels" can be defined by the following exponential function (with your levels starting at 1):
For simplicity, let's assume you have (scale) and and you're trying to find .
Seeing how is defined, you can see that
However, your scale is not given in neat powers of 2 like that, so you can find a with the following (rounding up as per your scenario):
Which means that and
Now that we have the new level, we have the following equation:
/edit
Yes, that's what 2x-1 means.
sx is just some constant that goes with x (in this case it's your scale), likewise, sy is some constant that goes with y.
The weird square brackets with just the top bit (⌈ ⌉) are just the ceiling function (Math.ceil
).
Logarithms are the inverse of exponentiation. Logarithms are defined as the following: if ac = b, then loga(b) = c. For instance, log2(8) = 3 because 23 = 8. Is anything else confusing?
/java sample
int newLevel = oldLevel + (int) Math.ceil(Math.log(oldScale) / Math.log(2));
double newScale = oldScale * Math.pow(2, oldLevel - newLevel);
We have to use the change of base formula for logarithms because Math only provides loge and log10. It might be worthwhile to create a constant for Math.log(2)
so you aren't constantly recalculating it.
so far...
public static void getZoom(int currentLevel, double userScale){
double ns = (1 << (currentLevel - 1)) * userScale;
int newLevel = 1;
int ts = 1;
while(ts < ns){
ts <<= 1;
newLevel++;
}
double newScale = ns / ts;
System.out.println(newLevel + ", " + newScale);
}
This simple matching game uses enum Game
to specify the constants needed for the game's four levels. By overriding toString()
(and slightly violating the case convention for constants) the Game.values()
can be used to populate the level selection gameCombo
directly. Changing levels then requires little more than replacing the components and invoking validate()
on the enclosing Container
.