views:

244

answers:

4

I have a set of previously defined float values(a set of angles) I need a function which takes two angles as input and returns all the angles between them. The user first enters a set of angles in float and then the user can enter any two angles(my program should return all the angles between these values) For example 30,310(return all angles >=30 and <310 but 310,30 should also be valid(angles should wrap around)

Thanks in advance

+1  A: 

I am guessing something like:

for( int f = start; f != end; f = (f + 1) % 360 ) {
    // do something with f
}
ArunSaha
ohh..is that what he means by 'wrap around'? yeah... that's a good approach then, as long as he's looking for integers.
Mark
+1  A: 

Do you mean to say your program should return all the angles present in previously entered set of angles?

In that case you just need to compare the stored values and the two input angles. Something like-

for(i=0; i < array_length; i++)

{

   if(array[i] >= value1 && array[i] <= value2)

  {
     cout << array[i];
  }

}

A better way may be to sort the previously stored angles. In that case you won't need to traverse all through the stored values.

If you need to get all the angles between two angles, then that is infinite(if you are not considering only integer values)

pkumar
I think there is confusion.I have the input of angles, its not angles between 2 float numbers,rather angles between my set of float values which is finite
abbas
Yes between a set of previously entered angles.
abbas
@TrickyM66: [You need 50 rep to leave comments on posts other than your own.](http://stackoverflow.com/faq)
In silico
@TrickyM66: He's also attempting to cover a couple possibilities and answers for each - too much ground for a comment + providing code samples that benefit from layout.
+1  A: 

I get what you're asking. For each angle A in a list of angles, you want to know if A is included in the sector defined by the angles B and C. If B>C, then the sector starts at angle B and wraps around the 0 degree mark to end at A.

Here's some code that does what you're asking for:

#include <vector>
#include <iostream>
#include <cmath>

bool angleIsBetween(float angle, float start, float end)
{
    // If angle is outside the range [0,360), convert it to the
    // equivalent angle inside that range.
    angle = fmod(angle, 360.0f);

    // If start>end, then the angle range wraps around 0.
    return (start<=end) ? (angle>=start && angle<=end)
                        : (angle>=start || angle<=end);
}

int main()
{
    float angles[] = {0.0, 180.0, 30};
    size_t nAngles = sizeof(angles)/sizeof(angles[0]);

    for (size_t i=0; i<nAngles; ++i)
    {
        std::cout << angleIsBetween(angles[i], 30.0, 310.0) << " ";
        std::cout << angleIsBetween(angles[i], 310.0, 30) << " ";
    }
    std::cout << "\n";

    return 0;
}

This outputs: 0 1 1 0 1 1

Emile Cormier
A: 

Here's a function that prints all angles between a given range. Hope this helps:

void angles(double a1, double a2) {
    int deg1, min1, sec1, deg2, min2, sec2;
    double const mult = 0.0166666667;
    double angle;
    if (a1 == (int)a1) {
        deg1 = a1; min1 = 0; sec1 = 0;
    } else {
        deg1 = a1;
        min1 = (int)(60 * (a1 - (int)a1));
        sec1 = (int)(60 * ((60 * (a1 - (int)a1)) - min1) + 0.5);
    }
    if (a2 == (int)a2) {
        deg2 = a2 - 1; min2 = 59; sec2 = 60;
    } else {
        deg2 = a2;
        min2 = (int)(60 * (a2 - (int)a2));
        sec2 = (int)(60 * ((60 * (a2 - (int)a2)) - min2) + 0.5);
        if (sec2 == 0) {
            sec2 = 60;
            min2--;
        }
    }
    if (deg1 <= deg2) {
        cout << deg1 << " " << min1 << " " << sec1 << " < " << deg2 << " " << min2 << " " << sec2 << endl;
        while (deg1 <= deg2) {
            if (deg1 < deg2) {
                while (min1 < 60) {
                    while (sec1 < 60) {
                        angle = deg1 + (min1 * mult) + (sec1 * mult * mult);
                        cout << deg1 << " " << min1 << " " << sec1 << " = " << angle << endl;
                        sec1++;
                    }
                    sec1 = 0;
                    min1++;
                }
            } else {
                if (min1 < min2) {
                    while (min1 <= min2) {
                        if (sec1 < sec2) {
                            while (sec1 < 60) {
                                angle = deg1 + (min1 * mult) + (sec1 * mult * mult);
                                cout << deg1 << " " << min1 << " " << sec1 << " = " << angle << endl;
                                sec1++;
                            }
                            sec1 = 0;
                            min1++;
                        } else {
                            while (sec1 <= sec2) {
                                angle = deg1 + (min1 * mult) + (sec1 * mult * mult);
                                cout << deg1 << " " << min1 << " " << sec1 << " = " << angle << endl;
                                sec1++;
                            }
                            sec1 = 0;
                            min1++;
                        }
                    }
                } else {
                    while (min1 < 60) {
                        while (sec1 < 60) {
                            angle = deg1 + (min1 * mult) + (sec1 * mult * mult);
                            cout << deg1 << " " << min1 << " " << sec1 << " = " << angle << endl;
                            sec1++;
                        }
                        sec1 = 0;
                        min1++;
                    }
                }
            }
            min1 = 0;
            deg1++;
        }
    }
}

int main() {
    angles(40.3472, 40.5);
    return 0;
}
Ruel