Results 1 to 4 of 4
- 07-01-2010, 07:58 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
A clever way of doing this ... (avoiding a LOT of if-else statements)
Hi, currently my code looks like this:
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:Java 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; }
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.Java 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...
-
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 #3
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
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 #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,376
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java 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 ...
JosLast edited by JosAH; 07-01-2010 at 08:42 PM.
Similar Threads
-
avoiding memory over-consuming
By itaipee in forum New To JavaReplies: 4Last Post: 12-14-2009, 11:59 AM -
Avoiding system.exit()
By swati.jyoti in forum New To JavaReplies: 5Last Post: 07-01-2009, 10:17 AM -
Avoiding refresh
By java_srinivasan in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 06-25-2008, 09:01 AM -
avoiding if statements
By valoyivd in forum New To JavaReplies: 1Last Post: 04-02-2008, 09:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks