I'm trying to implement something quite simple (in the principle) but i have some troubles with the implementation.
In my programm the user has to choose between 1 up to 4 directions which shall be excluded values. For every direction the user can choose a number range for the values to be excluded (from 0 to 360). But somehow i can't figure out how to make this in an easy way.
My panel looks like that : A)
Here's a short example of what i'm trying to achieve.
I want the same result for this : B)
As for this (but it should work for all the 4 cases of course): C)
Do you have any idea on how to solve this ? Or some hints? Any help will be greatly appreciated. (If you want me to provide the code i tryied, i will).
Edit : Okey, you wanted some code, here is what i did (but it's not working as i want).
content[i].getNwdMean() is the value i want to compare with those in the spinners, to see if my point is in the exclusion zone.
buttons.chkExcludedA1, buttons.chkExcludedA2, buttons.chkExcludedA3 and buttons.chkExcludedA4 are the buttons for the directions to exclude.
buttons.getDir1A(), getDir2A, getDir3A, getDir4A, getDir1B, getDir2B, getDir3B, getDir4B are the values stored in the spinners, in my example C), getDir1A = 180, getDir1B = 190; getDir2A = 190 and getDir2B = 200.
// Is supposed to check the interval in both sides.
public static boolean isBetween(double x, double a, double c)
{
return(x > (a < c ? a : c) && x < (a > c ? a : c));
}
private boolean parsDirections(boolean state)
{
boolean a1 = false, a2 = false, a3 = false, a4 = false;
if (buttons.chkExcludedA1 == true || buttons.chkExcludedA2 == true || buttons.chkExcludedA3 == true || buttons.chkExcludedA4 == true)
{
if (isBetween(content[i].getNwdMean(), buttons.getDir1A(), buttons.getDir1B()) == false)
{
a1 = true;
}
else if (isBetween(content[i].getNwdMean(), buttons.getDir2A(), buttons.getDir2B()) == false)
{
a2 = true;
}
else if (isBetween(content[i].getNwdMean(), buttons.getDir3A(), buttons.getDir3B())== false)
{
a3 = true;
}
else if (isBetween(content[i].getNwdMean(), buttons.getDir4A(), buttons.getDir4B()) == false)
{
a4 = true;
}
}
// Basic test to see if it prints what i want, but it doesn't.
if (a4 == true && a2 == true)
{
print(state);
}
else if (a1 == true && a2 == false)
{
print(state);
}
else
{
return state;
}
return state;
}
You should check the individual chkExcludedAX
's in every if-statement (among other issues with your logic).
Try something like:
boolean excluded1 = buttons.chkExcludedA1 &&
isBetween(content[i].getNwdMean(), buttons.getDir1A(), buttons.getDir1B()));
boolean excluded2 = buttons.chkExcludedA2 &&
isBetween(content[i].getNwdMean(), buttons.getDir2A(), buttons.getDir2B()));
boolean excluded3 = buttons.chkExcludedA3 &&
isBetween(content[i].getNwdMean(), buttons.getDir3A(), buttons.getDir3B()));
boolean excluded4 = buttons.chkExcludedA4 &&
isBetween(content[i].getNwdMean(), buttons.getDir4A(), buttons.getDir4B()));
boolean excluded = (excluded1 || excluded2 || excluded3 || excluded4);
(favouring readability above what's likely to be micro-optimizations)
Or you can combine all of them into one statement:
boolean excluded =
(buttons.chkExcludedA1 &&
isBetween(content[i].getNwdMean(), buttons.getDir1A(), buttons.getDir1B())) ||
(buttons.chkExcludedA2 &&
isBetween(content[i].getNwdMean(), buttons.getDir2A(), buttons.getDir2B())) ||
(buttons.chkExcludedA3 &&
isBetween(content[i].getNwdMean(), buttons.getDir3A(), buttons.getDir3B())) ||
(buttons.chkExcludedA4 &&
isBetween(content[i].getNwdMean(), buttons.getDir4A(), buttons.getDir4B()));