-1

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

abbas
  • 11
  • 1
  • 10
    All the angles? Aren't there an infinite number? – duffymo Oct 02 '10 at 02:54
  • @duffymo well if its between 30 and 310, its not infinite. – Ruel Oct 02 '10 at 02:56
  • this is not homework...i have a tree and i am putting all the angles in the tree and much more...i just need the math function ..im weak at maths, not programming – abbas Oct 02 '10 at 02:56
  • 1
    @Ruel - the range is finite only if you constrain the outputs in some way, eg. whole numbers or to 2 decimal places. My guess is the questioner has misinterpreted or undercommunicated the assignment. – Steve Townsend Oct 02 '10 at 02:57
  • I dont need help on how to store...just the maths part. – abbas Oct 02 '10 at 02:58
  • Not angles, I guess.. As far as I know. it will go up from `29 59' 60"` to `309 59' 59"` Just convert the values to decimal. – Ruel Oct 02 '10 at 02:58
  • like how to calculate the angles.I didi this in high school but forgot the maths.so if someone can provide the pseudo code for this plz – abbas Oct 02 '10 at 02:59
  • @abbas: you haven't answered their questions. what exactly do you want the output to be?? are you looking for whole numbers, or what? – mpen Oct 02 '10 at 02:59
  • 1
    @abbas: Your requirements are underspecified. What do you mean by "return all angles >=30 and <310"? Do you want all *integer* angles between two floats? – In silico Oct 02 '10 at 02:59
  • 1
    There is already an infinity of numbers between 30 and 31... – Breaking not so bad Oct 02 '10 at 03:37
  • When we speak of angles, we're talking about degrees, minutes, and seconds. `30 0 0` to `30 59 60`. You can convert each unit to float. – Ruel Oct 02 '10 at 03:48
  • 1
    @Ruel, we can speak of decimal degrees with as much decimal precision as we want. There is a continuum of angles. In other words, there is an infinite number of angles between `30 0 0` and `30 0 1`. As an analogy: just because wall clocks are in hours:minutes:seconds, doesn't mean that we can't speak about time in nanoseconds or any other arbitrarily small unit. – Emile Cormier Oct 02 '10 at 04:26
  • Fair point. Sorry didn't think of smaller units than seconds. Thanks. – Ruel Oct 02 '10 at 04:37
  • @Ruel - only if you're thinking of integer angles. – duffymo Oct 02 '10 at 13:11
  • abbas, if you want people to spend _their_ free time helping you, spend some of _your_ time to write a clear question. In your question, you have only **one** full stop. Take the time to use correct punctuation, spelling and grammar. – Clonkex May 28 '14 at 12:39

4 Answers4

3

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
  • 26,080
  • 13
  • 87
  • 116
  • 1
    +1 for coming up with a vaguely credible interpretation of a rambling spec.. I'm not convinced though: abbas seemed to think he needed high school maths to solve this, and I just don't see how this is any more mathematical than any other mundane programming task (which he says he's fine with). All very confusing! We're led to think there might be some need for sine, cosine etc.. Nice touch to handling negative & >=360 angles via fmod(). – user433534 Oct 02 '10 at 05:24
  • I believe this is indeed what the OP is asking. The question, while _really_ badly worded, is actually not that difficult to figure out, and once you do you realise there's no (or very little) maths involved. No angle maths necessary, just compare the two values as you have done, Emile. I don't see what the OP's confusion was about. – Clonkex May 28 '14 at 12:42
  • One thing I would say, though. I think the OP would prefer to get a list of the angles, rather than a list of whether or not the angle is in the sector. – Clonkex May 28 '14 at 13:01
2

I am guessing something like:

for( int f = start; f != end; f = (f + 1) % 360 ) {
    // do something with f
}
Arun
  • 18,092
  • 8
  • 46
  • 58
  • ohh..is that what he means by 'wrap around'? yeah... that's a good approach then, as long as he's looking for integers. – mpen Oct 02 '10 at 03:01
2

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
  • 4,501
  • 2
  • 15
  • 21
  • 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 Oct 02 '10 at 03:03
  • Yes between a set of previously entered angles. – abbas Oct 02 '10 at 03:04
  • @TrickyM66: [You need 50 rep to leave comments on posts other than your own.](http://stackoverflow.com/faq) – In silico Oct 02 '10 at 03:07
  • @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. – user433534 Oct 02 '10 at 05:18
1

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
  • 14,301
  • 6
  • 34
  • 49
  • What...the...heck.. Whaaaaaaaaa..... What IS this? You realise the OP wanted users to be able to input an arbitrary number of angles, then input a range and have the program output any of the angles they input first that fall within the range, right? Does...does this output EVERY angle within a range, down the the second?? Woah... – Clonkex May 28 '14 at 12:47