A clever way of doing this ... (avoiding a LOT of if-else statements)

• 07-01-2010, 07:58 PM
doejohn
A clever way of doing this ... (avoiding a LOT of if-else statements)
Hi, currently my code looks like this:

Code:

```// Adjust x aim point if(xWind < 0.50) {         offsetX = 1; } else if(xWind < 0.70) {         offsetX = 1; } else if(xWind < 0.80) {         offsetX = 2; } else if(xWind < 1.00) {         offsetX = 5; } else {         offsetX = 7; } // Adjust y aim point if(yWind < 0.50) {         offsetY = 14; } else if(yWind < 0.70) {         offsetY = 16; } else if(yWind < 0.80) {         offsetY = 17; } else if(yWind < 1.00) {         offsetY = 18; } else {         offsetY = 19; }```
However, offsetX and offsetY should both depend on both yWind and xWind. So I need to combine the two, so that I can adjust both the X and Y offset for every combination of xWind and yWind, as such:
Code:

```if(xWind < 0.50) {         if(yWind < 0.50) {                 offsetX = 1;                 offsetY = 14;         } else if(yWind < 0.70) {                 offsetX = 1;                 offsetY = 16;         } else if(yWind < 0.80) {                 offsetX = 1;                 offsetY = 17;         } else if(yWind < 1.00) {                 offsetX = 2;                 offsetY = 18;         } else {                 offsetX = 2;                 offsetY = 19;         } } else if(xWind < 0.70) {         if(yWind < 0.50) {                 offsetX = 1;                 offsetY = 14;         } else if(yWind < 0.70) {                 offsetX = 2;                 offsetY = 17;         }         // and so on...```
But that results in 100s of if-else-statements. Is there a better way to do this? I need to be able to manually adjust the offsets for any range of xWind and yWind easily in my code.
• 07-01-2010, 08:18 PM
Fubarable
The best way is to use a mathematical formula for calcuating x and y offsets based on the input. How that's done, I have no idea based on what you've told us. Perhaps you can tell us how these numbers are calculated?
• 07-01-2010, 08:35 PM
doejohn
Ok thanks. I think the formula is hard to figure out manually, so I guess I'll have to collect as much data as possible so I can use a curve fitting tool to find polynomials that match the data (non-linear regression I guess).
• 07-01-2010, 08:39 PM
JosAH
You can do without that long sequence of nested if-else statements but you have to supply the actual data in a couple of matrixes (or, as Fubarable suggested: find a functional depenceny fx(x, y) and fy(x, y) where both fx and fy are the wanted offsets).

Else a few data matrixes and a bit of code are a solution:

Code:

```double winds[][] = // you have to supply the data here. double[] windx[] = // you have to supply the data here. int offsetsX[][] = // again, you supply the data here. int offsetsY[][] = // again, you supply the data here. for (int x= 0; x < windx.length; x++)   if (windx[x] >= actualX)       for (int y= 0; y < winds[x].length; y++)         if (winds[x][y] >= actualY)             // found a value for actualX and actualY: offsetsX[x][y] and offsetsY[x][y] are the answer ...```
kind regards,

Jos