|
5
|
|
|
This problem can be solved mathematically much better than it can with a list, or collection.
What you have is 9 states, and a timer that counts down. The timer can be expressed as a proportion of 1. Because the states are all equally spaced on 1/9 intervals, you can just do math....
So, if the time is \$x\$ and the total time is \$y\$, then the current portion is \$\frac{x}{y}\$ If you multiply this value by 9, you get something on the scale of 0 to 9 inclusive. you really want:
public int tileID(int currentTime, int totalTime, int tileCount) {
// we want the shift to happen at less than half-the-time (in the middle of the period).
double shift = (1.0 / tileCount) / 2;
double progress = (double)currentTime / (double)(totalTime);
int tile = (int)(progress * tileCount + shift);
return tile;
}
shift is needed to make the (int) truncation work. Your comment has made me think, and, in reality, it is not the most readable/understandable code. In fact it is broken, and shift should just be 0.5 always... Let me re-do it in the form of a round() instead of a truncation:
public int tileID(int currentTime, int totalTime, int tileCount) {
double progress = (double)currentTime / (double)totalTime;
int tile = (int)Math.round(progress * tileCount + 0.3);
// because your tiles are in the opposite order: tileCount - 1 - tile
return tileCount - 1 - tile;
}
Then, you can use this with:
TargetScope tile = TargetScope.values[tileId(this.time,
TargetManager.TARGET_SEARCH_WAIT, TargetScope.values[].length)];
This problem can be solved mathematically much better than it can with a list, or collection.
What you have is 9 states, and a timer that counts down. The timer can be expressed as a proportion of 1. Because the states are all equally spaced on 1/9 intervals, you can just do math....
So, if the time is \$x\$ and the total time is \$y\$, then the current portion is \$\frac{x}{y}\$ If you multiply this value by 9, you get something on the scale of 0 to 9 inclusive. you really want:
public int tileID(int currentTime, int totalTime, int tileCount) {
// we want the shift to happen at less than half-the-time (in the middle of the period).
double shift = (1.0 / tileCount) / 2;
double progress = (double)currentTime / (double)(totalTime);
int tile = (int)(progress * tileCount + shift);
return tile;
}
shift is needed to make the (int) truncation work. Your comment has made me think, and, in reality, it is not the most readable/understandable code. In fact it is broken, and shift should just be 0.5 always... Let me re-do it in the form of a round() instead of a truncation:
public int tileID(int currentTime, int totalTime, int tileCount) {
double progress = (double)currentTime / (double)totalTime;
int tile = (int)Math.round(progress * tileCount);
// because your tiles are in the opposite order: tileCount - 1 - tile
return tileCount - 1 - tile;
}
Then, you can use this with:
TargetScope tile = TargetScope.values[tileId(this.time,
TargetManager.TARGET_SEARCH_WAIT, TargetScope.values[].length)];
This problem can be solved mathematically much better than it can with a list, or collection.
What you have is 9 states, and a timer that counts down. The timer can be expressed as a proportion of 1. Because the states are all equally spaced on 1/9 intervals, you can just do math....
So, if the time is \$x\$ and the total time is \$y\$, then the current portion is \$\frac{x}{y}\$ If you multiply this value by 9, you get something on the scale of 0 to 9 inclusive. you really want:
public int tileID(int currentTime, int totalTime, int tileCount) {
// we want the shift to happen at less than half-the-time (in the middle of the period).
double shift = (1.0 / tileCount) / 2;
double progress = (double)currentTime / (double)(totalTime);
int tile = (int)(progress * tileCount + shift);
return tile;
}
shift is needed to make the (int) truncation work. Your comment has made me think, and, in reality, it is not the most readable/understandable code. In fact it is broken, and shift should just be 0.5 always... Let me re-do it in the form of a round() instead of a truncation:
public int tileID(int currentTime, int totalTime, int tileCount) {
double progress = (double)currentTime / (double)totalTime;
int tile = (int)Math.round(progress * tileCount + 0.3);
// because your tiles are in the opposite order: tileCount - 1 - tile
return tileCount - 1 - tile;
}
Then, you can use this with:
TargetScope tile = TargetScope.values[tileId(this.time,
TargetManager.TARGET_SEARCH_WAIT, TargetScope.values[].length)];
|
|
|
4
|
|
edited Sep 4 '14 at 19:17
|
This problem can be solved mathematically much better than it can with a list, or collection.
What you have is 9 states, and a timer that counts down. The timer can be expressed as a proportion of 1. Because the states are all equally spaced on 1/9 intervals, you can just do math....
So, if the time is \$x\$ and the total time is \$y\$, then the current portion is \$\frac{x}{y}\$ If you multiply this value by 9, you get something on the scale of 0 to 9 inclusive. you really want:
public int tileID(int currentTime, int totalTime, int tileCount) {
// we want the shift to happen at less than half-the-time (in the middle of the period).
double shift = (1.0 / tileCount) / 2;
double progress = (double)currentTime / (double)(totalTime);
int tile = (int)(progress * tileCount + shift);
return tile;
}
shift is needed to make the (int) truncation work. Your comment has made me think, and, in reality, it is not the most readable/understandable code. In fact it is broken, and shift should just be 0.5 always... Let me re-do it in the form of a round() instead of a truncation:
public int tileID(int currentTime, int totalTime, int tileCount) {
double progress = (double)currentTime / (double)totalTime;
int tile = (int)Math.round(progress * tileCount);
// because your tiles are in the opposite order: tileCount - 1 - tile
return tileCount - 1 - tile;
}
Then, you can use this with:
TargetScope tile = TargetScope.values[tileId(this.time,
TargetManager.TARGET_SEARCH_WAIT, TargetScope.values[].length)];
This problem can be solved mathematically much better than it can with a list, or collection.
What you have is 9 states, and a timer that counts down. The timer can be expressed as a proportion of 1. Because the states are all equally spaced on 1/9 intervals, you can just do math....
So, if the time is \$x\$ and the total time is \$y\$, then the current portion is \$\frac{x}{y}\$ If you multiply this value by 9, you get something on the scale of 0 to 9 inclusive. you really want:
public int tileID(int currentTime, int totalTime, int tileCount) {
// we want the shift to happen at less than half-the-time (in the middle of the period).
double shift = (1.0 / tileCount) / 2;
double progress = (double)currentTime / (double)(totalTime);
int tile = (int)(progress * tileCount + shift);
return tile;
}
shift is needed to make the (int) truncation work. Your comment has made me think, and, in reality, it is not the most readable/understandable code. In fact it is broken, and shift should just be 0.5 always... Let me re-do it in the form of a round() instead of a truncation:
public int tileID(int currentTime, int totalTime, int tileCount) {
double progress = (double)currentTime / (double)totalTime;
int tile = (int)Math.round(progress * tileCount);
return tile;
}
Then, you can use this with:
TargetScope tile = TargetScope.values[tileId(this.time,
TargetManager.TARGET_SEARCH_WAIT, TargetScope.values[].length)];
This problem can be solved mathematically much better than it can with a list, or collection.
What you have is 9 states, and a timer that counts down. The timer can be expressed as a proportion of 1. Because the states are all equally spaced on 1/9 intervals, you can just do math....
So, if the time is \$x\$ and the total time is \$y\$, then the current portion is \$\frac{x}{y}\$ If you multiply this value by 9, you get something on the scale of 0 to 9 inclusive. you really want:
public int tileID(int currentTime, int totalTime, int tileCount) {
// we want the shift to happen at less than half-the-time (in the middle of the period).
double shift = (1.0 / tileCount) / 2;
double progress = (double)currentTime / (double)(totalTime);
int tile = (int)(progress * tileCount + shift);
return tile;
}
shift is needed to make the (int) truncation work. Your comment has made me think, and, in reality, it is not the most readable/understandable code. In fact it is broken, and shift should just be 0.5 always... Let me re-do it in the form of a round() instead of a truncation:
public int tileID(int currentTime, int totalTime, int tileCount) {
double progress = (double)currentTime / (double)totalTime;
int tile = (int)Math.round(progress * tileCount);
// because your tiles are in the opposite order: tileCount - 1 - tile
return tileCount - 1 - tile;
}
Then, you can use this with:
TargetScope tile = TargetScope.values[tileId(this.time,
TargetManager.TARGET_SEARCH_WAIT, TargetScope.values[].length)];
|
|
|
3
|
|
edited Sep 4 '14 at 18:15
|
This problem can be solved mathematically much better than it can with a list, or collection.
What you have is 9 states, and a timer that counts down. The timer can be expressed as a proportion of 1. Because the states are all equally spaced on 1/9 intervals, you can just do math....
So, if the time is \$X\$ \$x\$ and the total time is \$Y\$ \$y\$, then the current portion is \$\frac{x}{y}\$ If you multiply this value by 9, you get something on the scale of 0 to 9 inclusive. you really want:
public int tileID(int currentTime, int totalTime, int tileCount) {
// we want the shift to happen at less than half-the-time (in the middle of the period).
double shift = (1.0 / tileCount) / 2;
double progress = (double)currentTime / (double)(totalTime);
int tile = (int)(progress * tileCount + shift);
return tile;
}
public int tileID(int currentTime, int totalTime, int tileCount) {
// we want the shift to happen at less than half-the-time (in the middle of the period).
double shift = (1.0 / tileCount) / 2;
double progress = (double)currentTime / (double)(totalTime);
int tile = (int)(progress * tileCount + shift);
return tile;
}
shift is needed to make the (int) truncation work. Your comment has made me think, and, in reality, it is not the most readable/understandable code. In fact it is broken, and shift should just be 0.5 always... Let me re-do it in the form of a round() instead of a truncation:
public int tileID(int currentTime, int totalTime, int tileCount) {
double progress = (double)currentTime / (double)(totalTime);totalTime;
int tile = (int)Math.round(progress * tileCount);
return tile;
}
Then, you can use this with:
TargetScope tile = TargetScope.values[tileId(this.time,
TargetManager.TARGET_SEARCH_WAIT, TargetScope.values[].length)];
This problem can be solved mathematically much better than it can with a list, or collection.
What you have is 9 states, and a timer that counts down. The timer can be expressed as a proportion of 1. Because the states are all equally spaced on 1/9 intervals, you can just do math....
So, if the time is \$X\$ and the total time is \$Y\$, then the current portion is \$\frac{x}{y}\$ If you multiply this value by 9, you get something on the scale of 0 to 9 inclusive. you really want:
public int tileID(int currentTime, int totalTime, int tileCount) {
// we want the shift to happen at less than half-the-time (in the middle of the period).
double shift = (1.0 / tileCount) / 2;
double progress = (double)currentTime / (double)(totalTime);
int tile = (int)(progress * tileCount + shift);
return tile;
}
shift is needed to make the (int) truncation work. Your comment has made me think, and, in reality, it is not the most readable/understandable code. Let me re-do it in the form of a round() instead of a truncation:
public int tileID(int currentTime, int totalTime, int tileCount) {
double progress = (double)currentTime / (double)(totalTime);
int tile = (int)Math.round(progress * tileCount);
return tile;
}
Then, you can use this with:
TargetScope tile = TargetScope.values[tileId(this.time,
TargetManager.TARGET_SEARCH_WAIT, TargetScope.values[].length)];
This problem can be solved mathematically much better than it can with a list, or collection.
What you have is 9 states, and a timer that counts down. The timer can be expressed as a proportion of 1. Because the states are all equally spaced on 1/9 intervals, you can just do math....
So, if the time is \$x\$ and the total time is \$y\$, then the current portion is \$\frac{x}{y}\$ If you multiply this value by 9, you get something on the scale of 0 to 9 inclusive. you really want:
public int tileID(int currentTime, int totalTime, int tileCount) {
// we want the shift to happen at less than half-the-time (in the middle of the period).
double shift = (1.0 / tileCount) / 2;
double progress = (double)currentTime / (double)(totalTime);
int tile = (int)(progress * tileCount + shift);
return tile;
}
shift is needed to make the (int) truncation work. Your comment has made me think, and, in reality, it is not the most readable/understandable code. In fact it is broken, and shift should just be 0.5 always... Let me re-do it in the form of a round() instead of a truncation:
public int tileID(int currentTime, int totalTime, int tileCount) {
double progress = (double)currentTime / (double)totalTime;
int tile = (int)Math.round(progress * tileCount);
return tile;
}
Then, you can use this with:
TargetScope tile = TargetScope.values[tileId(this.time,
TargetManager.TARGET_SEARCH_WAIT, TargetScope.values[].length)];
|
|
|
2
|
|
edited Sep 4 '14 at 18:10
|
|
|
|
1
|
|
answered Sep 4 '14 at 17:36
|
|
|