开发者

c++ calculate all the angles between two angles [closed]

开发者 https://www.devze.com 2023-01-18 16:30 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reo开发者_如何学Cpened, visit the help center. Closed 12 years ago.

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


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


I am guessing something like:

for( int f = start; f != end; f = (f + 1) % 360 ) {
    // do something with f
}


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)


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消